2DPhysics_CollisionOnly.lua 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. class "Detector" (EventHandler)
  2. function Detector:Detector(shape)
  3. self.shape = shape
  4. EventHandler.EventHandler(self)
  5. end
  6. function Detector:handleEvent(e)
  7. if e:getDispatcher() == screen then
  8. local pe = PhysicsScreenEvent(e)
  9. if e:getEventCode() == PhysicsScreenEvent.EVENT_NEW_SHAPE_COLLISION then
  10. if pe:getFirstEntity() == self.shape or pe:getSecondEntity() == self.shape then
  11. pe:getFirstEntity():setColor(1.0, 0.0, 0.0, 1.0)
  12. pe:getSecondEntity():setColor(1.0, 0.0, 0.0, 1.0)
  13. end
  14. elseif e:getEventCode() == PhysicsScreenEvent.EVENT_END_SHAPE_COLLISION then
  15. pe:getFirstEntity():setColor(1.0, 1.0, 1.0, 1.0)
  16. pe:getSecondEntity():setColor(1.0, 1.0, 1.0, 1.0)
  17. end
  18. end
  19. end
  20. screen = PhysicsScreen(10, 60)
  21. checkShape = ScreenShape(ScreenShape.SHAPE_RECT, 90,10)
  22. screen:addCollisionChild(checkShape, PhysicsScreenEntity.ENTITY_RECT)
  23. for i=0,50 do
  24. shape = ScreenShape(ScreenShape.SHAPE_RECT, 30,15)
  25. shape:setRotation(random(360))
  26. shape:setPosition(random(640), random(480))
  27. screen:addCollisionChild(shape, PhysicsScreenEntity.ENTITY_RECT)
  28. end
  29. detector = Detector(checkShape)
  30. screen:addEventListener(detector, PhysicsScreenEvent.EVENT_NEW_SHAPE_COLLISION)
  31. screen:addEventListener(detector, PhysicsScreenEvent.EVENT_END_SHAPE_COLLISION)
  32. function onMouseMove(x,y)
  33. checkShape:setPosition(x,y)
  34. end
  35. function Update(e)
  36. checkShape:setRotation(checkShape:getRotation() + (e*100))
  37. end