/*文档类*/ package { import flash.display.Sprite; import flash.events.KeyboardEvent; import flash.events.Event; import flash.ui.Keyboard; public class Main extends Sprite { private var flag:Boolean = true; private var bulletArr:Array = new Array ; private var bullet:Bullet; private var time:int; public function Main() { stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDownHandler); stage.addEventListener(KeyboardEvent.KEY_UP,keyUpHandler); addEventListener(Event.ENTER_FRAME,enterFrameHandler); } private function keyDownHandler(e:KeyboardEvent):void { if (e.keyCode == Keyboard.SPACE) { if(time==0) { fire(); } time++; //flag = false; if (time % 5 == 0) { fire(); } } else if (e.keyCode == Keyboard.A) { mc.x -= 5; } else if (e.keyCode == Keyboard.D) { mc.x += 5; } } private function keyUpHandler(e:KeyboardEvent):void { if (e.keyCode == Keyboard.SPACE) { //flag = true; time = 0; } } private function enterFrameHandler(e:Event):void { for (var i:int=0; i<bulletArr.length; i++) { if (bulletArr[i].y < 0) { removeChild(bulletArr[i]); bulletArr.splice(i,1); } } } private function fire():void { bullet = new Bullet(10,10,0XFF0000); bullet.x = mc.x; bullet.y = mc.y; addChild(bullet); bulletArr.push(bullet); } } }
/*子弹类*/ package { import flash.display.Sprite; import flash.events.Event; public class Bullet extends Sprite { private var _Number; private var _height:Number; private var color:uint; private var bullet:Bullet; public function Bullet(_Number=10,_height:Number=10,color:uint=0XFF0000) { this._width = _width; this._height = _height; this.color = color; drawBullet(); addEventListener(Event.ENTER_FRAME,enterFrameHandler,false,0,true); } private function drawBullet():void { graphics.beginFill(color); graphics.drawRect(0,0,_width,_height); graphics.endFill(); } private function enterFrameHandler(e:Event):void { if (this.y >-10) { this.y -= 5; } } } }