Using arrowkey to move rocket

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
Newbie4
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 332
Joined: Sun Apr 15, 2012 1:17 am
Contact:

Using arrowkey to move rocket

Post by Newbie4 » Sun Apr 15, 2012 1:36 am

I want to use the arrowkeys to move the rocket up and down and left and right. I can do it on a card by itself but when I put it in the "on updateScreen" block of code, it keeps giving errors (card "rocket": compilation error at line 28 (if: error in command) near "on", char 1).

What am I doing wrong or what should I be using?

This is from Game Academy - Lesson one where I want to use the arrow keys instead of just the mouse to move the rocket.

The code is
on updateScreen
if the mouse is down then
....
end updateScreen

I want to put the following code in
on arrowkey x
if x = "up"
.....
end updateScreen

It works on a card by itself but not in this case. I can use the arrow keys to move the rocket, up/down/left/right with the full code. But how do I get that functionality to work in the updateScreen code? Any ideas?
Thank you
Cyril Pruszko
https://sites.google.com/a/pgcps.org/livecode/
https://sites.google.com/a/setonhs.org/app-and-game-workshop/home
https://learntolivecode.com/

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: Using arrowkey to move rocket

Post by sturgis » Sun Apr 15, 2012 4:07 am

You can't use a handler within a handler so

on updatescreen
stuff here

on arrowkey

end arrowkey

end updatescreen

won't work. To use the updatescreen method, the smoothest way to get this to work is by using keysdown(). keysdown returns the keycodes for all keys currently pressed so you can do something like..

Code: Select all

if 65361 is among the items of keysdown() then 
 --move the rocket left.
end if

if 65363 is among the items of keysdown() then
-- move the rocket right
end if
Be aware that there is a thread here that goes over some keyboards not correctly handling multiple arrow keys down at the same time (seems to be a keyboard issue, some keyboards work fine, some don't). I don't know if this has been fixed in the most recent version of lc or if its even fixable on the lc side. If you need, let me know and i'll see if I can relocate the thread.

Another would be to have an on rawkeydown handler and a on rawkeyup handler and track the keypresses yourself. Meaning a key is pressed and you add the keycode to a list of keys and then check that list from within your updatescreen handler to see if the key is down. Remove the key from the list on rawkeyup. This method might suffer from the same drawback as keysdown() though if its a hardware quirk.

Newbie4
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 332
Joined: Sun Apr 15, 2012 1:17 am
Contact:

Re: Using arrowkey to move rocket

Post by Newbie4 » Sun Apr 15, 2012 5:07 am

Thank you, I had a feeling that it would be something like that.

Where do you find the numbers for each key? (65361, 65363,...?)

Is there another way other than updateScreen?

Also, the narrator in the Game Academy, in the first video said that the UpdateScreen message was not handled in LiveCode itself but would be in a few weeks. What did he mean and how would that affect the code?

That was back in the first week of Dec. Has it been added to LiveCode? Should it be done differently now?

Thank you for your help
Cyril Pruszko
https://sites.google.com/a/pgcps.org/livecode/
https://sites.google.com/a/setonhs.org/app-and-game-workshop/home
https://learntolivecode.com/

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: Using arrowkey to move rocket

Post by sturgis » Sun Apr 15, 2012 5:24 am

Since you already have things set up with the gameloop you can just use it to figure out the keys you need to track.
add this line to the updateScreen handler

Code: Select all

put keysdown() into msg -- will show the keys currently down in the message box
This way you can push a key, make note of the number that shows up in the message box. I can never remember keycodes so I do this quite often.
while you are doing this you can press multiple keys at the same time and see if your keyboard is on the strange behavior list. push an arrow key, then start pushing more. See if the number of items, and keycodes in the message box match the keys you are pressing. (like I say, don't know if this is an lc issue or a keyboard weirdness. I'll see if I can find that other thread that talks about it)

As for the updatescreen handler, as of 5.5 it is not there yet. My guess is other priorities have superseded that for now, but the stack script from the game academy works very well so its not a big issue. When it does become a part of LC you won't need the stack script and there will most likely be some tweaks and changes to things so at that point you'll want to read up on the differences and adjust your scripts accordingly. (as well as remove the stack script that controls updatescreen)

I'm sure there are quite a few ways to accomplish similar functionality but hard to say what would work best. For moving a rocket around in a game like manner a gameloop such as is being used with updatescreen is a really good way to go. You could roll your own, and since I don't know exactly what you're trying to accomplish its hard to say more. There might be a better method for your specific game but hard to say.

And to reiterate, nope its not in LC yet as far as I can tell so for now the stack script provided by david is probably the best way to go.
Newbie4 wrote:Thank you, I had a feeling that it would be something like that.

Where do you find the numbers for each key? (65361, 65363,...?)

Is there another way other than updateScreen?

Also, the narrator in the Game Academy, in the first video said that the UpdateScreen message was not handled in LiveCode itself but would be in a few weeks. What did he mean and how would that affect the code?

That was back in the first week of Dec. Has it been added to LiveCode? Should it be done differently now?

Thank you for your help

Post Reply