when two objects spawned from tables collide-desparate for help by Friday!

Hey all, I have been working on this for two days and I am trying to finish it up by Friday. Any help would be great!
so I have two objects, I want to spawn 10 of each like this.
ball_table = {}
hole table= {}
ball = display.newCircle(100,100,50)
hole = display.newCircle(100,100,200)
hole.myname = “hole”
for i = 1, 10 do
ball_table[i] = ball
end
for i = 1, 10 do
hole_table[i] = hole
end

I am basically trying to make it so that when a ball_table[i] collides with a hole_table[i] the ball that collided with corresponding hole, stops it’s velocity. But I can’t seem to get the collisions down for both items from both tables. If that makes sense. I have something like this.

for i = 1, #ball_table do
function ball_collision (self,event)
if event.phase == “began” then
if event.other.myname then
print(hole_table[i])
end
end
end
end
ball_table[i].collision = head_grave_collision
ball_table[i]]:addEventListener(“collision”,ball_table[i])
end

so, it print’s the ball and the number of the hole it collides with, well the table index, I still can’t get it to print a number. But I can’t get it make the ball_table[i] stop at the correct hole. If anyone could help know what I am missing or point me in the direction of a tutorial that might help, that would be really really great! thanks in advance.
Sincerely,
SM [import]uid: 79586 topic_id: 31838 reply_id: 331838[/import]

do you get any error messages? [import]uid: 119483 topic_id: 31838 reply_id: 127055[/import]

only when I close the scene, but that is a story board thing I am trying to work out separately. [import]uid: 79586 topic_id: 31838 reply_id: 127056[/import]

Ok, here is what i did. I did a similar thing. The function Attack() spawns out a new bullet, the function spawnEnemy() spawns enemy. Then i inserted a collision for the enemy.

 local bullets = {}  
 function Attack()   
 local b = newBullet(-350, 5, Character[Red])  
 bullets[b] = b  
 BulletLoadedInChamber = false  
 transition.to(bullets[b], {time=(150), delta=(false), x=(Character[Green].x), y=(Character[Green].y), onStart=Reload} )  
 LocalGroup:insert(bullets[b])   
  
 end   
  
 local enemies = {}  
 function spawnEnemy()  
 local enmy = newEnemy((math.random(5)\*100), -10, (math.random(5)\*100), 10)  
 enemies[enmy] = enmy   
 enemies[enmy]:addEventListener("collision", enemies[enmy])  
 enemies[enmy].collision = checkEnemyCollision  
 LocalGroup:insert(enemies[enmy])  
 end  
  
 -- when enemy has collision  
 local function checkEnemyCollision(self, event)  
 print ("Collision detected")  
  
 -- play collion audio  
 local rndaudio = math.random(3)  
 if( rndaudio == 1) then  
 audio.play(GlassBreakAudio1)  
 elseif (rndaudio == 2 ) then  
 audio.play(GlassBreakAudio2)  
 else  
 audio.play(GlassBreakAudio3)  
 end  
  
 --  
 if(event.phase == "began") then   
 print(self.Type)  
 print(event.other.Type)  
 end   
  
  
 -- reduce enemies health and delete bullet  
 local function damageCollidedParties()  
 if(self.Type == "enemy") then  
 self:reduceHealth(event.other.Damage) -- enemy looses health, the amount of health lost is the amount of damage the bullet has  
 event.other:Delete() -- delete the bullet   
 end  
 if deleteTimer then  
 timer.cancel(deleteTimer)  
 deleteTimer = nil  
 end  
 end  
  
 local deleteTimer = timer.performWithDelay(10, damageCollidedParties)  
 end  
  

Im new to the Forums so im still learning how to post comments [import]uid: 119483 topic_id: 31838 reply_id: 127057[/import]

if you look at damageCollidedParties(), you’ll see i called

self:reduceHealth(event.other.Damage)  

reduceHealth is a method inside the enemy class. In there i guess you can then do whatever you want. I know if this is helpful but this is what i understood from your question.
[import]uid: 119483 topic_id: 31838 reply_id: 127058[/import]

Hi ugobyte, thanks, that does help. I am also not very good at posting code or explaining myself sometimes. But thanks, I will play with this and see if I can make it work.
SM [import]uid: 79586 topic_id: 31838 reply_id: 127101[/import]

do you get any error messages? [import]uid: 119483 topic_id: 31838 reply_id: 127055[/import]

only when I close the scene, but that is a story board thing I am trying to work out separately. [import]uid: 79586 topic_id: 31838 reply_id: 127056[/import]

Ok, here is what i did. I did a similar thing. The function Attack() spawns out a new bullet, the function spawnEnemy() spawns enemy. Then i inserted a collision for the enemy.

 local bullets = {}  
 function Attack()   
 local b = newBullet(-350, 5, Character[Red])  
 bullets[b] = b  
 BulletLoadedInChamber = false  
 transition.to(bullets[b], {time=(150), delta=(false), x=(Character[Green].x), y=(Character[Green].y), onStart=Reload} )  
 LocalGroup:insert(bullets[b])   
  
 end   
  
 local enemies = {}  
 function spawnEnemy()  
 local enmy = newEnemy((math.random(5)\*100), -10, (math.random(5)\*100), 10)  
 enemies[enmy] = enmy   
 enemies[enmy]:addEventListener("collision", enemies[enmy])  
 enemies[enmy].collision = checkEnemyCollision  
 LocalGroup:insert(enemies[enmy])  
 end  
  
 -- when enemy has collision  
 local function checkEnemyCollision(self, event)  
 print ("Collision detected")  
  
 -- play collion audio  
 local rndaudio = math.random(3)  
 if( rndaudio == 1) then  
 audio.play(GlassBreakAudio1)  
 elseif (rndaudio == 2 ) then  
 audio.play(GlassBreakAudio2)  
 else  
 audio.play(GlassBreakAudio3)  
 end  
  
 --  
 if(event.phase == "began") then   
 print(self.Type)  
 print(event.other.Type)  
 end   
  
  
 -- reduce enemies health and delete bullet  
 local function damageCollidedParties()  
 if(self.Type == "enemy") then  
 self:reduceHealth(event.other.Damage) -- enemy looses health, the amount of health lost is the amount of damage the bullet has  
 event.other:Delete() -- delete the bullet   
 end  
 if deleteTimer then  
 timer.cancel(deleteTimer)  
 deleteTimer = nil  
 end  
 end  
  
 local deleteTimer = timer.performWithDelay(10, damageCollidedParties)  
 end  
  

Im new to the Forums so im still learning how to post comments [import]uid: 119483 topic_id: 31838 reply_id: 127057[/import]

if you look at damageCollidedParties(), you’ll see i called

self:reduceHealth(event.other.Damage)  

reduceHealth is a method inside the enemy class. In there i guess you can then do whatever you want. I know if this is helpful but this is what i understood from your question.
[import]uid: 119483 topic_id: 31838 reply_id: 127058[/import]

Hi ugobyte, thanks, that does help. I am also not very good at posting code or explaining myself sometimes. But thanks, I will play with this and see if I can make it work.
SM [import]uid: 79586 topic_id: 31838 reply_id: 127101[/import]