Advertisement
alestane

Single-listener collision

Mar 15th, 2013
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function createPara(copter)
  2.     local randX = math.random(100, 200)
  3.     if copter.x ~= nil then
  4.         if tonumber(copter.x) > 50 and tonumber(copter.x) < 470 then
  5.             if copter.isAlive then
  6.                 local paraChuter = display.newSprite( getSheet( obj.paraChuter.paraGlide), getSequenceData.paraData )
  7.                 paraChuter.x = copter.x-10
  8.                 paraChuter.y = copter.y+5
  9.                 paraChuter:play()
  10.                 paraChuter.name = "paraChuter"
  11.                 physics.addBody(paraChuter, "dynamic", physicsData:get("paraglider"))
  12.                 paraChuter.isFixedRotation = true
  13.                 paraChuter.linearDamping = 5
  14.             end
  15.         end                            
  16.     end
  17. end
  18.  
  19. local function onLocalPreCollision( event )
  20.     if not event.target.Skipped or event.target.Skipped == event.other then
  21.             event.contact.isEnabled = false
  22.         event.target.Skipped = event.other
  23.     end
  24. end
  25.  
  26. function onCollision(self, event)
  27.    
  28.     ---- if bullet hits parachuter
  29.     if event.phase == "began" and (self.name == "bullet" and event.other.name == "paraChuter") then
  30.        
  31.         event.other:setSequence( "paraGlideHs" )
  32.         event.other.name = "paraGlideHs"
  33.  
  34.         event.other:addEventListener( "preCollision", onLocalPreCollision )
  35.  
  36.         event.other.isFixedRotation = true
  37.         event.other.linearDamping = 5
  38.        
  39.         event.other:play()
  40.        
  41.         display.remove(self)
  42.         self = nil
  43.      end
  44. end
  45.  
  46. function setUpCatchPlatform()
  47.  
  48.     local platform = display.newRect( 0, 0, display.contentWidth * 4, 0)
  49.     platform.myName = "platform1"
  50.     platform.x =  (display.contentWidth / 2)
  51.     platform.y = (display.contentHeight / 2) + 40
  52.     physics.addBody(platform, "static", collisionTable.firstParaPlatformProp)
  53.  
  54.     platform.collision = onCatchPlatformCollision
  55.     platform:addEventListener( "collision", platform )
  56. end
  57.  
  58. function setUpCatchPlatform2()
  59.     local platform2 = display.newRect( 0, 0, display.contentWidth * 4, 0)
  60.     platform2.myName = "platform2"
  61.     platform2.x =  (display.contentWidth / 2)
  62.     platform2.y = (display.contentHeight / 2) + 150
  63.     physics.addBody(platform2, "static", collisionTable.secondParaPlatformProp)
  64.  
  65.     platform2.collision = onCatchPlatformCollision2
  66.     platform2:addEventListener( "collision", platform2 )
  67. end
  68.  
  69. function onCatchPlatformCollision(self, event)
  70.     if event.phase == "began" and event.other.name == "paraChuter" then
  71.        
  72.         if event.other ~= nil then
  73.             event.other:setSequence( "paraLand" )
  74.             event.other.name = "paraLander"
  75.            
  76.             local doCollision = function()
  77.                 physics.removeBody(event.other)
  78.                 physics.addBody(event.other, "dynamic", physicsData:get("paralander"))
  79.                 event.other:play()
  80.             end
  81.             local collisionTimer = timer.performWithDelay( 1, doCollision, 1 )
  82.         end
  83.    
  84.     end
  85. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement