Advertisement
alestane

Sticking bullets

Jun 26th, 2014
367
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.41 KB | None | 0 0
  1. local physics = require "physics"
  2. physics.start()
  3. physics.setGravity(0, 0)
  4.  
  5. local rock = display.newGroup(display.newCircle(0, 0, 30))
  6.  
  7. physics.addBody(rock, "kinematic", {radius = 30})
  8.  
  9. rock.x, rock.y = display.contentWidth, display.contentHeight * 0.25
  10. rock:setLinearVelocity(-50, 15)
  11.  
  12. local function stick(event)
  13.     local self, other = event.target, event.other
  14.     self:removeEventListener('collision', stick)
  15.     timer.performWithDelay(0,
  16.         function()
  17.             physics.removeBody(self)
  18.             local x, y = self:localToContent(0, 0)
  19.             other:insert(self)
  20.             self.x, self.y = other:contentToLocal(x, y)
  21.             timer.performWithDelay(5000, function(...) if self and self.removeSelf then self:removeSelf() end end)
  22.         end
  23.     )
  24. end
  25.  
  26. local function spawn()
  27.     local self = display.newCircle(display.contentCenterX, display.contentCenterY * 1.5, 6)
  28.     self:setFillColor(0xFF, 0x00, 0x00)
  29.     physics.addBody(self, 'dynamic', {radius = 5})
  30.     self:setLinearVelocity(0, -150)
  31.     self:addEventListener('collision', stick)
  32.     local function monitor()
  33.         local bounds = self and self.contentBounds
  34.         if not bounds or bounds.xMin > display.contentHeight or bounds.xMax < 0 or bounds.yMax < 0 or bounds.yMin > display.contentHeight then
  35.             if self and self.removeSelf then self:removeSelf() end
  36.             Runtime:removeEventListener('enterFrame', monitor)
  37.         end
  38.     end
  39.     Runtime:addEventListener('enterFrame', monitor)
  40. end
  41.  
  42. timer.performWithDelay(500, spawn, 0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement