Page 1 of 1

Moving objects with arrowKey

Posted: Thu Dec 22, 2011 11:49 pm
by richardmac
What is the best way to move an object around on a screen using the arrow keys? I tried this:

Code: Select all

on arrowKey whichKey
     if whichKey is "up" then
          aeMoveTo the long id of button "goodGuy", 134,69,5000
     end if
 end arrowKey
This will move a button upward on the screen, but you can't stop it from moving upward until it reaches its destination. I'd like the button to keep moving upward but stop when I release the up arrow. And then do the same thing with the down arrow. I know I can do something like this:

Code: Select all

on arrowKey whichKey
   if whichKey is "up" then
          set the top of button "goodGuy" to the top of button "goodGuy" - 10
   end if
end arrowKey
That works, but it looks absolutely terrible on the screen - it's jumpy movement. And if you go 1 instead of 10 it moves smooth but at a snail's pace.

Any suggestions? I'm in the Game Academy right now and I'll be posting a similar message there, but the aeMoveTo command is nice and smooth and I was hoping I could use it.

Re: Moving objects with arrowKey

Posted: Fri Dec 23, 2011 12:11 am
by richardmac
The following script works really well:

Code: Select all

on keyDown keyname
   if keyname = "a" then aeMoveTo the long id of button "rocket", 134,50,1000
   if keyname = "z" then aeMoveTo the long id of button "rocket", 134,691,1000
end keyDown

on keyUp keyname
   aeStopMoving the long id of button "rocket"
end keyUp
This uses the A key for up and the Z key for down and works great. The problem is that the arrow keys don't send a keyDown or keyUp message - they send an arrowKey message. And there's no message sent when you release an arrow key... or at least no message that I can find. Anyone?

Re: Moving objects with arrowKey

Posted: Fri Dec 23, 2011 5:02 pm
by malte
Hi,

what you can do is use rawkey messages, or poll the keysDown function. I'll be posting a few demo scripts tonight. (Hollidays and kids keeping me busy at the moment)

All the best,

Malte

Re: Moving objects with arrowKey

Posted: Sat Dec 24, 2011 12:33 am
by malte
Ok,

quickly checked. The messages that are fired are:

on arrowKey theKey
put theKey -- up, down, left or right
end arrowKey

on rawKeyUp theKey
put theKey
end rawKeyUp

--

That said, this might not be the ideal solution. Here is the why. Keyboards can be set to have different repeat rates. So the messages will not be fired consistently across machines and you might see different behaviours on different machines. If you have interest in a more elaborate example, just give me a holler.

All the best,

malte

Re: Moving objects with arrowKey

Posted: Sun Jan 01, 2012 2:35 am
by emdalton
malte wrote:Hi,

what you can do is use rawkey messages, or poll the keysDown function. I'll be posting a few demo scripts tonight. (Hollidays and kids keeping me busy at the moment)

All the best,

Malte
Polling the keysDown function instead of using on arrowkey might work... I'm trying to do the same thing. I'll give it a try and post back.