2DPhysics_Contacts.lua 1022 B

12345678910111213141516171819202122232425262728293031323334
  1. class "ImpactNoise" (EventHandler)
  2. function ImpactNoise:ImpactNoise()
  3. self.collisionSound = Sound("collision.wav")
  4. self:EventHandler()
  5. end
  6. function ImpactNoise:handleEvent(e)
  7. if e:getDispatcher() == screen then
  8. local pe = PhysicsScreenEvent(e)
  9. if e:getEventCode() == EVENT_NEW_SHAPE_COLLISION then
  10. if pe.impactStrength > 5 then
  11. self.collisionSound:Play()
  12. end
  13. end
  14. end
  15. end
  16. screen = PhysicsScreen(10, 60)
  17. shape = ScreenShape(SHAPE_RECT, 600,30)
  18. shape:setColor(0.0,0.0,0.0,1.0)
  19. shape:setPosition(640/2, 400)
  20. screen:addPhysicsChild(shape, ENTITY_RECT, true)
  21. for i=0,50 do
  22. shape = ScreenShape(SHAPE_RECT, 20,5)
  23. shape:setRotation(random(360))
  24. shape:setPosition(random(640), random(300))
  25. screen:addPhysicsChild(shape, ENTITY_RECT, false)
  26. end
  27. noiseMaker = ImpactNoise()
  28. screen:addEventListener(noiseMaker, EVENT_NEW_SHAPE_COLLISION)