alestane icon

Sticking bullets

alestane | PRO | 06/26/14 05:08:19 PM UTC | 0 ⭐ | 510 👁️ | Never ⏰ | []
Lua |

1.41 KB

|

None

|

0 👍

/

0 👎

local physics = require "physics"
physics.start()
physics.setGravity(0, 0)
 
local rock = display.newGroup(display.newCircle(0, 0, 30))
 
physics.addBody(rock, "kinematic", {radius = 30})
 
rock.x, rock.y = display.contentWidth, display.contentHeight * 0.25
rock:setLinearVelocity(-50, 15)
 
local function stick(event)
    local self, other = event.target, event.other
    self:removeEventListener('collision', stick)
    timer.performWithDelay(0,
        function()
            physics.removeBody(self)
            local x, y = self:localToContent(0, 0)
            other:insert(self)
            self.x, self.y = other:contentToLocal(x, y)
            timer.performWithDelay(5000, function(...) if self and self.removeSelf then self:removeSelf() end end)
        end
    )
end
 
local function spawn()
    local self = display.newCircle(display.contentCenterX, display.contentCenterY * 1.5, 6)
    self:setFillColor(0xFF, 0x00, 0x00)
    physics.addBody(self, 'dynamic', {radius = 5})
    self:setLinearVelocity(0, -150)
    self:addEventListener('collision', stick)
    local function monitor()
        local bounds = self and self.contentBounds
        if not bounds or bounds.xMin > display.contentHeight or bounds.xMax < 0 or bounds.yMax < 0 or bounds.yMin > display.contentHeight then
            if self and self.removeSelf then self:removeSelf() end
            Runtime:removeEventListener('enterFrame', monitor)
        end
    end
    Runtime:addEventListener('enterFrame', monitor)
end
 
timer.performWithDelay(500, spawn, 0)

Comments