Error:my dynamic object becomes static when the scene/level changes on its own

hello,my game is running,there is no runtime error

but when i go from level1 to level2 on collision with a box,although the game is still running but

i get an error in corona simulator output saying

(physics.addBody()cannot be called when the worlds is locked

and in the middle of number crunching,such as during collision)

–and my main player becomes static as the scene changes on its own


–i did my coding like this

–level1.lua

local storyboard=require(“storyboard”);

local scene=storyboard.newScene();

function scene:createScene(event)

–some objects

end

local function next(self,event)

if(event.phase==“began”)then

storyboard.gotoScene(“level2”);

storyboard.removeScene(“level1”);

end

end

–added the event listeners in enterScene and removed the event listeners in exitScenes

I would like to help but I need code to be able to reproduce this issue. I am a little pushy to storyboard but that is a story for another post. As for the error, first it is not a corona problem but a box2d problem 

from Corona docs (https://docs.coronalabs.com/daily/guide/physics/collisionDetection/index.html)

Important Currently, the Box2D physics engine is liable to crash during a collision if Corona code attempts to modify objects still involved in the collision. This is because Box2D is still working out the iterated mathematics on these objects. However, your collision handler may set a flag or include a time delay via timer.performWithDelay() so that the action can occur in the next application cycle or later. Complete removal of object(s) via display.remove() or object:removeSelf() can be executed during the same collision event time step, but the following APIs and methods can not be called during a collision event: object.bodyType object:rotate() object.rotation object:translate() object.x object.y physics.addBody() physics.removeBody() physics.newJoint() physics.newParticleSystem() physics.setContinuous() physics.stop()

You should add a timer before add physics body

timer.performWithDelay(100, function() physics.addBody(obj, "static") -- or object.bodyType = "static" end 

how did you used timer.perform.performWithDelay my main code in level1.lua related to_COLLISION,PLAYER AND SCENE _is;- 

—level1.lua

local storyboard=require(“storyboard”);

local scene=storyboard.newScene();

local centerX=display.contentWidth*0.5;

local centerY=display.contentHeight*0.5;

local physics=require(“physics”);

physics.start();

physics.setGravity(0,0);

function scene:createScene(event)

----a display image of player,dollar  and an icon

end

–functions-----

local function onCollision(self,event)

if(event.phase==“began”)then

a1=transition.to(player,{time=4000,y=170,x=40});

end

end

local function Dollars(self,event)

if(event.phase==“began”)then

storyboard.gotoScene(“level2”);

storyboard.removeScene(“level1”);

end

end

local function Return(event) --an icon to return to previous screen if the user clicks

if(event.phase==“began”)then

storyboard.gotoScene(“levelscreen”);

storyboard.removeScene(“level1”);

end

end

function scene:enterScene(event)

storyboard.removeScene(“levelscreen”);

dollar.collision=Dollars;

dollar:addEventListener(“collision”)

player.collision=onCollision;

player:addEventListener(“collision”)

iconR:addEventListener(“touch”,Return);

end

function scene:exitScene(event)

iconR:removeEventListener(“touch”,Return);

player:removeEventListener(“collision”,onCollision);

dollar:removeEventListener(“collision”,Dollars);

transition.cancel(a1)

end

function scene:destroyScene(event)

end

scene:addEventListener(“createScene”,scene);

scene:addEventListener(“enterScene”,scene);

scene:addEventListener(“exitScene”,scene);

scene:addEventListener(“destroyScene”,scene);

return scene;

when i  go to next level on collision with the dollar image my player becomes “static” on its own.

also i think i didn’t remove the event listeners properly

when i use timer.performWithDelay it shows another error attempted to call global “player”

You cannot call a local attribute in another file

thank you and how can i add a timer to the collision function??

Are you trying use addBody?

yes in createScene

timer.performWithDelay(100, function()
physics.addBody(dollar, “static”) – or object.bodyType = “static”
end

This assuming you call from level 1

there is a runtime error along with collision error when i do it.it says unexpected symbol on line 35 although i have closed all the brackets

local storyboard=require(“storyboard”);

local scene=storyboard.newScene();

local centerX=display.contentWidth*0.5;

local centerY=display.contentHeight*0.5;

local physics=require(“physics”);

physics.start();

physics.setGravity(0,10);

function scene:createScene(event)

local screenGroup=self.view

ball=display.newImageRect(“ball.png”,20,20);

ball.x=centerX+90;

ball.y=centerY-150;

physics.addBody(ball,“dynamic”)

screenGroup:insert(ball);

dollar=display.newImageRect(“dollar.png”,50,50);

dollar.x=centerX+110;

dollar.y=centerY+170;

physics.addBody(dollar,“static”)

screenGroup:insert(dollar);

end

 function onCollision(self,event)

if(event.phase==“began”)then

move1=transition.to(ball,{time=1000,y=centerY-150,x=centerX+90});

end

end

 function Dollars(self,event)

if(event.phase==“began”)then

storyboard.gotoScene(“game2”);

storyboard.removeScene(“game1”);

end

end

function scene:enterScene(event)

timer.performWithDelay(100, function()

physics.addBody(dollar, “static”) )

end 

dollar.collision=Dollars;–collision

dollar:addEventListener(“collision”);

ball.collision=onCollision;–collision

ball:addEventListener(“collision”)–collision

end

function scene:exitScene(event)

ball:removeEventListener(“collision”);

dollar:removeEventListener(“collision”,Dollars);

physics.stop();

end

function scene:destroyScene(event)

–screenGroup=nil;

end

scene:addEventListener(“createScene”,scene);

scene:addEventListener(“enterScene”,scene);

scene:addEventListener(“exitScene”,scene);

scene:addEventListener(“destroyScene”,scene);

return scene;

I would like to help but I need code to be able to reproduce this issue. I am a little pushy to storyboard but that is a story for another post. As for the error, first it is not a corona problem but a box2d problem 

from Corona docs (https://docs.coronalabs.com/daily/guide/physics/collisionDetection/index.html)

Important Currently, the Box2D physics engine is liable to crash during a collision if Corona code attempts to modify objects still involved in the collision. This is because Box2D is still working out the iterated mathematics on these objects. However, your collision handler may set a flag or include a time delay via timer.performWithDelay() so that the action can occur in the next application cycle or later. Complete removal of object(s) via display.remove() or object:removeSelf() can be executed during the same collision event time step, but the following APIs and methods can not be called during a collision event: object.bodyType object:rotate() object.rotation object:translate() object.x object.y physics.addBody() physics.removeBody() physics.newJoint() physics.newParticleSystem() physics.setContinuous() physics.stop()

You should add a timer before add physics body

timer.performWithDelay(100, function() physics.addBody(obj, "static") -- or object.bodyType = "static" end 

how did you used timer.perform.performWithDelay my main code in level1.lua related to_COLLISION,PLAYER AND SCENE _is;- 

—level1.lua

local storyboard=require(“storyboard”);

local scene=storyboard.newScene();

local centerX=display.contentWidth*0.5;

local centerY=display.contentHeight*0.5;

local physics=require(“physics”);

physics.start();

physics.setGravity(0,0);

function scene:createScene(event)

----a display image of player,dollar  and an icon

end

–functions-----

local function onCollision(self,event)

if(event.phase==“began”)then

a1=transition.to(player,{time=4000,y=170,x=40});

end

end

local function Dollars(self,event)

if(event.phase==“began”)then

storyboard.gotoScene(“level2”);

storyboard.removeScene(“level1”);

end

end

local function Return(event) --an icon to return to previous screen if the user clicks

if(event.phase==“began”)then

storyboard.gotoScene(“levelscreen”);

storyboard.removeScene(“level1”);

end

end

function scene:enterScene(event)

storyboard.removeScene(“levelscreen”);

dollar.collision=Dollars;

dollar:addEventListener(“collision”)

player.collision=onCollision;

player:addEventListener(“collision”)

iconR:addEventListener(“touch”,Return);

end

function scene:exitScene(event)

iconR:removeEventListener(“touch”,Return);

player:removeEventListener(“collision”,onCollision);

dollar:removeEventListener(“collision”,Dollars);

transition.cancel(a1)

end

function scene:destroyScene(event)

end

scene:addEventListener(“createScene”,scene);

scene:addEventListener(“enterScene”,scene);

scene:addEventListener(“exitScene”,scene);

scene:addEventListener(“destroyScene”,scene);

return scene;

when i  go to next level on collision with the dollar image my player becomes “static” on its own.

also i think i didn’t remove the event listeners properly

when i use timer.performWithDelay it shows another error attempted to call global “player”

You cannot call a local attribute in another file

thank you and how can i add a timer to the collision function??

Are you trying use addBody?

yes in createScene

timer.performWithDelay(100, function()
physics.addBody(dollar, “static”) – or object.bodyType = “static”
end

This assuming you call from level 1

there is a runtime error along with collision error when i do it.it says unexpected symbol on line 35 although i have closed all the brackets

local storyboard=require(“storyboard”);

local scene=storyboard.newScene();

local centerX=display.contentWidth*0.5;

local centerY=display.contentHeight*0.5;

local physics=require(“physics”);

physics.start();

physics.setGravity(0,10);

function scene:createScene(event)

local screenGroup=self.view

ball=display.newImageRect(“ball.png”,20,20);

ball.x=centerX+90;

ball.y=centerY-150;

physics.addBody(ball,“dynamic”)

screenGroup:insert(ball);

dollar=display.newImageRect(“dollar.png”,50,50);

dollar.x=centerX+110;

dollar.y=centerY+170;

physics.addBody(dollar,“static”)

screenGroup:insert(dollar);

end

 function onCollision(self,event)

if(event.phase==“began”)then

move1=transition.to(ball,{time=1000,y=centerY-150,x=centerX+90});

end

end

 function Dollars(self,event)

if(event.phase==“began”)then

storyboard.gotoScene(“game2”);

storyboard.removeScene(“game1”);

end

end

function scene:enterScene(event)

timer.performWithDelay(100, function()

physics.addBody(dollar, “static”) )

end 

dollar.collision=Dollars;–collision

dollar:addEventListener(“collision”);

ball.collision=onCollision;–collision

ball:addEventListener(“collision”)–collision

end

function scene:exitScene(event)

ball:removeEventListener(“collision”);

dollar:removeEventListener(“collision”,Dollars);

physics.stop();

end

function scene:destroyScene(event)

–screenGroup=nil;

end

scene:addEventListener(“createScene”,scene);

scene:addEventListener(“enterScene”,scene);

scene:addEventListener(“exitScene”,scene);

scene:addEventListener(“destroyScene”,scene);

return scene;