1

我通过创建一个新类(Fireball.as)并在类名后键入“extends 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();
}
4

2 回答 2

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);
}

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

于 2013-02-18T00:39:18.037 回答
0

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

FlxGroups 是比数组更好的数据结构例如,它们从 FlxObject 继承所有方法,因此您可以直接针对组检查冲突,它们也具有处理对象集合的特定方法,例如countDeadand countAlive(如果您使用alive属性)recyclegetFirstDead等等在。

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

于 2013-07-09T16:36:07.550 回答