It looks like you're new here. If you want to get involved, click one of these buttons!
require "box2d" game = gideros.class(Sprite) function game:init() self.world= b2.World.new(0,0.1,true) self:addEventListener(Event.ENTER_FRAME, self.onEnterFrame, self) self:addEventListener("exitBegin", self.onExitBegin, self) self.layers={} self.layers[1]=Sprite.new() self.layers[2]=Sprite.new() self.layers[3]=Sprite.new() self.layers[4]=Sprite.new() self:addChild(self.layers[1]) self:addChild(self.layers[2]) self:addChild(self.layers[3]) self:addChild(self.layers[4]) self:createBall(1,100,50) self:createBall(2,150,50) self:createBall(3,200,50) self:createBall(4,250,50) self:createBall(3,100,10) self:createBall(4,150,10) self:createBall(1,200,10) self:createBall(2,250,10) self:createBodilessBall(4,100,80) self:createBodilessBall(3,150,80) self:createBodilessBall(2,200,80) self:createBodilessBall(1,250,80) self:createBodilessBall(1,115,90) self:createBodilessBall(2,165,90) self:createBodilessBall(3,215,90) self:createBodilessBall(4,265,90) end function game:createBall(layerNo,px,py) local bbody = self.world:createBody{type = b2.DYNAMIC_BODY,position ={x=px, y=py}} local shotball = Bitmap.new(Texture.new("images/ball"..layerNo..".png")) local bradius = shotball:getWidth()/2 local bcircle = b2.CircleShape.new(0, 0, bradius) local bfixture = bbody:createFixture{shape = bcircle,density = 0.7, restitution = 0.6, friction = 0.2, filter = {categoryBits = 2, maskBits = 1}} shotball:setAnchorPoint(0.5,0.5) bbody:setAngle(shotball:getRotation() * math.pi/180) shotball.body = bbody shotball.body.type = "ball" shotball:setPosition(px,py) self.layers[layerNo]:addChild(shotball) end function game:createBodilessBall(layerNo,px,py) local shotball = Bitmap.new(Texture.new("images/ball"..layerNo..".png")) shotball:setPosition(px,py) self.layers[layerNo]:addChild(shotball) end function game:onEnterFrame(event) self.world:step(1/60, 10,8 ) for j = 1, #self.layers do for i = 1, self.layers[j]:getNumChildren() do local sprite = self.layers[j]:getChildAt(i) if sprite.body then local body = sprite.body local bodyX, bodyY = body:getPosition() sprite:setPosition(bodyX, bodyY) sprite:setRotation(body:getAngle() * 180 / math.pi) end end end end function game:onExitBegin() self:removeEventListener(Event.ENTER_FRAME, self.onEnterFrame, self) end |
Likes: ar2rsawseen