I am trying to have a timer start once a collision happens between two units and have them fight until one of them dies. My problem is, the units are part of a group and all have the same name. When they collide everything goes fine, but once the timer starts and calls a different function outside of the collision function, and one of the units in a group dies, it removes a random unit in the group instead of the one that has been fighting.
My question is, once the collision has begun, how do I make sure the program knows which unit of the group to remove? Here is my code. I may be doing this totally wrong, but I just can’t seem to get it to work.
local goodDudes = {}
local badDudes = {}
local function spawnGoodDude()
local goodDude = display.newImageRect( "ghost1.png", 20, 50 )
goodDude.health = 100
goodDude.myName = "goodDude"
goodDude.myTransition = transition.to(goodDude, {time=10000, x=(goodDude.x + 500 ) , onComplete=repeatGoodDudeTransition})
goodDudes[#goodDudes+1] = goodDude
end
local function spawnBadDude()
local badDude = display.newImageRect( "ghost2.png", 20, 50 )
badDude.health = 50
badDude.myName = "badDude"
badDude.myTransition = transition.to(badSDude, {time=10000, x=(badDude.x - 500 ) , onComplete=repeatBadDudeTransition})
badDudes[#badDudes+1] = badDude
end
local function onGoodDudeBadDudeCollision( self, event )
if ( event.phase == "began" ) and (event.other.myName == "badDude") then
if self.myTransition then
transition.cancel(self.myTransition)
end
goodDudeDamageBadDude()
repeatGoodDudeDamageBadDude( self )
end
end
local function repeatGoodDudeDamageBadDude()
goodDude.badDudeCollisionTimer = timer.performWithDelay(1500, goodDudeDamageBadDude,0)
end
local function goodDudeDamageBadDude()
badDude.health = badDude.health - 31
if (badDude.health \< 1 ) then
killBadDude()
timer.cancel(goodDude.badDudeCollisionTimer)
goodDude.myTransition = transition.to(goodDude, {time=2500, x=goodDude.x + 500, onComplete=repeatGoodDudeTransition})
end
end
local function killBadDude(badDude)
badDude:playClip("die")
timer.performWithDelay(700, nilBadDude, 1)
local function nilBadDude(badDude)
if badDude ~= nil then
badDude:removeSelf()
badDude =nil
end
end
end
[import]uid: 45667 topic_id: 20836 reply_id: 320836[/import]
[import]uid: 52491 topic_id: 20836 reply_id: 82141[/import]