Chapter 21 Example 11
♥0 |
Line 40 |
Modified 2010-01-28 08:43:47 |
MIT License
archived:2017-03-10 21:19:42
ActionScript3 source code
/**
* Copyright actionscriptbible ( http://wonderfl.net/user/actionscriptbible )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/ci45
*/
package {
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
[SWF(backgroundColor="0x000000", frameRate="4")]
public class ch21ex11 extends Sprite {
protected var hero:Hero;
protected var keys:Array;
protected const MAX_KEY:int = 128;
public function ch21ex11() {
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKey);
stage.addEventListener(KeyboardEvent.KEY_UP, onKey);
keys = new Array(MAX_KEY);
hero = new Hero();
addChild(hero);
hero.x = stage.stageWidth/2;
hero.y = stage.stageHeight/2;
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
protected function onKey(event:KeyboardEvent):void {
if (event.keyCode >= MAX_KEY) return;
keys[event.keyCode] = (event.type == KeyboardEvent.KEY_DOWN);
}
protected function onEnterFrame(event:Event):void {
if (keys[Keyboard.UP]) hero.y -= hero.height;
if (keys[Keyboard.DOWN]) hero.y += hero.height;
if (keys[Keyboard.LEFT]) hero.x -= hero.width;
if (keys[Keyboard.RIGHT]) hero.x += hero.width;
}
}
}
import flash.display.Shape;
class Hero extends Shape {
public function Hero() {
graphics.beginFill(0x10c010);
graphics.drawRect(0, 0, 12, 30);
graphics.endFill();
}
}