【问题标题】:Removing sprite assigned as Child to another sprite - Phaser将指定为子的精灵移除给另一个精灵 - Phaser
【发布时间】:2016-08-31 22:44:29
【问题描述】:

我已将一个精灵指定为另一个精灵的孩子。基本上,我正在尝试为法师装备他的法杖,并让法杖在他移动时跟随他。

Melee.handleInput = function (wizard) {
    if (wizard.state !== STATE.STANDING) {
        var staff = new Melee(game, 0, 0);
        staff.scale.set(.60, .60);
        // Tweak anchor position to correctly align over player
        staff.anchor.setTo(.07, -0.4);
        wizard.addChild(staff);
        wizard.body.velocity.y = 600;
    }

};

但是,我只希望在向导的 stateflying, falling, jumping or diving 时发生这种情况,因此是 if 语句。

这一切都很好。当按下向下箭头时,法杖会出现并跟随向导降落到地面。

现在我希望在巫师降落后工作人员离开。但我不确定如何访问员工精灵并将其杀死。部分问题可能是它已被分配为另一个精灵的孩子。如何访问其他精灵的孩子?

提前谢谢你。

【问题讨论】:

  • 我从未使用过 Phaser,但 Phaser.Sprite 对象的文档列出了您可能使用的许多属性和方法,例如,children、getChildAt、getChildIndex。他们还有一个forum,人们可能会更好地回答您的问题。

标签: javascript sprite phaser-framework


【解决方案1】:

最简单的方法是保存参考

wizard.staff = wizard.addChild(staff);
wizard.staff.kill()

但你也可以直接访问孩子

wizard.children.forEach(
  function(child){
    if(child instanceof Melee) child.kill();
  }
);

【讨论】: