Page 1 of 1

Moving an object

Posted: Sat Aug 16, 2014 3:32 pm
by SammL1
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

Re: Moving an object

Posted: Sat Aug 16, 2014 3:57 pm
by sefrojones
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

Re: Moving an object

Posted: Sat Aug 16, 2014 8:50 pm
by richmond62
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

Re: Moving an object

Posted: Sat Aug 16, 2014 8:57 pm
by sefrojones
richmond62 wrote: the only SNAG about that is if your end-user is using, say, an Armenian keyboard . . . no 'K'
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?


--Sefro

Re: Moving an object

Posted: Sat Aug 16, 2014 9:02 pm
by richmond62
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.

Re: Moving an object

Posted: Tue Aug 19, 2014 3:51 pm
by malte
My suggestion would be to set up a timer and poll for the keysDown

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