Hey so I’m looking for a way to remove a different image (heart) each time my object collides with another.
I tried using numerical variables to keep track of the state of the object then use if statements to do something based on the number:
[lua]local healthLeft = 3
onLocalCollision = function( event )
healthLeft = 3
end
easyScoring = function( event )
if (healthLeft == 2) then
crashMessage = display.newText(‘You crashed! D:’, 0, 0, “Helvetica”, 15)
transition.from( crashMessage, {time=2000, alpha=1})
makeExplosion()
display.remove(enemy)
enemy=nil
display.remove(lifebar)
elseif (healthLeft == 1) then
crashMessage = display.newText(‘You crashed! D:’, 0, 0, “Helvetica”, 15)
transition.from( crashMessage, {time=2000, alpha=1})
makeExplosion()
display.remove(enemy)
enemy=nil
display.remove(lifebar2)
elseif (healthLeft == 0) then
display.remove(enemy)
enemy=nil
display.remove(spaceship)
spaceship=nil
display.newText(‘You have died…’, 0, 0, “Helvetica”, 15)
end[/lua]
But for some reason my collision stopped working…
So I changed it (back) to something like this:
[lua]local enemyHit1 = false
local enemyHit2 = false
local enemyHit3 = false
local hitTable = { ‘enemyHit1’, ‘enemyHit2’, ‘enemyHit3’ }
onLocalCollision = function( event )
–enemyHit = true
for i=1,#hitTable do
–I’m not sure what to put here
end
end
if (enemyHit1 == true) then
enemyHit = false
crashMessage = display.newText(‘You crashed! D:’, 0, 0, “Helvetica”, 15)
transition.from( crashMessage, {time=2000, alpha=1})
makeExplosion()
display.remove(enemy)
enemy=nil
display.remove(lifebar)
lifebar=nil
elseif (enemyHit2 == true) then
enemyHit2 = false
crashMessage = display.newText(‘You crashed! D:’, 0, 0, “Helvetica”, 15)
transition.from( crashMessage, {time=2000, alpha=1})
makeExplosion()
display.remove(enemy)
enemy=nil
display.remove(lifebar2)
elseif (enemyHit3 == 0) then
enemyHit3 = false
display.newText(‘You have died…’, 0, 0, “Helvetica”, 15)
end[/lua]
(Which obviously doesn’t work)
To clarify, I want to remove one heart each time my object collides and cycle from enemyHit1 to enemyHit2 to enemyHit3 each time a collision occurs: