|
|
@@ -1,24 +1,3 @@
|
|
|
-class "Detector" (EventHandler)
|
|
|
-
|
|
|
-function Detector:Detector(shape)
|
|
|
- self.shape = shape
|
|
|
- EventHandler.EventHandler(self)
|
|
|
-end
|
|
|
-
|
|
|
-function Detector:handleEvent(e)
|
|
|
- if e:getDispatcher() == screen then
|
|
|
- local pe = PhysicsScreenEvent(e)
|
|
|
- if e:getEventCode() == PhysicsScreenEvent.EVENT_NEW_SHAPE_COLLISION then
|
|
|
- if pe:getFirstEntity() == self.shape or pe:getSecondEntity() == self.shape then
|
|
|
- pe:getFirstEntity():setColor(1.0, 0.0, 0.0, 1.0)
|
|
|
- pe:getSecondEntity():setColor(1.0, 0.0, 0.0, 1.0)
|
|
|
- end
|
|
|
- elseif e:getEventCode() == PhysicsScreenEvent.EVENT_END_SHAPE_COLLISION then
|
|
|
- pe:getFirstEntity():setColor(1.0, 1.0, 1.0, 1.0)
|
|
|
- pe:getSecondEntity():setColor(1.0, 1.0, 1.0, 1.0)
|
|
|
- end
|
|
|
- end
|
|
|
-end
|
|
|
|
|
|
screen = PhysicsScreen(10, 60)
|
|
|
|
|
|
@@ -32,9 +11,22 @@ for i=0,50 do
|
|
|
screen:addCollisionChild(shape, PhysicsScreenEntity.ENTITY_RECT)
|
|
|
end
|
|
|
|
|
|
-detector = Detector(checkShape)
|
|
|
-screen:addEventListener(detector, PhysicsScreenEvent.EVENT_NEW_SHAPE_COLLISION)
|
|
|
-screen:addEventListener(detector, PhysicsScreenEvent.EVENT_END_SHAPE_COLLISION)
|
|
|
+function onNewCollision(t, event)
|
|
|
+ physicsEvent = safe_cast(event, PhysicsScreenEvent)
|
|
|
+ if same_c_class(physicsEvent:getFirstEntity(),checkShape) or same_c_class(physicsEvent:getSecondEntity(),checkShape) then
|
|
|
+ physicsEvent:getFirstEntity():setColor(1.0, 0.0, 0.0, 1.0)
|
|
|
+ physicsEvent:getSecondEntity():setColor(1.0, 0.0, 0.0, 1.0)
|
|
|
+ end
|
|
|
+end
|
|
|
+
|
|
|
+function onEndCollision(t, event)
|
|
|
+ physicsEvent = safe_cast(event, PhysicsScreenEvent)
|
|
|
+ physicsEvent:getFirstEntity():setColor(1.0, 1.0, 1.0, 1.0)
|
|
|
+ physicsEvent:getSecondEntity():setColor(1.0, 1.0, 1.0, 1.0)
|
|
|
+end
|
|
|
+
|
|
|
+screen:addEventListener(nil, onNewCollision, PhysicsScreenEvent.EVENT_NEW_SHAPE_COLLISION)
|
|
|
+screen:addEventListener(nil, onEndCollision, PhysicsScreenEvent.EVENT_END_SHAPE_COLLISION)
|
|
|
|
|
|
function onMouseMove(x,y)
|
|
|
checkShape:setPosition(x,y)
|