【问题标题】:Why doesn't my custom FlxSprite appear on the stage?为什么我的自定义 FlxSprite 没有出现在舞台上?
【发布时间】:2013-02-02 09:12:00
【问题描述】:

我通过创建一个新类 (Fireball.as) 并在类名后键入“扩展 FlxSprite”,为火球创建了一个自定义 FlxSprite。在构造函数中,它调用了一个名为“shoot()”的函数,该函数将其指向目标并播放声音:

private function shoot():void {
    dx = target.x - x;
    dy = target.y - y;
    var norm:int = Math.sqrt(dx * dx + dy * dy);
    if (norm != 0) {
        dx *= (10 / norm);
        dy *= (10 / norm);
    }
    FlxG.play(soundShoot);
}


然后,我创建了一个数组来保存游戏状态下的所有火球(PlayState.as):

public var fire:Array = new Array();


然后在我的 input() 函数中,当有人按下鼠标按钮时,它会在数组中推送一个新的火球:

if (FlxG.mouse.justPressed()) {
    fire.push(new Fireball(x, y, FlxG.mouse.x, FlxG.mouse.y));
}


但是当我测试它时,它只播放声音,但没有出现火球。我猜火球在舞台外的某个地方,但我该如何解决呢?

如果有帮助,这里是 Fireball.as 的构造函数:

public function Fireball(x:Number, y:Number, tar_x:Number, tar_y:Number) {
    dx = 0;
    dy = 0;
    target = new FlxPoint(tar_x, tar_y);
    super(x, y, artFireball);
    shoot();
}

【问题讨论】:

    标签: actionscript-3 flixel


    【解决方案1】:

    我认为您必须将它们添加到舞台上。

    if (FlxG.mouse.justPressed()) {
        var fireball:Fireball = new Fireball(x, y, FlxG.mouse.x, FlxG.mouse.y);
        fire.push(fireball);
    
        // This line needs to be changed if you add the fireball to a group, 
        //  or if you add it to the stage from somewhere else
        add(fireball);
    }
    

    让我知道它是否对你有用。

    【讨论】:

    • 谢谢!我忘了将它添加到 Flixel。 Flixel 有自己的自定义添加函数add(),它几乎是addChild() 的Flixel 版本。非常感谢!
    • 你能把代码发给我吗,Flixel 把它添加到舞台的地方吗?声音可以在不增加舞台的情况下播放,所以。
    • 我改变了我之前的评论。谢谢!
    【解决方案2】:

    如前所述,问题是由于未将 FlxSprite 添加到 Flixel 显示树。但我也建议您使用FlxObject groups 而不是简单的数组。

    FlxGroup 是比数组更好的数据结构方式。例如,它们继承了 FlxObject 的所有方法,因此您可以直接针对组检查冲突,它们也具有处理对象集合的特定方法,例如 countDeadcountAlive(如果你让你alive 属性)、recyclegetFirstDead 等等。

    看看吧,你不会失望的。 :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多