package  {
	
	 import flash.display.MovieClip; 

        import flash.events.TimerEvent; 

        import flash.utils.Timer; 

        import flash.utils.getTimer; 

        import flash.events.Event; 

         

        public class Airplane extends MovieClip { 

                private var dx:Number; // 飞机的速度、方向 

                private var lastTime:int; // 控制移动的Timer 

                //左右移动 

                public function Airplane(side:String, speed:Number, altitude:Number) { 

                        if (side == "left") { 

                                this.x = -50; // start to the left 

                                dx = speed; // fly left to right 

                                this.scaleX = -1; // reverse 

                        } else if (side == "right") { 

                                this.x = 600; // start to the right 

                                dx = -speed; // fly right to left 

                                this.scaleX = 1; // not reverse 

                        } 

                        this.y = altitude; // vertical position 

                         

                        this.gotoAndStop(Math.floor(Math.random()*5+1)); 

                        // set up animation 

                        addEventListener(Event.ENTER_FRAME,movePlane); 

                        lastTime = getTimer(); 

                } 

                //检测边界 

                public function movePlane(event:Event) { 

                        // get time passed 

                        var timePassed:int = getTimer()-lastTime; 

                        lastTime += timePassed; 

                         

                        // move plane 

                        this.x += dx*timePassed/1000; 

                         

                        // check to see if off screen 

                        if ((dx < 0) && (x < -50)) { 

                                deletePlane(); 

                        } else if ((dx > 0) && (x > 600)) { 

                                deletePlane(); 

                        } 

                         

                } 

                 

                // 检测碰撞，子弹击中飞机跳转到帧标签"explode"演示爆炸动画。 

                public function planeHit() { 

                        removeEventListener(Event.ENTER_FRAME,movePlane); 

                        MovieClip(parent).removePlane(this); 

                        gotoAndPlay("explode"); 

                } 

                 

                // 移除舞台上的飞机和事件 

                public function deletePlane() { 

                        removeEventListener(Event.ENTER_FRAME,movePlane); 

                        MovieClip(parent).removePlane(this); 

                        parent.removeChild(this); 

                } 

          
  } 


	
}
