Click on Stage and use Keyboard Arrows
to Move the Disc Below!
On this Switch Statement HD tutorial, Avraham creates a Flying Saucer on stage and using one single event listener on the code he moves the disc using the Keyboard arrow keys and the switch statement. This flash tutorial has the X FIles Theme as background music.
DOWNLOAD THIS TUTORIAL TO YOUR HARD DRIVE
Here is the source code:
// Stage Keyboard Listener and Switch Example
// by Avraham Saltoun
// solutions at say-web dot com
// FREE TO USE COPY AND DISTRIBUTE
stage.addEventListener(KeyboardEvent.KEY_DOWN,MoveSaucer);
//This will set the compiler
//waiting for a key stroke, and then
// execute the MoveSaucer function
function MoveSaucer (event:KeyboardEvent):void
// the void states that the function returns no value
// but receives the value of the key stroke
{
switch (event.keyCode){
case Keyboard.RIGHT:
FlyingSaucer_mc.x = FlyingSaucer_mc.x+5;
break;
// x is the coordinate for the horizontal position
// break will exit the switch and wait for
// next key stroke.
case Keyboard.LEFT:
FlyingSaucer_mc.x -=5;
// “FlyingSaucer_mc.x = FlyingSaucer_mc.x-5″
// is the same as
// “FlyingSaucer_mc.x -=5;”
break;
case Keyboard.UP:
FlyingSaucer_mc.y -=5;
break;
case Keyboard.DOWN:
FlyingSaucer_mc.y +=5;
break;
//Thanks for Watching!
//!!!!!!!!!!!!!!!!!!
}
}



































Tue, Aug 18, 2009
ActionScript