Moving objects with arrowKey

Create fast, sprite powered games with Animation Engine, co-developed with DerBrill Software!

Moderators: heatherlaine, kevinmiller, robinmiller, malte

Post Reply
richardmac
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 211
Joined: Sun Oct 24, 2010 12:13 am

Moving objects with arrowKey

Post by richardmac » Thu Dec 22, 2011 11:49 pm

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.

richardmac
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 211
Joined: Sun Oct 24, 2010 12:13 am

Re: Moving objects with arrowKey

Post by richardmac » Fri Dec 23, 2011 12:11 am

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?

malte
Posts: 1098
Joined: Thu Feb 23, 2006 8:34 pm
Location: Ostenfeld germany
Contact:

Re: Moving objects with arrowKey

Post by malte » Fri Dec 23, 2011 5:02 pm

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

malte
Posts: 1098
Joined: Thu Feb 23, 2006 8:34 pm
Location: Ostenfeld germany
Contact:

Re: Moving objects with arrowKey

Post by malte » Sat Dec 24, 2011 12:33 am

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

emdalton
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 152
Joined: Thu Jul 30, 2009 4:03 am

Re: Moving objects with arrowKey

Post by emdalton » Sun Jan 01, 2012 2:35 am

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.
Elizabeth

Image

Post Reply

Return to “Animation Engine”