When checking the index on event.selfElement during collisions, the index is being reported incorrectly on the exited event.phase.
In the following example, when the complex body enters the ground, 2, 1 is printed to the console as “groundSensor” and then “playerBodyShape” enter the ground collider (this is correct).
However, when the complex body exits the ground (when jumping for example), -123, -123 is printed to the console (not correct).
I created a simple project so you can easily test this yourself, download it here: http://dl.dropbox.com/u/3302748/selfElementBug.zip
local physics = require "physics"
physics.start()
physics.setGravity(0, 20)
physics.setDrawMode( "hybrid" )
-- create player with "complex body"
local player = display.newImage( "player.png",100,100 )
local playerBodyShape = { -4,-10 , 4,-10 , 4,10 , -4,10 } -- index 1
local groundSensor = { -4,12 , 4,12 , 4,20 , -4,20 } -- index 2
physics.addBody( player, "dynamic",
{ density=3, friction=0, bounce=0, shape=playerBodyShape },
{ density=0, friction=0, bounce=0, shape=groundSensor, isSensor = true }
)
player.isFixedRotation = true
-- create ground
local ground = display.newRect(0, 300, 480, 20); ground:setFillColor(0,0,200)
physics.addBody( ground, "static", { density=3.0, friction=0.5, bounce=0 } )
-- jump button
local function touchMyRectangle(event)
local phase = event.phase
if phase == "began" then
player:applyLinearImpulse( 0, -5, player.x, player.y )
end
end
local myRectangle = display.newRect(0, 0, 480, 320); myRectangle:setFillColor(0,0,0,0)
myRectangle:addEventListener( "touch", touchMyRectangle )
-- player collision
local function OnPlayerCollision(self, event)
if event.phase == "began" then
print (event.selfElement) -- selfElement index # is correct
elseif event.phase == "ended" then
print (event.selfElement) -- selfElement index # is not correct
end
end
player.collision = OnPlayerCollision
player:addEventListener("collision", player)
Someone else also reported this, but since this appears to be a bug I’m adding it to the bug reports forum: http://developer.anscamobile.com/forum/2011/05/28/complex-body-not-recognized-ended-event
[import]uid: 48658 topic_id: 10731 reply_id: 310731[/import]