1

I created a custom FlxSprite for a fireball by creating a new class(Fireball.as) and typing "extends FlxSprite" after the class name. In the constructor, it calls a function called "shoot()", which directs it to the target and plays a sound:

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


Then, I created an array to hold all the fireballs in the game state(PlayState.as):

public var fire:Array = new Array();


Then in my input() function, when someone pushes the mouse button, it pushes a new Fireball in the array:

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


But when I test it, it only plays the sound, but the fireball doesn't show up. I'm guessing that the fireball is somewhere offstage, but how do I fix it?

If it helps, here's the constructor for 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();
}
dizzydj7
  • 43
  • 4

2 Answers2

1

I think you must add them to stage.

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

Let me know if it worked for you.

IQAndreas
  • 8,060
  • 8
  • 39
  • 74
sybear
  • 7,837
  • 1
  • 22
  • 38
  • 1
    Thanks! I forgot to add it to Flixel. Flixel has it's own custom function for adding called `add()`, which is pretty much the Flixel version of `addChild()`. Thank you so much! – dizzydj7 Feb 18 '13 at 00:41
  • Can you post me the code, where Flixel adds it to stage? Sounds may be played without adding to stage, so. – sybear Feb 18 '13 at 00:42
  • I changed my comment from before. Thank you! – dizzydj7 Feb 18 '13 at 00:45
0

As stated before, the problem was due to not adding the FlxSprite to the Flixel display tree. But also I would advise you to use FlxObject groups instead of a simple Array.

FlxGroups are way better data structure than arrays. For instance they inherit all the methods from a FlxObject, so you can check for collisions directly against the group, also they have specific methods to handle the collection of objects, such as countDead and countAlive (if you make you of the alive attribute), recycle, getFirstDead and so on.

Check it out, you won't be disappointed. :)

Alvaro Cavalcanti
  • 2,938
  • 4
  • 21
  • 31