Keyboard Events

Posted by antirealism
Feb 23 2009

Here we go finally,

Only a small exercise due to most of the code is the same as the last few exercises, so in this One there was not much for me to try to remember :)

so first off in this we have to make the ” EventListener ” listen to the stage for an event, the reason for this is if we call up the instance name, like I did on the other codes, we would have to select the item before it would move, so if we make the whole stage active the listen will find the Object without us having to click on it.
Here we will be using Key_Down (press) and the Key_Up (release)
I have chosen to make the item jump as we press the button and on release the item will return to position.

Here is the code…

stage.addEventListener(KeyboardEvent.KEY_DOWN, jump);
stage.addEventListener(KeyboardEvent.KEY_UP, land);

//stage listener so we dont have to select Object

function jump(event:KeyboardEvent):void
{
Man_mc.y -= 100;
}
//jump up 100 pixels

function land(event:KeyboardEvent):void
{
Man_mc.y += 100;
}
/*man drop back to the original
place which is down 100 pixels*/

To test this flash movie please click in box first or click this link.
TESTMAN


I would like to add that if u hold down space bar the object (the man) will fly off screen. This is because the drop down is a listener, soon I will be learning how to make it return to its position and not run away.

Thank you for reading

Next it will be timer event…

Share and Enjoy:
  • Twitter
  • Facebook
  • Google Bookmarks
  • Print
  • del.icio.us
  • RSS
  • email
  • Digg
  • StumbleUpon
  • Live
  • Ping.fm
  • Slashdot
  • MySpace

One Response

  1. [...] off the stage when the key is held down, (You can see this problem in the last section I did called Keyboard events), the key press looped the upward motion and did not return it to its original position. So to [...]

Trackback URL for this entry