Bad argument #-1 to 'addBody' (Proxy expected, got nil)

I have a code like this


local storyboard = require( “storyboard” )
local widget = require “widget”
local scene = storyboard.newScene()

local image, text1, memTimer, wallFloor, wallTop, wallLeft, wallRightBottom, wallRightTop, wallScape
local ladyBug

local function movePlace()
        storyboard.gotoScene( “level1Q14”, “crossFade”, 500 )    
    return true
end

local function dragBody ( event )
    local body = event.target
    local phase = event.phase
    local stage = display.getCurrentStage ( )
    
    if “began” == phase then
        stage:setFocus ( body, event.id )
        body.isFocus = true
        body.tempJoint = physics.newJoint ( “touch”, body, event.x, event.y )
    elseif body.isFocus then
    
            if “moved” == phase then
                body.tempJoint:setTarget ( event.x, event.y )
            elseif “ended” == phase or “cancelled” == phase then
                stage:setFocus (body, nil)
                body.isFocus = false
                body.tempJoint:removeSelf ( )
            end
        
    end
    
    return true
    
end
--------------------------------------------------------------CREATE SCENE----------
function scene:createScene( event )
    local screenGroup = self.view
    
    local physics = require (“physics”)
    physics.start()
    
        image = display.newImage( “backgroundPlayLadyBug.png” )
        screenGroup:insert( image )
        image.x = 509
        image.y = 384

        wallFloor = display.newImage( “wallFloor.png” )
        screenGroup:insert( wallFloor )
        wallFloor.x = 512
        wallFloor.y = 759
        
        wallTop = display.newImage( “wallTop.png” )
        screenGroup:insert( wallTop )
        wallTop.x = 512
        wallTop.y = 5
        
        wallLeft = display.newImage( “wallLeft.png” )
        screenGroup:insert( wallLeft )
        wallLeft.x = 5
        wallLeft.y = 380
        
        wallRightBottom = display.newImage( “wallRightBottom.png” )
        screenGroup:insert( wallRightBottom )
        wallRightBottom.x = 1020
        wallRightBottom.y = 495
        
        wallRightTop = display.newImage( “wallRightTop.png” )
        screenGroup:insert( wallRightTop )
        wallRightTop.x = 1020
        wallRightTop.y = 90
        
        wallScape = display.newImage( “wallLadyBugScape.png” )
        screenGroup:insert( wallScape )
        wallScape.x = 1020
        wallScape.y = 212
    
        text1 = display.newText( “Help the LadyBug scape!”, 0, 0, nil, 20 )
        text1:setTextColor( 255 )
        text1:setReferencePoint( display.CenterReferencePoint )
        text1.x, text1.y = display.contentWidth * 0.5, 50
        screenGroup:insert( text1 )
        
end

--------------------------------------------------------------ENTER SCENE----------
function scene:enterScene( event )
    local group = self.view
    
        storyboard.purgeScene( “home” )
    
        buttonB = widget.newButton{
        defaultFile=“buttonHome.png”,
        onRelease = movePlace
        }
        buttonB.x = 500
        buttonB.y = 695
        group:insert ( buttonB )
                
        ladyBug = display.newImage (“ladyBugSmall.png”)
        ladyBug.x = 200
        ladyBug.y = 600

physics.addBody (wallFloor, “static”)
physics.addBody (wallTop, “static”)
physics.addBody (wallLeft, “static”)
physics.addBody (wallRightBottom, “static”)
physics.addBody (wallRightTop, “static”)

physics.addBody (ladyBug, “dynamic”, {density=9, friction=-0.8 , bounce = 0.4})

ladyBug:addEventListener (“touch”, dragBody)
    
end

