Multiple health bars

I want to show each player’s health above it’s head (above the sprite itself). I dynamically create players thus want to create bars dynamically aswell, but i can’t figure out a way to associate a health bar to a player.

I mean, i create a player for example…

players = {}

local character  = display.newSprite( myImageSheet , sequenceData )

character.health = 100

players[#players + 1] = character

Now i have my code for the health bar which is like this…

healthRect = display.newRect(0,0,100,10) healthRect:setFillColor(0,255,0) healthRect:setReferencePoint(display.TopLeftReferencePoint) if(player.health\>50)then healthRect:setFillColor(0, 255, 0) elseif(player.health\<=50 and player.health\>20)then healthRect:setFillColor(255, 255, 0) elseif(player.health\<=20)then healthRect:setFillColor(255, 0, 0) end healthRect.width = player.health healthRect.x = player.x healthRect.y = player.y - (player.height/2) - 20

If there was only one player i could use something like above but since there will be multiple players each created dynamically, how do i associate each health bar to the specific player?

Thanks Alot!

Just use references?

-- If you always want to find the health bar for this player character.healthbar = healthRect -- But this requires you know who "character" is at the time of making the bar. -- Since you know 'character' is also player[##] you could find him that way too. -- If you want to visually associate the bar (move the bar with the player) use displayGroups local characterGroup = display.newGroup() -- then specify it when making the character and healthBar, ie: display.newSprite( displayGroup, sheet, sequence) display.newRect( displayGroup, x,y,width,height)

Just use references?

-- If you always want to find the health bar for this player character.healthbar = healthRect -- But this requires you know who "character" is at the time of making the bar. -- Since you know 'character' is also player[##] you could find him that way too. -- If you want to visually associate the bar (move the bar with the player) use displayGroups local characterGroup = display.newGroup() -- then specify it when making the character and healthBar, ie: display.newSprite( displayGroup, sheet, sequence) display.newRect( displayGroup, x,y,width,height)

Old topic, but I’m messing with this right now and running into a problem. 

My health bar is above the player and both are sitting in a display group. But according to this in the docs:

http://docs.coronalabs.com/guide/physics/limitations/index.html

…that’s why O’m not able to check for collisions using the physics library. Because once I move the group that contains the player+health bar, collisions won’t work any longer.

Has anyone come up with a decent workaround for this?

I could use a Runtime event to reposition the health bars over the heads of all the the players every frame, but that just seems whack. :slight_smile:

 Jay

Just don’t group them and move them at the same time. Or you can check the player position in a timed loop and move the health bar to the player position (the lag should be quite small).

Alternately use hidden dummy player object and add the physics object to that. You still have to move it separately from the group though, but it might look smoother.

Thanks for the suggestions. I was so happy with the “group solution” because it made things compact and easy.

It just didn’t work. :slight_smile:

I’ve gone ahead and decided to move both parts at the same time. Not elegant, but at least it works.

Thanks!

 Jay

Old topic, but I’m messing with this right now and running into a problem. 

My health bar is above the player and both are sitting in a display group. But according to this in the docs:

http://docs.coronalabs.com/guide/physics/limitations/index.html

…that’s why O’m not able to check for collisions using the physics library. Because once I move the group that contains the player+health bar, collisions won’t work any longer.

Has anyone come up with a decent workaround for this?

I could use a Runtime event to reposition the health bars over the heads of all the the players every frame, but that just seems whack. :slight_smile:

 Jay

Just don’t group them and move them at the same time. Or you can check the player position in a timed loop and move the health bar to the player position (the lag should be quite small).

Alternately use hidden dummy player object and add the physics object to that. You still have to move it separately from the group though, but it might look smoother.

Thanks for the suggestions. I was so happy with the “group solution” because it made things compact and easy.

It just didn’t work. :slight_smile:

I’ve gone ahead and decided to move both parts at the same time. Not elegant, but at least it works.

Thanks!

 Jay