问题描述:

I am building a game in Corona SDK that involves two types of collisions with two different types of objects. There are 3 objects total in the game, a rocket ship, an asteroid, and a yellow sphere. When the asteroid hits the rocket ship, you lose one life. When a yellow sphere hits the rocket ship, you gain a point. For some reason both the asteroid and the yellow sphere say that they have multiple collisions with the rocket ship when they are only colliding once. I have re-evaluated my code multiple times and I cannot figure out what the problem is. Please help and if you need sample code I can post some.

网友答案:

Collisions have multiple phases. You have to test the phases. Collisions begin and end. There should be an event.phase that tells you what phase your in. There are also pre-collision events too.

网友答案:

You code must be with in any of these two phases,

local crate1 = display.newImage( "crate.png" )
physics.addBody( crate1, { density=3.0, friction=0.5, bounce=0.3 } )
crate1.myName = "first crate"

local crate2 = display.newImage( "crate.png" )
physics.addBody( crate2, { density=3.0, friction=0.5, bounce=0.3 } )
crate2.myName = "second crate"

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

crate1.collision = onLocalCollision
 crate1:addEventListener( "collision", crate1 )

crate2.collision = onLocalCollision
crate2:addEventListener( "collision", crate2 )

This will help you to avoid multiple collisions.

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

相关阅读:
Top