It looks like you're new here. If you want to get involved, click one of these buttons!
require 'box2d' local puzzle = Core.class(Sprite) function puzzle:init() self.world = b2.World.new(0,0); local screenW = 402 local screenH = 402 self:wall(200,screenH/2+300,0, screenH) self:wall(screenW/2+200,300,screenW,0) self:wall(screenW+200,screenH/2+300,0,screenH) self:wall(screenW/2+200,screenH+300,screenW,0) self.tiles = {} local shift = 100 for i = 1,15 do local xpos = ((i-1) % 4) * shift + 200 local ypos = math.floor( (i-1) / 4 ) * shift + 300 self.tiles[i] = self:createBox(xpos,ypos,i) end local ground = self.world:createBody({}) local mouseJoint = nil function self:onMouseDown(event) for index, tile in ipairs(self.tiles) do if tile:hitTestPoint(event.x, event.y) then local jointDef = b2.createMouseJointDef(ground, tile.body, event.x, event.y, 100000) mouseJoint = self.world:createJoint(jointDef) return end end end function self:onMouseMove(event) if mouseJoint ~= nil then mouseJoint:setTarget(event.x, event.y) end end function self:onMouseUp(event) if mouseJoint ~= nil then self.world:destroyJoint(mouseJoint) mouseJoint = nil end end function self:onPreSolve(e) local contact = e.contact contact:setRestitution(0) end self:addEventListener(Event.MOUSE_DOWN, self.onMouseDown, self) self:addEventListener(Event.MOUSE_MOVE, self.onMouseMove, self) self:addEventListener(Event.MOUSE_UP, self.onMouseUp, self) self:addEventListener(Event.PRE_SOLVE, self.onPreSolve, self) --local debugDraw = b2.DebugDraw.new(false) --self.world:setDebugDraw(debugDraw) --self:addChild(debugDraw) self:addEventListener(Event.ENTER_FRAME, self.onEnterFrame, self) end function puzzle:wall(x, y, width, height) local wall = Shape.new() wall:setFillStyle(Shape.SOLID, 0xff0000) wall:beginPath() wall:moveTo(-width/2,-height/2) wall:lineTo(width/2, -height/2) wall:lineTo(width/2, height/2) wall:lineTo(-width/2, height/2) wall:closePath() wall:endPath() wall:setPosition(x,y) local body = self.world:createBody{type = b2.STATIC_BODY} body:setPosition(wall:getX(), wall:getY()) local poly = b2.PolygonShape.new() poly:setAsBox(wall:getWidth()/2, wall:getHeight()/2) local fixture = body:createFixture{shape = poly, density = 0, friction = 0, restitution = 0} wall.body = body wall.body.type = "wall" local body = self.world:createBody{type = b2.STATIC_BODY} body:setPosition(wall:getX(), wall:getY()) local poly = b2.PolygonShape.new() poly:setAsBox(wall:getWidth()/2, wall:getHeight()/2) wall.body = body wall.body.type = "wall" self:addChild(wall) return wall end function puzzle:createBox(x, y, num) local tile = Bitmap.new(Texture.new('./' .. num .. '.jpg')); -- 50 = half the tile dim tile:setPosition(x+50,y+50) tile:setAnchorPoint(0.5, 0.5) local shape = b2.PolygonShape.new() shape:setAsBox(50, 50) local myBody = self.world:createBody{type = b2.DYNAMIC_BODY} myBody:setPosition(tile:getX(), tile:getY()) myBody:setLinearDamping(5) myBody:createFixture{shape = shape, density = 0, restitution = 0, friction = 0} tile.body = myBody tile.body.type = "tile" self:addChild(tile) return tile end function puzzle:onEnterFrame() self.world:step(1/60, 8, 3) for i = 1, self:getNumChildren() do local sprite = self:getChildAt(i) if sprite.body then local body = sprite.body local bodyX, bodyY = body:getPosition() sprite:setPosition(bodyX, bodyY) end end end local mainScene = puzzle.new() stage:addChild(mainScene) |
Comments
Ultimate Games on Appstore
Ultimate Games on Google Play
Though I would make all the boxes static and then make the one touched dynamic, more importantly, why would you want to use physics for moving tiles
Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
Cool Vizify Profile at https://www.vizify.com/oz-apps
Ultimate Games on Appstore
Ultimate Games on Google Play