【问题标题】:actionscript 3 - deleting children from stage issueactionscript 3 - 从舞台问题中删除儿童
【发布时间】:2015-01-23 20:32:38
【问题描述】:

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

ai 的子弹存储在一个数组中,我想知道当 ai 本身从舞台上移除时,是否有办法移除由 ai 添加到舞台上的孩子。

这是处理 AI 被 PLAYER 子弹击中的代码。 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;
                }
            }
        }

【问题讨论】:

  • 您发布的代码显示了移除一个敌人和一颗子弹。但真正的问题是为什么当敌人被移除时子弹会冻结。敌人和它产生的子弹应该是独立的对象。请发布移动子弹的代码(并且在不执行时冻结它们)以及它在您的项目中的位置。

标签: actionscript-3 flash


【解决方案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);
    }
}

你的 Bullet 类:

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 类。这种分离将防止您遇到类似的问题,并且在未来更加灵活。

【讨论】:

  • 将 Main 传递给每个类是糟糕的设计。相反,通过从 Bullet 和 Enemy 类调度事件来传达子弹和敌人的移除以及子弹的创建。
  • 我不同意。将 Main 作为引用传递更好,原因如下:1)它在构造函数中声明了对 Main 的依赖,2)事件很慢(真的很慢),这对游戏很重要,3)事件冒泡显示列表正在使整个游戏更依赖于显示列表。更好的设计是将显示对象与游戏数据完全分开。
  • 另外,传递 Main 并不是我的观点或问题的答案,而是将游戏对象添加和删除到 Main 类的举动。引用 Main 可以通过其他几种方式完成,传递引用似乎与他目前所拥有的差别最小。
  • 感谢您的详细回复,我测试了removedfromstage 事件,它似乎只从ai 中移除1 个子弹,如果ai 有超过1 个子弹在空中它移除1 个并且其他冻结。还没有时间研究它,但是当我以后有更多时间时,我会按照你的建议尝试在主课上做子弹。再次感谢。
  • @Aaron 1) 依赖于 Main 是问题,而不是它如何获得参考 2) 过早优化是万恶之源 3) 我没有说任何关于冒泡的事情。你的回答是对的,这不是好的设计,这就是我想指出的。
猜你喜欢
  • 2014-10-03
  • 1970-01-01
  • 1970-01-01
  • 2011-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-10
相关资源
最近更新 更多