--------------------------------------------------------------EXIT SCENE----------
function scene:exitScene()
    --cat:removeEventListener (“touch”, dragBody)
    ladyBug:removeEventListener (“touch”, dragBody)
    
    --display.remove ( cat )
    display.remove ( wallFloor )
    display.remove ( wallScape )
    display.remove ( ladyBug )
    display.remove ( wallTop )
    display.remove ( wallLeft )
    display.remove ( wallRightBottom )
    display.remove ( wallRightTop )

end

--------------------------------------------------------------DESTROY SCENE----------
function scene:destroyScene( event )
    if buttonB then
            buttonB:removeSelf()
            buttonB = nil
        end
end

– END OF YOUR IMPLEMENTATION

scene:addEventListener( “createScene”, scene )
scene:addEventListener( “enterScene”, scene )
scene:addEventListener( “exitScene”, scene )
scene:addEventListener( “destroyScene”, scene )

return scene




The first time I go to this gameScene, the game  (or the code) runs perfect!

When I go to another scene and then I come back to the same GameScene

that’s when I have this message:

File: …sers/victormbarba/Desktop/Piano For Kids/working.lua
Line: 113

Bad argument #-1 to ‘addBody’ (Proxy expected, got nil)


I found one article that tells me to copy the proxy.lua file and drop it in my folder, I did and didn’t work!

Could you please help me out and tell me what am I doing wrong,

This is all I need to finish my app.

Thanks

Victor

I change this line

physics.addBody (wallRightBottom, “static”) – This line was on the enterScene, and I move it to

wallRightBottom = display.newImage( “wallRightBottom.png” )
        screenGroup:insert( wallRightBottom )
        wallRightBottom.x = 1020
        wallRightBottom.y = 495
        physics.addBody (wallRightBottom, “static”)

in the createScene, I also change all the walls line of code…

Now it’s not giving me any errors, but when I comeback to the gameScene,

the code it’s not displaying the walls at all!

I get the background, the ladyBug, the buttonHome, but NO WALLS!

that’s strange, can you please help me out with this dilemma?

Victor

Hi Victor,

You should “require” the physics library in the core of this scene (at the top), above and outside of the “createScene” function. You’ve localized it to just that function, so when you try to add a physics body in another scene function, it doesn’t know what “addBody” is (hence the error you report).

Brent

Hi Brent, thanks. I put some code in there and the “physics” works!

but now all of the rest of the app doesn’t work. Rob is helping me trying to solve this “problem”

if you also have some ideas, please help me out.

everything is in here

http://forums.coronalabs.com/topic/35607-help-on-purgescene-i-still-dont-understand-it/

I also sent an e-mail to Dr. Burton, hoping that someone knows where is the problem.

I just need this to finish the app.

Thanks Brent for all your help.

Victor

I change this line

physics.addBody (wallRightBottom, “static”) – This line was on the enterScene, and I move it to

wallRightBottom = display.newImage( “wallRightBottom.png” )
        screenGroup:insert( wallRightBottom )
        wallRightBottom.x = 1020
        wallRightBottom.y = 495
        physics.addBody (wallRightBottom, “static”)

in the createScene, I also change all the walls line of code…

Now it’s not giving me any errors, but when I comeback to the gameScene,

the code it’s not displaying the walls at all!

I get the background, the ladyBug, the buttonHome, but NO WALLS!

that’s strange, can you please help me out with this dilemma?

Victor

Hi Victor,

You should “require” the physics library in the core of this scene (at the top), above and outside of the “createScene” function. You’ve localized it to just that function, so when you try to add a physics body in another scene function, it doesn’t know what “addBody” is (hence the error you report).

Brent

Hi Brent, thanks. I put some code in there and the “physics” works!

but now all of the rest of the app doesn’t work. Rob is helping me trying to solve this “problem”

if you also have some ideas, please help me out.

everything is in here

http://forums.coronalabs.com/topic/35607-help-on-purgescene-i-still-dont-understand-it/

I also sent an e-mail to Dr. Burton, hoping that someone knows where is the problem.

I just need this to finish the app.

Thanks Brent for all your help.

Victor