Attach a lifebar to an element

Hi guys

Today, I want to “attach” a displayable element (a lifebar) to a physical element.

I’m currently developing a kind of “shot’em all” where the player got a tower at the bottom of the screen and has to shot enemies falling from the sky (so the top of the screen).

Now, I want to handle enemies’s hp and then attach them a lifebar moving with them so that this bar always stays at the head of the enemy.

The current vision is just pivot joint attached to the enemy and added to him manually but it rotates with the enemy if this one rotates too due to a player’s shooting i.e.

local options = F.genSizeOptions(35, 15)

local imageSheet = graphics.newImageSheet( img_path…“enemybarcontainer.png”, options )

dEnemy.bar = display.newImage( imageSheet, 1 )

dEnemy.bar.x = dEnemy.x

dEnemy.bar.y = dEnemy.y - 30

physics.addBody( dEnemy.bar, { density=0.2, friction=0.1, bounce=0.1, radius=30, isSensor = true} )

dEnemy.bar.bodyType = “dynamic”

if (physics ~= nil) then

physics.addBody( dEnemy, { density=10.1, friction=0.3, bounce=0.3} )

end

physics.newJoint( “pivot”, dEnemy.bar, dEnemy, dEnemy.x, dEnemy.y )

Any idea for it ? Thanks.

You could just do this in an enterFrame listener:

[lua]

dEnemy.bar.x = dEnemy.x

dEnemy.bar.y = dEnemy.y - 30

[/lua]

Sure I thought of it but I was afraid that it would cause some lags.

 

Is it a good practice for this kind of situation ?

I’ve used it before with no problems. 

You could just do this in an enterFrame listener:

[lua]

dEnemy.bar.x = dEnemy.x

dEnemy.bar.y = dEnemy.y - 30

[/lua]

Sure I thought of it but I was afraid that it would cause some lags.

 

Is it a good practice for this kind of situation ?

I’ve used it before with no problems.