创造敌人和碰撞检测

时间:2013-12-11 16:56:56

标签: actionscript-3

我正试图制作一个射击游戏,其中敌人随机掉落,玩家将向它射击。但是我遇到了制造敌人的麻烦,当导弹与敌人发生碰撞时,hitTestObject无效。子弹和敌人就不会消失。谁能帮我这个?敌人根本没有下降。它只是一直在同一个地方弹出。这是一个家庭作业,很快就会到期。如果我不能做到这一点,我就无法继续。真的很绝望。

enemy.as文件:

package{
    import flash.display.MovieClip;
    import flash.events.Event;

    public class Enemy extends MovieClip{
        var Enemy_One;



        public function Enemy(){
            addEventListener(Event.ADDED_TO_STAGE,onEnemyAdded);
        }

        private function onEnemyAdded (event:Event) {
            Enemy_One = MovieClip(this);

            addEventListener.(Event.ENTER_FRAME, enemyLoop);
        }

        private function enemyLoop (event:Event) {

            for (var i:int = 0;i<Enemy_One.myMissile.numChildren; i++) {
                var MissileTarget:MovieClip = Enemy_One.myMissile.getChildAt(i);
                if (hitTestObject(MissileTarget)){
                    Enemy_One.myMissile.getChildAt(i).removeListener();
                    Enemy_One.myMissile.removeChild(MissileTarget);


                    removeEventListener.(Event.ENTER_FRAME, enemyLoop);
                    Enemy_One.removeChild(this);
                }

            this.y+= 2;
            }
        }

        public function removeListeners():void{
            removeEventListener.(Event.ENTER_FRAME, enemyLoop);
        }

    }

missile as.file:

package{
    import flash.display.MovieClip;
    import flash.events.Event;

    public class Missile extends MovieClip{
        public var speed:int = 30;
        var radianAngle:Number;
        var xVelocity:Number;
        var yVelocity:Number;

        public function Missile(){
             addEventListener(Event.ADDED_TO_STAGE,onMissileAdded);
         }

        private function onMissileAdded (event:Event):void {
            // execute these when Missile is added to stage
            radianAngle = this.rotation*(Math.PI/180);
            yVelocity = Math.sin(radianAngle)*speed;
            xVelocity = Math.cos(radianAngle)*speed;
            addEventListener(Event.ENTER_FRAME, update);
            }

        private function update (event:Event):void {
            // update Missile every frame
            // Missile moves itself on the stage and remove itself when it is off the stage
            if (this.x<0 || this.x>stage.stageWidth || this.y<0 || this.y>stage.stageHeight) {
                removeEventListener(Event.ENTER_FRAME, update);
                parent.removeChild(this);

            }
            this.x+= xVelocity;
            this.y+= yVelocity;
        }

        public function removeListeners ():void {
            removeEventListener(Event.ENTER_FRAME, update);
        }
    }

主要时间表为:

//Shooting Missile Setup//


stage.addEventListener(MouseEvent.CLICK, doShoot);
function doShoot (e:MouseEvent):void {
    var radianAngle = Math.atan2(mouseY-myShooter.y,mouseX-myShooter.x);
    var myMissile:MovieClip = new Missile();
    myMissile.x = myShooter.x;
    myMissile.y = myShooter.y;
    myMissile.rotation = radianAngle*180/Math.PI;
    addChild(myMissile);
    myShooter.gotoAndPlay("blast");

}

//**Refer to external as. file (Missile)**
//Shooting Missile Setup//


//Enemy Setup//

var myMissile:MovieClip = new Missile();
var enemyTimer:Timer = new Timer (1000);
enemyTimer.addEventListener(TimerEvent.TIMER ,onStart);
enemyTimer.start();

function onStart (e:Event){

    var myEnemy1:MovieClip = new Enemy1();
    myEnemy1.y+=200;
    myEnemy1.x = Math.random()*600;
    addChild(myEnemy1);

}

}

0 个答案:

没有答案