Keyboard Interaction with ActionScripting 3.0
Select the flash piece above and use your keyboard to control the ball. Enjoy the tuts.
// Copyright by CreativeReform
// All rights reserved
package{
//import statement for the package
import flash.display.Sprite;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class Keycode extends Sprite{
private var ball:Sprite;
public function Keycode(){
init();
}
private function init():void{
//create ball, add instance to stage and add event listener to capture key stroke
ball = new Sprite();
addChild(ball);
ball.graphics.beginFill(0x777777);
ball.graphics.drawCircle(50,50,20);
ball.graphics.endFill();
stage.addEventListener(KeyboardEvent.KEY_DOWN, eventKeyDown);
}
private function eventKeyDown(event:KeyboardEvent):void{
//trace(event.keyCode);
if(event.keyCode == 38){
//key up
ball.y -=10;
} else if (event.keyCode == 40){
//key down
ball.y +=10;
} else if (event.keyCode == 37){
//key left
ball.x -=10;
} else if (event.keyCode == 39){
//key down
ball.x +=10;
}
}
}
}