Moving an object

Creating Games? Developing something for fun?

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
SammL1
Posts: 5
Joined: Thu Jun 05, 2014 11:12 am

Moving an object

Post by SammL1 » Sat Aug 16, 2014 3:32 pm

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

sefrojones
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 447
Joined: Mon Jan 23, 2012 12:46 pm

Re: Moving an object

Post by sefrojones » Sat Aug 16, 2014 3:57 pm

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

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10115
Joined: Fri Feb 19, 2010 10:17 am

Re: Moving an object

Post by richmond62 » Sat Aug 16, 2014 8:50 pm

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

sefrojones
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 447
Joined: Mon Jan 23, 2012 12:46 pm

Re: Moving an object

Post by sefrojones » Sat Aug 16, 2014 8:57 pm

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

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10115
Joined: Fri Feb 19, 2010 10:17 am

Re: Moving an object

Post by richmond62 » Sat Aug 16, 2014 9:02 pm

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.

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

Re: Moving an object

Post by malte » Tue Aug 19, 2014 3:51 pm

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

Post Reply