How to decrease life when there is collision

Hi. I have this code:

[code]local physics = require(“physics”)
physics.start()
display.setStatusBar( display.HiddenStatusBar )
–physics.setDrawMode(“hybrid”)

local background = display.newImage(“background.jpeg”)
background.y = 210

local road = display.newImage(“road.jpg”,0,430)
local player = display.newImage(“player.png”)
player.x = display.contentWidth/2
player.y = 420
player.myName = “first crate”

local dreapta = display.newImage(“dreapta.png”)
dreapta.x = player.x +100
dreapta.y = 455

local stanga = display.newImage(“stanga.png”)
stanga.x = player.x - 100
stanga.y = 455

health = 164

local healthbar = display.newRect(143,9,health,16)
healthbar:setReferencePoint(display.TopLeftReferencePoint)
healthbar:setFillColor(38,130,224)

local health = display.newImage(“health.png”)
health:setReferencePoint(display.TopLeftReferencePoint)
health.x =135
health.y = 1

physics.addBody(road, {friction = 0.5})
road.bodyType = “static”
physics.addBody(player,{density=10.0,friction=0.2,bounce=0.3})
player.bodyType = “dynamic”

local function moveright (event)
player.x = player.x+5
end
local function moveleft (event)
player.x = player.x-5
end
local function wrapit(event)
if(player.x <32) then
player.x = 16
elseif player.x > 290 then
player.x = 305
end
end

local collisionBall = {}

–Function to spawn an object
function spawn()
local ball = display.newImage(“ball.png”)
ball.x = math.random(320)
ball.y = -100

physics.addBody(ball, {bounce = 0,density= 0.1, radius = 28})

transition.to(ball, {time=2000,y=600})
for j = 1 , #collisionBall do
collisionBall[j] = “second crate”
return ball
end
end

–Create a table to hold our spawns
local ballTable = {}

–Spawn two objects
local i = 1
local function spawnball ()
for i = 1,1 do
ballTable[i] = spawn()
end
for i = 1, #ballTable do
print(ballTable[i])
end
end

local function onLocalCollision(road,event)

if(event.phase == “ended”) then

print(“Collision”)

end
end
for j =1 ,#collisionBall do
collisionBall[j].myName = “second crate”
for j =1 ,#collisionBall do

collisionBall[j].collision = onLocalCollision
collisionBall[j]:addEventListener( “collision”, collisionBall[j] )
end
end
–Add Event Listeners
player.collision = onLocalCollision
player:addEventListener( “collision”, player )
Runtime:addEventListener(“enterFrame”, wrapit)
stanga:addEventListener(“touch”, moveleft)
dreapta:addEventListener(“touch”, moveright)

local spawner = timer.performWithDelay(500,spawnball,1000)[/code]
What should I edit to decrease the health rectangle everytime a collision is made? [import]uid: 178587 topic_id: 31491 reply_id: 331491[/import]

You can edit the width property of your lifeline (rect).

On hit: decrese width

When width reaches zero => die

Simple [import]uid: 177091 topic_id: 31491 reply_id: 125814[/import]

That’s what I want.
When a collision is made , health = health - 10 but the rectangle still has the same width.
Maybe because it isn’t updated? [import]uid: 178587 topic_id: 31491 reply_id: 125816[/import]

Use this in your OnHit function:

health.width = health.width - 10  

Make sure you declare health (rect) before changing its properties. [import]uid: 177091 topic_id: 31491 reply_id: 125820[/import]

Thanks . The healthbar is decreasing now but not how I want.
I want to decrease from right to left, not from both sides.
Here is a picture: http://i46.tinypic.com/1z6ukrc.png
Solved: A good guy helped me out on IRC
healthbar.width = healthbar.width - 20 ; healthbar.x = healthbar.x - 10 ; [import]uid: 178587 topic_id: 31491 reply_id: 125824[/import]

It looks like you are using the center reference point for the health bar. Try using the center left reference point. You’ll probably have to adjust the bar’s initial position though [import]uid: 135765 topic_id: 31491 reply_id: 125866[/import]

You can edit the width property of your lifeline (rect).

On hit: decrese width

When width reaches zero => die

Simple [import]uid: 177091 topic_id: 31491 reply_id: 125814[/import]

That’s what I want.
When a collision is made , health = health - 10 but the rectangle still has the same width.
Maybe because it isn’t updated? [import]uid: 178587 topic_id: 31491 reply_id: 125816[/import]

Use this in your OnHit function:

health.width = health.width - 10  

Make sure you declare health (rect) before changing its properties. [import]uid: 177091 topic_id: 31491 reply_id: 125820[/import]

Thanks . The healthbar is decreasing now but not how I want.
I want to decrease from right to left, not from both sides.
Here is a picture: http://i46.tinypic.com/1z6ukrc.png
Solved: A good guy helped me out on IRC
healthbar.width = healthbar.width - 20 ; healthbar.x = healthbar.x - 10 ; [import]uid: 178587 topic_id: 31491 reply_id: 125824[/import]

It looks like you are using the center reference point for the health bar. Try using the center left reference point. You’ll probably have to adjust the bar’s initial position though [import]uid: 135765 topic_id: 31491 reply_id: 125866[/import]