[Resolved] I don't know how to add collision detection

In my game, I have an object called sprite1 which automatically move from left to right of the screen, using a for cicle, until it reach another object called sprite2. Now I would like to print a “game over” message when “sprite1” collide with “sprite2”. Any suggestion? Thanks :wink: [import]uid: 111398 topic_id: 31978 reply_id: 331978[/import]

Hi,
Collision detection is part of the physics engine, and is fully detailed in the documentation. If you aren’t using (or don’t want to use) the physics engine, then you’ll need to detect collision mathematically. Corona ambassador Rob Miracle has written a guide to it here: http://omnigeek.robmiracle.com/2011/12/14/collision-detection-without-physics/

Hope this helps!
Brent Sorrentino [import]uid: 9747 topic_id: 31978 reply_id: 127540[/import]

Thanks for your help! :slight_smile: I’ve used the following code:

local objects = {}  
  
for i=1, 20, do  
  
 objects[i] = spawnEnemy() -- spawnEnemy creates on display objects and returns it.  
  
end  
  
   
  
local player = display.newImageRect("avatar.png", 64, 64) -- create me.  
  
local function testCollisions()  
  
 for i=1, #objects do  
  
 if hasCollided(objects[i], player) then  
  
 -- do what you need to do if they hit each other  
  
 end  
  
 end  
  
end  
  
   
  
Runtime:addEventListener("enterFrame", testCollisions)  

but corona return this error:

main.lua:3: unexpected symbol near "do"   

How can I fix it? Thanks! [import]uid: 111398 topic_id: 31978 reply_id: 127587[/import]

Remove the comma after 20 on line 3.
[import]uid: 33275 topic_id: 31978 reply_id: 127594[/import]

You can add collision listeners on your objects

[lua]function sprite1:collision (event)
if (event.other.name == ‘sprite2’) then
print(“game over”)
end
end
sprite1:addEventListener(“collision”,sprite1) [import]uid: 75779 topic_id: 31978 reply_id: 127595[/import]

Thanks you both! It works! :wink: [import]uid: 111398 topic_id: 31978 reply_id: 127684[/import]

Hi,
Collision detection is part of the physics engine, and is fully detailed in the documentation. If you aren’t using (or don’t want to use) the physics engine, then you’ll need to detect collision mathematically. Corona ambassador Rob Miracle has written a guide to it here: http://omnigeek.robmiracle.com/2011/12/14/collision-detection-without-physics/

Hope this helps!
Brent Sorrentino [import]uid: 9747 topic_id: 31978 reply_id: 127540[/import]

Thanks for your help! :slight_smile: I’ve used the following code:

local objects = {}  
  
for i=1, 20, do  
  
 objects[i] = spawnEnemy() -- spawnEnemy creates on display objects and returns it.  
  
end  
  
   
  
local player = display.newImageRect("avatar.png", 64, 64) -- create me.  
  
local function testCollisions()  
  
 for i=1, #objects do  
  
 if hasCollided(objects[i], player) then  
  
 -- do what you need to do if they hit each other  
  
 end  
  
 end  
  
end  
  
   
  
Runtime:addEventListener("enterFrame", testCollisions)  

but corona return this error:

main.lua:3: unexpected symbol near "do"   

How can I fix it? Thanks! [import]uid: 111398 topic_id: 31978 reply_id: 127587[/import]

Remove the comma after 20 on line 3.
[import]uid: 33275 topic_id: 31978 reply_id: 127594[/import]

You can add collision listeners on your objects

[lua]function sprite1:collision (event)
if (event.other.name == ‘sprite2’) then
print(“game over”)
end
end
sprite1:addEventListener(“collision”,sprite1) [import]uid: 75779 topic_id: 31978 reply_id: 127595[/import]

Thanks you both! It works! :wink: [import]uid: 111398 topic_id: 31978 reply_id: 127684[/import]