Need help with basic collisions!

Hello, I do not completly understand the way you can detect collisions in Corona.

I have read this- http://developer.coronalabs.com/content/game-edition-collision-detection, but still have no clue.

local function onCollision( event ) if ( event.phase == "began" ) then print( "began: " .. event.object1.myName .. " & " .. event.object2.myName ) elseif ( event.phase == "ended" ) then print( "ended: " .. event.object1.myName .. " & " .. event.object2.myName ) end end Runtime:addEventListener( "collision", onCollision )

Here is an exaple code.

On line 1, what does ( event ) mean/do?

Also, I am making a game that when “player” collides with “prize”, score = score + 1.

How would I create a listener that is only activated when two specified objects (that I delcrare) casue a result.

Any basic tutorials are welcome!

–Regards

Hi @maxniebylski,

In Corona, “events” entail many, many things. They are specific to the functionality you’re “listening” for in the function, and different events return different information to you.

In this case, “event” is basically the collision event. It will give you the following info:

http://docs.coronalabs.com/api/event/collision/index.html

To control which objects register a collision with other objects, you need to set up “collision filters”. There is a tutorial on that here:

http://forums.coronalabs.com/topic/2128-collision-filters-helper-chart/

Hope this helps!

Brent Sorrentino

Hello, I do not completly understand the way you can detect collisions in Corona.

I have read this- http://developer.coronalabs.com/content/game-edition-collision-detection, but still have no clue.

local function onCollision( event ) if ( event.phase == “began” ) then print( "began: " … event.object1.myName … " & " … event.object2.myName ) elseif ( event.phase == “ended” ) then print( "ended: " … event.object1.myName … " & " … event.object2.myName ) end end Runtime:addEventListener( “collision”, onCollision )

Here is an exaple code.

On line 1, what does ( event ) mean/do?

Also, I am making a game that when “player” collides with “prize”, score = score + 1.

How would I create a listener that is only activated when two specified objects (that I delcrare) casue a result.

Any basic tutorials are welcome!

–Regards

 For your player variable set:

player.myName = “player”

and for your prize:

prize.myName = “prize”

then you can use the collision listener as follows:

local function onCollision( event ) if (event.phase == "began") then if (event.object1.myName == "player") and (event.object2.myName == "prize") then score = score + 1 end end end  Runtime:addEventListener( "collision", onCollision )  

Thanks for the help! I now understand the basics of collisions.

I have used your code for my  Game but is not working…

I’m inside a Storyboard i’ have put all in the create scene function. even the listener. is it right ?

are you sure the  if- and -if- then statement works ?

Hi @maxniebylski,

In Corona, “events” entail many, many things. They are specific to the functionality you’re “listening” for in the function, and different events return different information to you.

In this case, “event” is basically the collision event. It will give you the following info:

http://docs.coronalabs.com/api/event/collision/index.html

To control which objects register a collision with other objects, you need to set up “collision filters”. There is a tutorial on that here:

http://forums.coronalabs.com/topic/2128-collision-filters-helper-chart/

Hope this helps!

Brent Sorrentino

Hello, I do not completly understand the way you can detect collisions in Corona.

I have read this- http://developer.coronalabs.com/content/game-edition-collision-detection, but still have no clue.

local function onCollision( event ) if ( event.phase == “began” ) then print( "began: " … event.object1.myName … " & " … event.object2.myName ) elseif ( event.phase == “ended” ) then print( "ended: " … event.object1.myName … " & " … event.object2.myName ) end end Runtime:addEventListener( “collision”, onCollision )

Here is an exaple code.

On line 1, what does ( event ) mean/do?

Also, I am making a game that when “player” collides with “prize”, score = score + 1.

How would I create a listener that is only activated when two specified objects (that I delcrare) casue a result.

Any basic tutorials are welcome!

–Regards

 For your player variable set:

player.myName = “player”

and for your prize:

prize.myName = “prize”

then you can use the collision listener as follows:

local function onCollision( event ) if (event.phase == "began") then if (event.object1.myName == "player") and (event.object2.myName == "prize") then score = score + 1 end end end  Runtime:addEventListener( "collision", onCollision )  

Thanks for the help! I now understand the basics of collisions.

I have used your code for my  Game but is not working…

I’m inside a Storyboard i’ have put all in the create scene function. even the listener. is it right ?

are you sure the  if- and -if- then statement works ?