0

所以在我的游戏中,我使用了一系列敌人来跟踪它们并更新它们等等 - 当玩家撞到敌人时,它的游戏然而,但是如果玩家射击击中敌人的子弹,则敌人被删除. 那么这应该是发生了什么但是当子弹它的敌人它删除它但由于某种原因该对象仍在更新并且仍然可以撞到玩家?

我该如何阻止这个?

这是我在激光类中用于检查碰撞的代码

private function loop(e:Event) : void
{
    //move bullet up
    y -= bulletSpeed;

    if (y < 0)
    {
        removeSelf();
    }

    for (var i:int = 0; i < AvoiderGame.enemyArray.length; i++)
    {
        if (hitTestObject(AvoiderGame.enemyArray[i]))
        {
            AvoiderGame.enemyArray[i].takeHit();
            removeSelf();
        }
    }
}

这是我在敌人类中的所有代码。

package 
{

import flash.display.MovieClip;
import flash.display.Stage;

public class Enemy extends MovieClip
{
    private var stageRef:Stage;

    public function Enemy(startX:Number, startY:Number, stageRef:Stage)
    {
        this.stageRef = stageRef;
        x = startX;
        y = startY;
    }       
    public function moveDown():void
    {
        y = y + (1 + (10 - 1) * Math.random());
        switch(Math.round(1 + (2 - 1) * Math.random()))
        {
            case 1: x = x + (1 + (10 - 1) * Math.random());
                    break;
            case 2: x = x - (1 + (10 - 1) * Math.random());
                    break;
        };
    }

    public function StayOnScreen():void
    {
        if  (x <= 0)
        {
            x = 0;
        }
        else if (x >= stageRef.stageWidth)
        {
            x = stageRef.stageWidth;
        }
        else
        {
            x = x;
        }
    }

    private function removeSelf() : void
    {
        if (stageRef.contains(this))
        {
            this.parent.removeChild(this);
            delete AvoiderGame.enemyArray[this];
        }

    }

    public function takeHit() : void
    {
        removeSelf();
    }
    }
}

这是我在主游戏中的所有代码。

package
{
import flash.display.MovieClip;
import flash.utils.Timer;
import flash.events.TimerEvent;
import com.freeactionscript.CollisionTest;
import flash.display.Stage;
import flashx.textLayout.formats.BackgroundColor;

public class AvoiderGame extends MovieClip
{
    public static var enemyArray:Array;
    public var enemy:Enemy
    public var Background:gameBackground;

    public var avatar:Avatar;
    public var gameTimer:Timer;

    private var _collisionTest:CollisionTest;
    private var numStars:int = 80;

    private var fireTimer:Timer; //causes delay between fires
    private var canFire:Boolean = true; //can you fire a laser

    public function AvoiderGame()
    {
        Background = new gameBackground();
        addChild(Background);

        enemyArray = new Array();
        var enemy = new Enemy(Math.round(1 + (500 - 1) * Math.random()), - 2, stage);
        enemyArray.push(enemy);
        addChild(enemy);

        avatar = new Avatar(stage);
        addChild(avatar);

        avatar.x = stage.stageWidth / 2;
        avatar.y = stage.stageHeight / 2;

        for (var i:int = 0; i < numStars; i++)
        {
            stage.addChildAt(new Star(stage), 1);
        }

        _collisionTest = new CollisionTest();

        gameTimer = new Timer(25);
        gameTimer.addEventListener(TimerEvent.TIMER, onTick);
        gameTimer.start();

        fireTimer = new Timer(300, 1);
        fireTimer.addEventListener(TimerEvent.TIMER, fireTimerHandler, false, 0, true);
        fireTimer.start();

    }

    public function onTick(timerEvent:TimerEvent):void 
    {
        if (Math.random() < 0.1)
        {
            var enemy = new Enemy(Math.round(1 + (500 - 1) * Math.random()), - 28, stage);
            enemyArray.push(enemy);
            addChild(enemy);
        }

        avatar.UpdateAvatar(canFire);
        if (canFire == true)
        {
            canFire = false;
            fireTimer.start();
        }
        avatar.StayOnScreen();

        for each (var enemy:Enemy in enemyArray)
        {
            enemy.moveDown();
            enemy.StayOnScreen();
            if (_collisionTest.complex(enemy, avatar)) 
            {
                gameTimer.stop();
            }
        }
    }
    private function fireTimerHandler(e:TimerEvent) : void
    {
        //timer ran, we can fire again.
        canFire = true;
    }
}

我知道我可能在使用 removeSelf() 函数的敌人类中出错了,但我将如何解决这个问题?

如果你不想要我的意思是 - 在我的网站上有一个这个游戏的可玩示例,使用空间射击会摧毁敌人,但它仍然会在游戏中?

http://dynamite-andy.co.uk/projects/free-time/as3-avoider-game/

4

1 回答 1

0

问题似乎与removeSelf()方法中的这一行有关:

delete AvoiderGame.enemyArray[this];

要从中删除某些内容,Array您必须使用修改数组而不是关键字的Array 类方法。delete

[编辑] 在这种情况下,您可以使用splice()从数组中删除敌人:

var enemyIndex:int = this.parent.getChildIndex(this);
this.parent.removeChild(this);
AvoiderGame.enemyArray.splice(enemyIndex, 1);
于 2012-11-18T17:36:19.147 回答