I have a stack of blocks that in certain cases is restacked to their default position. But when I do this, their x position seems to have a certain randomness that I do not want. This randomness seems to be dependent on the display resolution. If I run the code below on the Galaxy III simulator the block number two from the bottom is acting up when the blocks are re-stacked.
local w = display.contentWidth local h = display.contentHeight local physics = require("physics") physics.start() physics.setScale(w/5) physics.setDrawMode("debug") -- Ground local ground = display.newRect(w/2, h-h/15, w/1.1, h/15) physics.addBody(ground, "static", { density=5.0, friction=0.3, bounce=0.0}) -- Add blocks local blockArr = {} local function placeBlocks() for i = 1, 5 do blockArr[i] = display.newRect(w - w/5, h-h/15 - 10 - i\*(55), 50, 50) print(string.format("Block[%d](%d, %d)", i, blockArr[i].x, blockArr[i].y)) physics.addBody(blockArr[i], "dynamic", { density=2.0 - i/20, friction=0.9, bounce=0.0}) end end -- Clear blocks local function clearBlocks() for i = #blockArr, 1, -1 do physics.removeBody(blockArr[i]) blockArr[i]:removeSelf() blockArr[i] = nil end end local function clearAndReplaceBlocks() clearBlocks() placeBlocks() end timer.performWithDelay( 1500, clearAndReplaceBlocks, 20 )
I’m completely new to lua and I’m not sure if the clearBocks() and placeBlocks() is written well. It could easily be that I’m doing something stupid and have myself to thank.
I guess inaccuracies in the block placement has something to do with it, but what I really do not understand is why the first placement is always “perfect” and the later placements has this random nature.