// PACMAN "game" EXAMPLE package { import flash.display.*; // includes MovieClip class import flash.events.*; // includes KeyboardEvent class import flash.ui.*; // includes Keyboard class public class Wk5Pacman extends MovieClip { public function Wk5Pacman( ) { // Trace some of the Keyboard class event contants just to try them out // and see what their ASCII code values are // These could be removed and not affect the movie at all trace( "Left arrow code: " + Keyboard.LEFT ); trace( "Right arrow code: " + Keyboard.RIGHT ); trace( "Up arrow code: " + Keyboard.UP ); trace( "Down arrow code: " + Keyboard.DOWN ); trace( "Escape code: " + Keyboard.ESCAPE ); trace( "Delete code: " + Keyboard.DELETE ); trace( "Backspace code: " + Keyboard.BACKSPACE ); // Set the speed of Pacman pacman.speed = 30; // Add the listener for Keyboard events to the stage, not to the pacman object // This will listen for ANY key that is pressed stage.addEventListener( KeyboardEvent.KEY_DOWN, movePacman ); } // end constructor function // Handler function for the Arrow Keys to move pacman around the stage // Define only one handler function for all of the arrow keys // Use the event parameter, evt, to determine which key is pressed // If a key other than the arrow keys is pressed, this handler function \ // will be executed, but no action will be taken, since there is no "else" clause function movePacman( evt: KeyboardEvent ) { if( evt.keyCode == Keyboard.LEFT ) // if key is left arrow, code 37 { pacman.x = pacman.x - pacman.speed; // move pacman to the left by the value of speed pacman.rotation = -180; // and rotate pacman to face left } else if( evt.keyCode == Keyboard.UP ) // up arrow, code 38 { pacman.y = pacman.y - pacman.speed; pacman.rotation = -90; } else if( evt.keyCode == Keyboard.RIGHT ) // right arrow, code 39 { pacman.x = pacman.x + pacman.speed; pacman.rotation = 0; } else if( evt.keyCode == Keyboard.DOWN ) // down arrow, code 40 { pacman.y = pacman.y + pacman.speed; pacman.rotation = 90; } } // end movePacman function } // end class } // end package