-1

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?

xhunterko
  • 49
  • 7

1 Answers1

2
  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.

Community
  • 1
  • 1
stroncium
  • 1,430
  • 9
  • 8
  • Darn. I was just wondering since I've seen it in a few places. Thanks. – xhunterko Jun 05 '14 at 13:37
  • Talking about lifebar component implementation, I guess iw would basically be composed of the same code you use now, just wrap it into a class and make some settabl variables like total and curret and update entities when those variables are set. You could also make the instance constuctor to take sprite(of 1 bar part) and dimensions(like all yours 96, 14, 24) as arguments and it will be pretty enough for the most of your games and definitely for this one. – stroncium Jun 06 '14 at 11:16