Animated Sprite Collision

Hi guys, is there any sample code can show how to create collision between two animated sprites? Currently I can do it for the images but for the animated sprites, I cannot find any results in google. Let’s say, you have FirstObject and SecondObject, and collision occurs when FirstObject touches with SecondObject. How I can make 2 animated sprites collision? Thanks guys. 

-- First Object firstObjectImageSheet = graphics.newImageSheet("firstObject.png", { width = 80, height = 104, numFrames = 6 })     firstObject= display.newSprite(firstObjectImageSheet, { name="firstObject", start=1, count=6, time=1000, loopCount=0, loopDirection="bounce" }) -- Second Object secondObjectImageSheet = graphics.newImageSheet("secondObject.png", { width = 80, height = 104, numFrames = 6 })     secondObject= display.newSprite(secondObjectImageSheet, { name="secondObject", start=1, count=6, time=1000, loopCount=0, loopDirection="bounce" })

Adding a body and a collision listener works the same way for sprites as it does for all other display objects (newImage(), newRect(), newImageRect(), newCircle()).

Just  use the same example you have for images and you’ll be fine.

https://docs.coronalabs.com/daily/guide/physics/collisionDetection/index.html

I was trying with this and it is not working. :frowning:

-- Object collides function objectCollide(self, event)   if (event.phase == "began") then     print(self.name .. ": begins with " .. event.other.name)   elseif(event.phase == "ended") then     print(self.name .. ": ends with " .. event.other.name)       end end -- "scene:show()" function scene:show( event ) firstObject.collision = objectCollide firstObject:addEventListener("collision", firstObject) secondObject.collision = objectCollide secondObject:addEventListener("collision", secondObject) end

Hi @jumoun,

This may be an obvious question, but are you using physics and did you apply physics bodies to your sprites? Collisions only work with physical objects… adding a collision listener to a sprite without physics will not work. You could still build your own basic collision detection without using physics, but it requires a different approach (I can link you to a tutorial if you need it).

Best regards,

Brent

Yes, after I added the physics, it is working. On previously, I didn’t set the physics gravity to 0,0, that is why I didn’t see it. Thanks. :slight_smile:

Adding a body and a collision listener works the same way for sprites as it does for all other display objects (newImage(), newRect(), newImageRect(), newCircle()).

Just  use the same example you have for images and you’ll be fine.

https://docs.coronalabs.com/daily/guide/physics/collisionDetection/index.html

I was trying with this and it is not working. :frowning:

-- Object collides function objectCollide(self, event)   if (event.phase == "began") then     print(self.name .. ": begins with " .. event.other.name)   elseif(event.phase == "ended") then     print(self.name .. ": ends with " .. event.other.name)       end end -- "scene:show()" function scene:show( event ) firstObject.collision = objectCollide firstObject:addEventListener("collision", firstObject) secondObject.collision = objectCollide secondObject:addEventListener("collision", secondObject) end

Hi @jumoun,

This may be an obvious question, but are you using physics and did you apply physics bodies to your sprites? Collisions only work with physical objects… adding a collision listener to a sprite without physics will not work. You could still build your own basic collision detection without using physics, but it requires a different approach (I can link you to a tutorial if you need it).

Best regards,

Brent

Yes, after I added the physics, it is working. On previously, I didn’t set the physics gravity to 0,0, that is why I didn’t see it. Thanks. :slight_smile: