动作3 - 从舞台问题中删除儿童

时间:2015-01-23 20:32:38

标签: actionscript-3 flash

所以我正在创建一个游戏,其中ai射击玩家。我遇到的这个问题是当AI发射子弹并在子弹被摧毁之前被杀死时,由于AI被从舞台上移除,子弹在半空中冻结。这是一段显示我的意思的视频:http://i.gyazo.com/a13a43467ae8c6c3d8b1c988b7010bc2.mp4

ai的子弹存储在一个数组中,我想知道是否有一种方法可以移除ai本身从舞台上移除后由ai添加到舞台上的子节点。

这是处理被PLAYER子弹击中的AI的代码。 ai的子弹以相同的方式存储,但存储在另一个类/文件中。

        for (var i = _bullets.length - 1; i >= 0; i--) {
            for (var j = enemies.length - 1; j >= 0; j--) {
                if (_bullets[i].hitTestObject(enemies[j])) {
                    stage.removeChild(enemies[j]); //removes the ai 
                    stage.removeChild(_bullets[i]); //removes the players bullet
                    _bullets.splice(i, 1); 
                    enemies.splice(j, 1);
                    return;
                }
            }
        }

1 个答案:

答案 0 :(得分:1)

你可以做你所描述的,但我不推荐它。要做到这一点,你可以听Event.REMOVED_FROM_STAGE并进行某种清理:

// in your AI class
addEventListener(Event.REMOVED_FROM_STAGE, removedFromStage);
function removedFromStage(e:Event):void {
    // remove all the bullets
    for each(var bullet:Bullet in _bullets){
        stage.removeChild(bullet);
    }
    _bullets.length = 0;
}

然而,这种方法并不现实(子弹将在飞行途中消失,而不是像你期望的那样继续沿着它们前进)并且它不是一个非常好的设计。

更好的方法是将所有项目符号存储在主游戏课程中。 AI负责"解雇"一个子弹,但在那之后,主要类处理子弹旅行,碰撞等。如果你像这样分开你的逻辑,你就不会遇到更多问题,比如你遇到的问题。

有很多方法可以实现这样的设计,但这是一个我已经多次使用过的简单例子:

您的主要游戏类:

public class Main {

    // a list of all enemies in the game
    private var enemies:Vector.<Enemy> = new <Enemy>[];

    // a list of all bullets in the game
    private var bullets:Vector.<Bullet> = new <Bullet>[];

    // adds a new enemy to the game
    public function addEnemy():void {
        var enemy:Enemy = new Enemy(this); // pass a reference to this Main class
        enemies.push(enemy);
        addChild(enemy);
    }

    // removes an enemy from the game
    public function removeEnemy(enemy:Enemy):void {
        enemies.splice(enemies.indexOf(enemy), 1);
        removeChild(enemy);
    }

    // adds a new bullet to the game
    public function addBullet():void {
        var bullet:Bullet = new Bullet(this); // pass a reference to this Main class
        bullets.push(bullet);
        addChild(bullet);
    }

    // removes a bullet from the game
    public function removeBullet(bullet:Bullet):void {
        bullets.splice(bullets.indexOf(bullet), 1);
        removeChild(bullet);
    }
}

你的敌人类:

public class Enemy {

    // a reference to the Main class
    public var main:Main;

    public function Enemy(main:Main){
        // store the reference to the Main class
        this.main = main;
    }

    // to shoot a bullet, call the Main class
    public function shoot():void {
        main.addBullet();
    }

    // to kill the enemy, call the Main class
    public function die():void {
        main.removeEnemy(this);
    }
}

你的子弹课程:

public class Bullet {

    // a reference to the Main class
    public var main:Main;

    public function Bullet(main:Main){
        // store the reference to the Main class
        this.main = main;
    }

    // to remove the bullet when it hits something, call the Main class
    public function collide():void {
        main.removeBullet(this);
    }
}

如您所见,Main类负责添加和删除敌人和子弹,代码中的其他各个地方只需回调Main类。这种分离可以防止您遇到的问题,并在未来更加灵活。

相关问题