Hey guys I was wondering how to make it so when a certain object collides with another certain object then it calls a function. I am also using collision filters. I need “sled” to collide with “flagFinal”.
Thanks Tyler Jacobson
Hey guys I was wondering how to make it so when a certain object collides with another certain object then it calls a function. I am also using collision filters. I need “sled” to collide with “flagFinal”.
Thanks Tyler Jacobson
Can you show us what you have so far?
Rob
Hi Tyler,
A couple of places to start;
Make sure you understand what types of bodies you have - there are dynamic, static and kinematic body types, and every collision needs to have at least one of the bodies as dynamic. https://docs.coronalabs.com/api/type/Body/bodyType.html
The next thing is to remember that you can add your own properties to objects. Adding a ‘name’ property is useful for checking what objects are involved in a collision. So for example, you might have
sled = display.newImage("sled.png") sled.name = "sled"
and do the same for the flag, but obviously setting the .name property to “flagFinal” (or something similar).
Then you look at the collision process; https://docs.coronalabs.com/daily/guide/physics/collisionDetection/index.html
First, add a collision listener to either one of the objects (not both), which calls a function of your choosing - I’ve gone with the self-explanatory ‘sledCollision’ as the function to be called;
sled:addEventListener("collision",sledCollision)
And then the final part is to check whether the other object involved in the collision is the flagFinal, and only process the code if it is. (You also want to limit it to event.phase = “began”, otherwise the function will call constantly while the sled and flagFinal are colliding.) The key here is that the ‘event’ which calls the function will have properties, one of which is ‘other’ - this represents the object the sled collided with, and so you can simply check if that other object has the right .name property.
function sledCollision(event) if event.phase == "began" then if event.other.name == "flagFinal" then \<your code here\> end end end
Hope this helps!
Cheers,
Simon
Dixon Court Entertainment
Thanks Simon It worked!
Can you show us what you have so far?
Rob
Hi Tyler,
A couple of places to start;
Make sure you understand what types of bodies you have - there are dynamic, static and kinematic body types, and every collision needs to have at least one of the bodies as dynamic. https://docs.coronalabs.com/api/type/Body/bodyType.html
The next thing is to remember that you can add your own properties to objects. Adding a ‘name’ property is useful for checking what objects are involved in a collision. So for example, you might have
sled = display.newImage("sled.png") sled.name = "sled"
and do the same for the flag, but obviously setting the .name property to “flagFinal” (or something similar).
Then you look at the collision process; https://docs.coronalabs.com/daily/guide/physics/collisionDetection/index.html
First, add a collision listener to either one of the objects (not both), which calls a function of your choosing - I’ve gone with the self-explanatory ‘sledCollision’ as the function to be called;
sled:addEventListener("collision",sledCollision)
And then the final part is to check whether the other object involved in the collision is the flagFinal, and only process the code if it is. (You also want to limit it to event.phase = “began”, otherwise the function will call constantly while the sled and flagFinal are colliding.) The key here is that the ‘event’ which calls the function will have properties, one of which is ‘other’ - this represents the object the sled collided with, and so you can simply check if that other object has the right .name property.
function sledCollision(event) if event.phase == "began" then if event.other.name == "flagFinal" then \<your code here\> end end end
Hope this helps!
Cheers,
Simon
Dixon Court Entertainment
Thanks Simon It worked!