Jump to content

Escaping button thing


Jex
 Share

Recommended Posts

I'm bored and tryied create little game. Left and right sides (22 and 24 lines) working good but up and down sides move button randomly? (26 and 28 lines) I can't figure out what is problem, maybe im doing small mistake <_<

#include <GUIConstants.au3>

Opt("MouseCoordMode", 2)
$Width = 500
$Height = 500

$Gui = GuiCreate("Test",$Width,$Height)
$Button = GUICtrlCreateButton("",250,250,50,50)
GUISetState()

While 1
    $msg = GUIGetMsg()
    If $msg = -3 Then Exit
    $Pos = MouseGetPos()
    $ControlPos = ControlGetPos("","",$Button)
    If $Pos[0] >= 0 And $Pos[0] <= $Width And $Pos[1] >= 0 And $Pos[1] <= $Height Then ;If mouse inside window
        ToolTip("Mouse X: " & $Pos[0] & " Y: " & $Pos[1] & " Button X: " & $ControlPos[0] & " Y: " & $ControlPos[1])
        $x1 = $ControlPos[0] - 20
        $x2 = $ControlPos[0] + $ControlPos[2] + 20
        $y1 = $ControlPos[1] - 20
        $y2 = $ControlPos[1] + $ControlPos[3] + 20
        If $Pos[0] > $x1 And $Pos[0] < $x1 + 20 And $Pos[1] > $y1 + 20 And $Pos[1] < $y2 - 20 Then ;Left
            GUICtrlSetPos($Button, $Pos[0] + 20, $ControlPos[1])
        ElseIf $Pos[0] < $x2 And $Pos[0] > $x2 - 20 And $Pos[1] > $y1 + 20 And $Pos[1] < $y2 - 20 Then ;Right
            GUICtrlSetPos($Button, ($Pos[0] - $ControlPos[2]) - 20, $ControlPos[1])
        ElseIf $Pos[1] > $y1 And $Pos[1] < $y1 + 20 And $Pos[0] > $x1 + 20 And $Pos[0] < $x2 - 20 Then ;Up
            GUICtrlSetPos($Button, $Pos[1] + 20, $ControlPos[0])
        ElseIf $Pos[1] < $y2 And $Pos[1] > $y2 - 20 And $Pos[0] > $x1 + 20 And $Pos[0] < $x2 - 20 Then ;Down
            GUICtrlSetPos($Button, ($Pos[1] - $ControlPos[3]) - 20, $ControlPos[0])
        EndIf
    EndIf
WEnd
Link to comment
Share on other sites

#include <GUIConstants.au3>

Opt("MouseCoordMode", 2)
$Width = 500
$Height = 500

$Gui = GuiCreate("Test",$Width,$Height)
$Button = GUICtrlCreateButton("",250,250,50,50)
GUISetState()

While 1
    $msg = GUIGetMsg()
    If $msg = -3 Then Exit
    $Pos = MouseGetPos()
    $ControlPos = ControlGetPos("","",$Button)
    If $Pos[0] >= 0 And $Pos[0] <= $Width And $Pos[1] >= 0 And $Pos[1] <= $Height Then ;If mouse inside window
        ToolTip("Mouse X: " & $Pos[0] & " Y: " & $Pos[1] & " Button X: " & $ControlPos[0] & " Y: " & $ControlPos[1])
        $x1 = $ControlPos[0] - 20
        $x2 = $ControlPos[0] + $ControlPos[2] + 20
        $y1 = $ControlPos[1] - 20
        $y2 = $ControlPos[1] + $ControlPos[3] + 20
        If $Pos[0] > $x1 And $Pos[0] < $x1 + 20 And $Pos[1] > $y1 + 20 And $Pos[1] < $y2 - 20 Then ;Left
           GUICtrlSetPos($Button, $Pos[0] + 20, $ControlPos[1])
        ElseIf $Pos[0] < $x2 And $Pos[0] > $x2 - 20 And $Pos[1] > $y1 + 20 And $Pos[1] < $y2 - 20 Then ;Right
            GUICtrlSetPos($Button, ($Pos[0] - $ControlPos[2]) - 20, $ControlPos[1])
        ElseIf $Pos[1] > $y1 And $Pos[1] < $y1 + 20  And $Pos[0] > $x1 And $Pos[0] < $x2 -20 Then ;Down
            GUICtrlSetPos($Button,$ControlPos[0] , $Pos[1] + 20)
        ElseIf $Pos[1] < $y2 And $Pos[1] > $y2 - 20 And $Pos[0] > $x1 + 20 And $Pos[0] < $x2 - 20 Then ;Up
            GUICtrlSetPos($Button,$ControlPos[0] ,($Pos[1] - $ControlPos[3]) - 20 )
        EndIf
    EndIf
WEnd

<_<

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...