Page 1 of 1

Handling Keyboard events in Continuous Action Games

Posted: Tue Jan 06, 2009 10:37 pm
by Steelweaver52
Hi, Friends!

I'm a new Rev programmer, which I think will become apparent very quickly as I ask questions.

Basically, I'm interested in writing games where multiple objects are moving simultaneously. I intend to drive this movement within a repeat loop, which is contained within a mouseUp handler. For example,

Code: Select all

On mouseUp
.
.
.

Repeat while exit_has_not_been_requested

   -- do a bunch of stuff with graphic objects

end Repeat
.
.
.

end mouseUp
But I also want to use keyboard events to interact with and drive my moving graphic objects, presumably like this.

Code: Select all

on keyDown some_funky_key 

  -- make the graphic objects do stuff

end keyDown
In many applications, once an event is handled, the program goes into a "wait state" until the next event arises. In my case, I don't want the program to enter a "wait state" after the keyboard event is handled. Rather, I want program control to return to the repeat loop so that it can continue driving the movement of graphic objects.

Also, should the keyDown handler be coded into the script of the card on which the graphic objects reside? If so, how would one make sure that card has "focus" so that Rev knows to require that the card script handle the keyboard event?

Thanks!

---Tom Nally, New Orleans

[edit: some spelling corrected; other errors probably missed...]

Posted: Wed Jan 07, 2009 7:14 am
by Janschenkel
Hi Tom,

Rather than using 'repeat' loops for your animations, you should look into 'send in time' constructs to choreograph the animations. I'll give you a quick example of setting up an 'animation heartbeat' - create a large stack and plop a button somewhere in the middle, then give it this script

Code: Select all

constant kAnimateInterval = 25  -- milliseconds, so about 40 frames per second
constant kAnimationLength = 2000  -- milliseconds, so will finish in 2 seconds
local sStartTime, sStartLoc, sTargetLoc, sAnimateMsgId

on mouseUp
   -- get the next random location
   put the milliseconds into sStartTime
   put the loc of me into sStartLoc
   put abs(item 1 of sStartLoc + random(200) - random(200)), \
   abs(item 2 of sStartLoc + random(200) - random(200)) into sTargetLoc
   if sAnimateMsgId is empty then
      -- kick-start the animation
      send "AnimateMe" to me in kAnimateInterval milliseconds
      put the result into sAnimateMsgId
   end if
end mouseUp

on AnimateMe
   -- first determine how far along the animation we are
   put the milliseconds - sStartTime into tElapsedTime
   put tElapsedTime / kAnimationLength into tRatio
   if tRatio >= 1 then 
      -- end of the animation, so just move to the targetloc
      set the loc of me to sTargetLoc
      put empty into sAnimateMsgId
   else
      -- move along the intended path
      put item 1 of sStartLoc + (item 1 of sTargetLoc - item 1 of sStartLoc) * tRatio, \
      item 2 of sStartLoc + (item 2 of sTargetLoc - item 2 of sStartLoc) * tRatio into tCurrentLoc
      set the loc of me to tCurrentLoc
      -- continue the animation heartbeat
      send "AnimateMe" to me in kAnimateInterval milliseconds
      put the result into sAnimateMsgId
   end if
end AnimateMe
Click on the button and see it move along. The first time, just wait until the animation finishes in 2 seconds time; then click the button again to start the animation, and click it once more while it is still moving - you'll see the button change course to another destination.

What happens is that every 25 milliseconds, Revolution will send an event to your button, and it checks how far along it is in the animation, and then moves it to where it would have been on its track.
The advantage of this approach is that you're not surprised when computers get faster, and you can still receive 'keyDown', 'mouseUp' and other events while animating.

HTH,

Jan Schenkel.

Posted: Wed Jan 07, 2009 9:04 am
by Steelweaver52
Janschenkel wrote: Rather than using 'repeat' loops for your animations, you should look into 'send in time' constructs...
Jan, thank you very much! I'll study it.

---Tom Nally, New Orleans

Posted: Wed Jan 07, 2009 9:10 am
by Steelweaver52
Jan,

Just glancing at your code, I notice that most (if not all) variables have a single-character, lower-case prefix, such as "k", "s" or "t".

Is their a common convention that the experienced Rev programmers follow?

Rather than write a full response, feel free to point me to an existing forum entry or URL if you wish.

Thanks!

---Tom Nally

Posted: Wed Jan 07, 2009 10:06 am
by Janschenkel
Hi Tom,

There's an unofficial Scripting Style Guide at Fourth World's Runtime Revolution Embassy. The embassy is also an excellent place for finding links, and it contains an excellent explanation of the message path. In addition to that article, you may want to look at Dar Scott's Primer on Message Mechanics, which also goes into the 'send in time' construct.

HTH,

Jan Schenkel.

Posted: Thu Jan 22, 2009 2:21 pm
by Steelweaver52
Hi, Friends:

I wrote a tiny space shooter game as a practice exercise with Revolution. I'm giving it the unlikely name, "Space Shooter 01". Download it here.

http://nally.wikispaces.com/file/view/S ... v/54390032

It features a single level of play, and a typical game should last about a minute, perhaps less. A help card is provided. Spaceship control is by three keys: left arrow, right arrow and space bar.

Note the following game flaw. The player is ostensibly given 40 rockets to destroy 25 "aliens". However, one condition that ends the game is the firing of the 40th rocket. Essentially, then, you have 39 rockets to destroy all aliens, because firing the 40th rocket causes the game to end, even if that rocket would otherwise be on a path to kill the last alien.

[Edit at 8:11am central time in the US: I noticed that I coded nothing for the application's Quit button. Oh well...]

It looks like images embedded in these forum posts are disabled. If that's the case, you can see a screenshot of Space Shooter 01 here:

http://nally.wikispaces.com/file/view/S ... g/54372458

Thank you, Jan Schenkel.

---Tom Nally, New Orleans



Image

Posted: Thu Jan 22, 2009 4:20 pm
by Klaus
Hi Tom,

VERY, VERY nice lil game!

If that is just "a practice exercise with Revolution" I really wonder what to exspect from you after a bit more practising :-)


Best from germany

Klaus

Posted: Thu Jan 22, 2009 4:31 pm
by Steelweaver52
Klaus wrote: VERY, VERY nice lil game!
Thanks, Klaus. You are very kind.

---Tom Nally, New Orleans

Posted: Thu Jan 22, 2009 8:10 pm
by Mark
Well done, Steelweaver52!

Mark

Posted: Fri Jan 23, 2009 7:04 am
by Janschenkel
Very good job, Tom - and you only recently joined the Revolution! Just think of where you can go from here :-)

Jan Schenkel.

Posted: Fri Jan 23, 2009 4:39 pm
by Steelweaver52
Janschenkel wrote:Very good job, Tom...
Jan, thanks for your assistance!

Let the record show that I definitely had fun building the little game.

---Tom Nally, New Orleans