Skip to content

Instantly share code, notes, and snippets.

@preaction
Created June 4, 2011 18:10
Show Gist options
  • Select an option

  • Save preaction/1008151 to your computer and use it in GitHub Desktop.

Select an option

Save preaction/1008151 to your computer and use it in GitHub Desktop.
import flash.utils.Timer;
import flash.events.TimerEvent;
// Flags for which keys are pressed, so the next tick moves the ball
var keys:Object = {
right: false,
left: false,
up: false,
down: false
};
// When a key is pressed, activate the flag
addEventListener( KeyboardEvent.KEY_DOWN, function (e:KeyboardEvent):void {
if ( e.keyCode == Keyboard.RIGHT ) {
keys["right"] = true;
}
// ... etc...
} );
// When a key is up, deactivate the flag
addEventListener( KeyboardEvent.KEY_UP, function (e:KeyboardEvent):void {
if ( e.keyCode == Keyboard.RIGHT ) {
keys["right"] = false;
}
} );
var gameLoop:Timer = new Timer(10); // Every 10 ms, or 0.01 seconds
gameLoop.addEventListener( TimerEvent.TIMER, function (e:TimerEvent):void {
// If keys are pressed, move the ball
if ( keys["right"] ) {
// Move ball to the right
}
else if ( keys["left"] ) {
// Move ball to the left
}
if ( keys["up"] ) {
// Move ball up
}
else if ( keys["down"] ) {
// Move ball down
}
// Check if ball hit a paddle
// Check if ball is outside the map
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment