The errors appear when the player goes to the next level : physics.addBody() cannot be called when the world is locked. Bad argument #-1 to 'newImage'

Hi. My problem is quite big, i need someone to help me.

About the game :

It has two levels: game.lua and game2.lua. If the player earns 10 points, s/he should go to the next level using composer.gotoScene(“game2”, “slideLeft”).  Level 1 works fine, level 2 is a slightly modified version of the 1st one (on its own, it works fine too).

The problem : when the player is about to go to the next level, i see an error message. Somehow i need to make the transition work.

Error 1 : game2.lua: 281 : physics.addBody() cannot be called when the world is locked and in the middle of number crunching, such as during a collision event

Line 281 : physics.addBody(player, “static”, physicsData:get(“shipPlay”))

It is a part of:

player = display.newImage(sceneGroup, "images/shipPlay.png")            player.x = L + 70         player.y = CY         player.name = "player"         physics.addBody(player, "static", physicsData:get("shipPlay"))

Error 2 : game2.lua: 288 : physics.addBody() cannot be called when the world is locked and in the middle of number crunching, such as during a collision event

Line 288 : physics.addBody(playerWall, “static”)

It is a part of: (playerWall helps with collision detection, to earn points)

playerWall = display.newRect(sceneGroup, 0, 0, 50, CH)         playerWall.x = L - 60         playerWall.y = CY         playerWall.name = "playerWall"         physics.addBody(playerWall, "static")         playerWall.alpha = 0

Error 3: game.lua:75: bad argument #-1 to ‘newImage’ (Proxy expected, got nil)

Line 75 :

obstacles[obstaclesCounter] = display.newImage(sceneGroup, "images/"..name..".png" )

It is a part of: (The function is above onCollision function)

local function sendObstacles()         local names = {"obstacle1", "obstacle2", "obstacle3"}         local name = names[math.random(#names)]             obstacles[obstaclesCounter] = display.newImage(sceneGroup, "images/"..name..".png" )             obstacles[obstaclesCounter].x = R + obstacles[obstaclesCounter].width             obstacles[obstaclesCounter].y = math.random(T+40, B-40)             obstacles[obstaclesCounter].name = "obstacles"             physics.addBody(obstacles[obstaclesCounter], physicsData:get(name))             obstacles[obstaclesCounter].isFixedRotation=true         transition.to(obstacles[obstaclesCounter], {x=L-40, time=obstaclesTravelSpeed})         obstaclesCounter = obstaclesCounter + 1     end

For #1 and #2, this happens because while a collision event is running, you can’t make changes to the physics world. Normally someome is trying to remove an object that collided. Since I can’t see all of your code you will just have to try and implement this pseudo code:

if objectCollided then     timer.performWithDelay( 10, function()           create your new display object and add the physics body here     end) end

For #3 “sceneGroup” is nil. If this is not inside scene:create() or scene:show() (and it probably should not be), then it won’t know about sceneGroup. Most of our templates define a variable called sceneGroup inside of scene:create() and scene:show() but those are scoped to only be visible to those functions. Those functions reference the scene’s “view” group as self.view, i.e.

local sceneGroup = self.view

If you’re not inside one of those functions, you have to access the view a different way. Since you define your functions as local to the module and not as a method to the scene object (again, you’re fine doing it they way you are), you have to setup sceneGroup a bit differently.

At the top of the function put:

local sceneGroup = scene.view

and see if that fixes your #3 problem.

Rob

For #1 and #2, this happens because while a collision event is running, you can’t make changes to the physics world. Normally someome is trying to remove an object that collided. Since I can’t see all of your code you will just have to try and implement this pseudo code:

if objectCollided then     timer.performWithDelay( 10, function()           create your new display object and add the physics body here     end) end

For #3 “sceneGroup” is nil. If this is not inside scene:create() or scene:show() (and it probably should not be), then it won’t know about sceneGroup. Most of our templates define a variable called sceneGroup inside of scene:create() and scene:show() but those are scoped to only be visible to those functions. Those functions reference the scene’s “view” group as self.view, i.e.

local sceneGroup = self.view

If you’re not inside one of those functions, you have to access the view a different way. Since you define your functions as local to the module and not as a method to the scene object (again, you’re fine doing it they way you are), you have to setup sceneGroup a bit differently.

At the top of the function put:

local sceneGroup = scene.view

and see if that fixes your #3 problem.

Rob