Jump to content

AutoIT - Control stop moving when pressed [spacebar]


Recommended Posts

Hello guys! I have created a script in which you can move a control by pressing the arrow (up, bottom, left, right) keys. I got stuck when I wanted to make the control stop when pressing the spacebar. How can I make the control stop moving while pressing the spacebar without affecting the script execution? 

Take a look at the script below:

#include <misc.au3>
#include <array.au3>

func _Distance($body,$pointA,$pointB)

   local $getACoord = ControlGetPos($body,"",$pointA)
   local $getBCoord = ControlGetPos($body,"",$pointB)

   local $xA = $getACoord[0]
   local $yA = $getACoord[1]

   local $xB = $getBCoord[0]
   local $yB = $getBCoord[1]

   return Sqrt(($xB-$xA)^2+($yB-$yA)^2)

EndFunc

func _MoveTop($env,$obj)

   local $getObjCoord = ControlGetPos($env,"",$obj) ; return the current coordinates of the object

   local $objX = $getObjCoord[0]
   local $objY = $getObjCoord[1]

   if GUICtrlSetPos($obj,$objX,$objY-5) = 1 then
      return True
   Else
      return false
   EndIf

endfunc

func _MoveBottom($env,$obj)

   local $getObjCoord = ControlGetPos($env,"",$obj) ; return the current coordinates of the object

   local $objX = $getObjCoord[0]
   local $objY = $getObjCoord[1]

   if GUICtrlSetPos($obj,$objX,$objY+5) = 1 then
      return True
   Else
      return false
   EndIf

endfunc

func _MoveLeft($env,$obj)

   local $getObjCoord = ControlGetPos($env,"",$obj) ; return the current coordinates of the object

   local $objX = $getObjCoord[0]
   local $objY = $getObjCoord[1]

   if GUICtrlSetPos($obj,$objX-5,$objY) = 1 then
      return True
   Else
      return false
   EndIf

endfunc

func _MoveRight($env,$obj,$state)

   local $hReturn[0]

   local $getObjCoord = ControlGetPos($env,"",$obj) ; return the current coordinates of the object

   local $objX = $getObjCoord[0]
   local $objY = $getObjCoord[1]

   Switch $state
   case True
      GUICtrlSetPos($obj,$objX+5,$objY)
   case False
       GUICtrlSetPos($obj,$objX+5,$objY)
    EndSwitch

    return $state

endfunc


global $body = GUICreate("Game Basics [1] - Player Movement",650,550)
global $controller = GUICtrlCreateGraphic(10,10,10,10)
GUICtrlSetBkColor($controller,0)
GUISetState()

While True
   Switch GUIGetMsg()
   case -3
      ExitLoop
   EndSwitch

   if _IsPressed("25") then
      local $left =_MoveLeft($body,$controller)
      if _IsPressed("20") Then
         While _IsPressed("20")
            $left = false
         WEnd
      EndIf ; this block will stop the execution of the script, therefore the controller will not be able to be moved anymore
   EndIf

    if _IsPressed("26") then
      _MoveTop($body,$controller)
   EndIf

    if _IsPressed("27") then
      _MoveRight($body,$controller,true)
      if _IsPressed("20") Then
         _MoveRight($body,$controller,false)
      EndIf
   EndIf

    if _IsPressed("28") then
      _MoveBottom($body,$controller)
   EndIf
WEnd

Thanks in advance!

Link to post
Share on other sites

You could check if space is pressed before checking other keys. Try using a while loop like this (and condense those control moves :))

While True
   Switch GUIGetMsg()
   case -3
      ExitLoop
   EndSwitch

   If Not _IsPressed($SpaceButton) Then

       If _IsPressed($RightButton)  Then _MoveControl($body, $controller, 5, 0)
       If _IsPressed($LeftButton)   Then _MoveControl($body, $controller, -5, 0)
       If _IsPressed($UpButton)     Then _MoveControl($body, $controller, 0, -5)
       If _IsPressed($DownButton)   Then _MoveControl($body, $controller, 0, 5)

   EndIf

WEnd

Edit: You probably should get the DLL into a variable to speed things up (see the _IsPressed helpfile)

Edited by seadoggie01

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts

Link to post
Share on other sites

@seadoggie01, it works! Thanks a lot. Now I've got another problem. I have written a function do detect the collision between two square objects. This is the code block: 

Func _DetectCollision($hwnd,$controller,$collider)

   local $pACoord = ControlGetPos($hwnd,"",$controller)
   local $pBCoord = ControlGetPos($hwnd,"",$collider)

   ; Coordinates of point A including size

   local $pA_left = $pACoord[0]
   local $pA_top = $pACoord[1]
   local $pA_right = $paCoord[0]+$paCoord[2]
   local $pA_bottom = $paCoord[1]+$paCoord[2]

   ; Coordinates of point B including size

   local $pB_left = $pBCoord[0]
   local $pB_top = $pBCoord[1]
   local $pB_right = $pBCoord[0]+$pBCoord[2]
   local $pB_bottom = $pBCoord[1]+$pBCoord[2]

   if $pB_left > $pA_right or $pB_right < $pA_left or $pb_top > $pa_bottom or $pb_bottom < $pa_top Then
      return 0
   else
      return 1
   EndIf

EndFunc

How can I make the $controller stop moving when a collision with the $collider is detected? I have tried something like:

while (true)

    if not _DetectCollision($body,$controller,$collider) then
        if _ispressed("25") then
            _MoveTop($body,$controller)
        endif
        ; ...
        
    endif

and it does not work. What should I do in this case? 

 

Link to post
Share on other sites

Your detection of collision is wrong.

local $pA_bottom = $paCoord[1]+$paCoord[2]

...

local $pB_bottom = $pbCoord[1]+$pbCoord[2]

should be

local $pA_bottom = $paCoord[1]+$paCoord[3]

...

local $pB_bottom = $pbCoord[1]+$pbCoord[3]

And the detection condition below is by far uncomplete.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to post
Share on other sites

You don't want to ignore button presses if there is a collision, otherwise you can never move away from a wall. You can just add something to your _MoveControl functions like this:

If $xPos > $MAX_X Then
    $xPos = $MAX_X
ElseIf $xPos < $MIN_X Then
    $xPos = $MIN_X
EndIf
If $yPos > $MAX_Y Then
    $yPos = $MAX_Y
ElseIf $yPos < $MIN_Y Then
    $yPos = $MIN_Y
EndIf

And setup the max/mins as global const

Edit: Oh, I see what you're doing. I should've read more carefully. Ignore this ^

Edited by seadoggie01

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts

Link to post
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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...