here is the code that replicates problem
in this code, 3 crates are stacked.
initially you see each crate is stacked correctly. once crates are removed, physics is paused and added new crates after starting physics crates are not stacked properly. they get displaced.
sequence is
physics start -> stack crates -> remove crates -> pause physics-> start physics -> stack crates (just move x-position by 10 pixels compared to previous crates position) -> crates get displaced
if I do the same operation and use stop physics instead of pause physics crates stacked properly.
problem is physics.stop() function causing game to crash often, game got low ratings because of this crash.
local physics = require("physics")
physics.setDrawMode("hybrid")
physics.start(true)
physics.setGravity(0,9.8)
display.setStatusBar( display.HiddenStatusBar )
local bkg = display.newImage( "bkg\_cor.png" )
local grass = display.newImage("grass.png")
grass.x = 160; grass.y = 430
local grass2 = display.newImage("grass2.png") -- non-physical decorative overlay
grass2.x = 160; grass2.y = 440
physics.addBody( grass, "static", { friction=0.5, bounce=0.3 } )
local crates = {}
local stopit = false --make it true to stop instead of pause, you wont see problem
local displace = 0
--local dropCrates = timer.performWithDelay( 500, newCrate, 100 )
function startagain()
physics.start(true)
if stopit then
local grass = display.newImage("grass.png")
grass.x = 160; grass.y = 430
physics.addBody( grass, "static", { friction=0.5, bounce=0.3 } )
end
if displace == 0 then
displace = 10 --change this to \> 30(since crate dimension is 30), won;t see the problem
else
displace = 0
end
SetCretes()
end
function CreatePhysicsProblem()
for i=1,#crates do
crates[i]:removeSelf()
crates[i] = nil
end
crates = nil
crates= {}
if stopit then
physics.stop()
else
physics.pause()
end
timer.performWithDelay( 1000, startagain )
end
function SetCretes()
local j = display.newImageRect("crate.png",30,30);
crates[#crates+1] = j
j.x = 60+displace
j.y = 405--335
physics.addBody( j, { density=3, friction=.500, bounce=0} )
local j = display.newImageRect("crate.png",30,30);
crates[#crates+1] = j
j.x = 60+displace
j.y = 368
physics.addBody( j, { density=3, friction=.500, bounce=0} )
local j = display.newImageRect("crate.png",30,30);
crates[#crates+1] = j
j.x = 60+displace
j.y = 330--402
physics.addBody( j, { density=3, friction=.500, bounce=0} )
timer.performWithDelay( 2000, CreatePhysicsProblem)
end
SetCretes()
I placed code in pastebin (http://pastebin.com/LwaDd8uu)
edit: above code is modified taking ManyCrates example as base
[import]uid: 97420 topic_id: 21705 reply_id: 321705[/import]