Collisions and Timers

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]

You need to spawn them with unique names.

do;

local goodDude = {}

then spawn as

goodDude[#goodDude]

rather than just named them all goodDude.

Peach :slight_smile: [import]uid: 52491 topic_id: 20836 reply_id: 82141[/import]

Thanks for answering my question. I’m still a little confused, though.

When I type in goodDude[#goodDude] it asks me to put a ‘=’ after it.

Is it supposed to look like this?

local goodDude = {}  
  
goodDude[#goodDude] = display.newImageRect( "ghost1.png", 20, 50 )  
  

or this?

local goodDude = {}  
goodDude = display.newImageRect( "ghost1.png", 20, 50 )  
goodDude[#goodDude] = goodDude  

My apologies if this is already posted somewhere but I’m having trouble finding anything about this specifically. [import]uid: 45667 topic_id: 20836 reply_id: 82160[/import]

It should look like the first bit of code.

The bridge example; CoronaSDK > SampleCode > Physics > Bridge has a good example of this when spawning the links for the bridge. It does that in a for loop but the idea is the same.

[import]uid: 52491 topic_id: 20836 reply_id: 82179[/import]

Awesome. Thanks. I looked it over and it seems like this will solve my problems. [import]uid: 45667 topic_id: 20836 reply_id: 82616[/import]

No worries; it’s a very handy method to learn when creating multiple objects that need unique names.

Peach :slight_smile: [import]uid: 52491 topic_id: 20836 reply_id: 82647[/import]