Moving an object
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
Moving an object
Hi all, I am planning on making a game but I am not sure how to create a command to check for input to make an object or button move on a key press, how would I go about doing this?
I only want to make it move around on a card at the moment
I only want to make it move around on a card at the moment
-
- Livecode Opensource Backer
- Posts: 447
- Joined: Mon Jan 23, 2012 12:46 pm
Re: Moving an object
This site has a great course on beginner game making, and covers this pretty early in the lessons:
https://sites.google.com/a/pgcps.org/livecode/home
--Sefro
edit: this link has a frogger example that covers your exact request
https://sites.google.com/a/pgcps.org/li ... code-links
https://sites.google.com/a/pgcps.org/livecode/home
--Sefro
edit: this link has a frogger example that covers your exact request

https://sites.google.com/a/pgcps.org/li ... code-links
-
- Livecode Opensource Backer
- Posts: 10115
- Joined: Fri Feb 19, 2010 10:17 am
Re: Moving an object
First of all:
there is a command which you could have in your card:
on keyDown KD
if KD = K then move image "mySillyPicture" to 10,10
else pass keyDown
end keyDown
the only SNAG about that is if your end-user is using, say, an Armenian keyboard . . . no 'K'
so this is probably better:
on rawKeyDown RK
if RK = 107 then move image "mySillyPicture" to 10,10
else pass rawKeyDown
end rawKeyDown
there is a command which you could have in your card:
on keyDown KD
if KD = K then move image "mySillyPicture" to 10,10
else pass keyDown
end keyDown
the only SNAG about that is if your end-user is using, say, an Armenian keyboard . . . no 'K'
so this is probably better:
on rawKeyDown RK
if RK = 107 then move image "mySillyPicture" to 10,10
else pass rawKeyDown
end rawKeyDown
-
- Livecode Opensource Backer
- Posts: 447
- Joined: Mon Jan 23, 2012 12:46 pm
Re: Moving an object
This has me wondering. All the examples I have seen use the ArrowKey handler to demonstrate moving an object with the keyboard, is ArrowKey universal? Or is it better to use RawKeydown?richmond62 wrote: the only SNAG about that is if your end-user is using, say, an Armenian keyboard . . . no 'K'
--Sefro
-
- Livecode Opensource Backer
- Posts: 10115
- Joined: Fri Feb 19, 2010 10:17 am
Re: Moving an object
When in doubt stick to rawKeyDown 
Living in Bulagria, as I do, you very quickly learn what a pain-in-the bum different keyboard layouts
and/or physical keyboards can be.

Living in Bulagria, as I do, you very quickly learn what a pain-in-the bum different keyboard layouts
and/or physical keyboards can be.
Re: Moving an object
My suggestion would be to set up a timer and poll for the keysDown
The advantage here is that you can
a) check for more than one key at the time
and
b) are not depending on all users having set the same key repeat rate (this differs depending on user settings)
Hope tht helps,
Malte
Code: Select all
on mouseUp
set the gameStarted of me to not the gameStarted of me
if the gameStarted of me then pollKeyboard
end mouseUp
on pollKeyboard
put the keysDown
if the gameStarted of me then send "pollKeyboard" to me in 40 millisecs
end pollKeyboard
a) check for more than one key at the time
and
b) are not depending on all users having set the same key repeat rate (this differs depending on user settings)
Hope tht helps,
Malte