cocos2d/cocos2d-x — pull request #5057 by edwardzhou (2014-01-14 11:34:48+00:00)
Title: refactor PhysicsContact inherits from EventCustom; simplify emitting col...

...lision events

a bit addition, for LUA binding, I added PhysicsContact::cast to cast correct type from EventCustom. Because, tolua.cast won't return the right data, and it will crash the app.
eg.
========================================================
local function collisionCallback(event)
print("Custom event received, ", event)
print('getEventName => ', event:getEventName())
local contact = tolua.cast(event, 'PhysicsContact')
print('shapeA => ', contact:getShapeA()) --- app will crash here.
print('A tag => ', contact:getShapeA():getBody():getNode():getTag())
print('B tag => ', contact:getShapeB():getBody():getNode():getTag())
end

local listener1 = cc.EventListenerCustom:create("PhysicsContactEvent", collisionCallback)
self._listener1 = listener1
local eventDispatcher = self:getEventDispatcher()
eventDispatcher:addEventListenerWithFixedPriority(listener1, 1)
========================================================

but in this way is ok
========================================================
local function collisionCallback(event)
print("Custom event received, ", event)
print('getEventName => ', event:getEventName())
local contact = cc.PhysicsContact:cast(event) -- let PhysicsContact handle the type
print('shapeA => ', contact:getShapeA())
print('A tag => ', contact:getShapeA():getBody():getNode():getTag())
print('B tag => ', contact:getShapeB():getBody():getNode():getTag())
end

local listener1 = cc.EventListenerCustom:create("PhysicsContactEvent", collisionCallback)
self._listener1 = listener1
local eventDispatcher = self:getEventDispatcher()
eventDispatcher:addEventListenerWithFixedPriority(listener1, 1)
========================================================