Question

I have a top down zombie shooter thing going on and I have an odd way from an example to display a health bar. The code goes like so:

private function createLives():Void
{
    BaseLifeText = new FlxText(4, 28, 220, "Base Health: ", 12);
    BaseLifeText.color = 0xffFFFFFF;        
    guiGroup.add(BaseLifeText);

    for (i in 0...9) {
        var cur:Int = lifeGroup.length + 1;

               //Start Trick
        var colnum:Int = cur;
        var xPos:Float = (BaseLifeText.x + 96) + 14 * (colnum - 1);
               //End Trick 
        life = new FlxSprite(xPos, 34,"assets/BaseHealth.png");         
        lifeGroup.add(life);
    }
}

While this is haxe/haxeflixel, I've seen this trick one other time in as3, only done on the draw call. The above trick instead of just displaying one sprite, displays 9. Is there a name for this specific trick?

Part 2 Pertaining to the trick above, I'm also trying to add a collectable that will heal the Base. However, I'm only successful in adding it numerically. I'm doing this:

private function collectCoolant(player:FlxObject, cooler:Coolant):Void
{

    cooler.kill();

    var cur:Int = lifeGroup.length + 1;     
    lives ++;

    life.x += 14 * cur;     
    lifeGroup.length + 1;
}

It adds to the life of the the thing, but it doesn't do it graphically like when it's created. Using this system how can I restore health to the thing graphically?

Was it helpful?

Solution

  1. It is not a trick. It's trivial approach to problem, quite strange and also not working, as you can see.
  2. There is no way you can do it as you do it now. First of all, lifeGroup.length + 1; is a meaningless statement unless + is overloaded which is not the case. It just takes the length and adds 1 to it. So I assume you wanted to do something like lifeGroup.length+= 1; to increment length by 1, but unfortunately it won't work too. First of all, it won't compile, because length property is read only. And even if it did compile and changing group length were implemented in Flixel, it wouldn't have known what object to add to your group, so it would crash.

    My suggestion would be to create a proper health bar component and just work with it. Otherwise, you can keep writing bad code and copypaste your creation code for incrementing lives.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top