Baci to Menù Button

Hi Everybody,
I’m having some problems with my button.
When i push the “back” botton (returns to “menu”) in my game and then turn back in game it shows me an error.

What is the error?

The error message is:

game.lua:98: attempt to index field ‘tempjoint’ (a nil value)

So look in your file: game.lua and go to line 98. You are trying to use a table/object that you are expecting to have a property named “tempjoint” and it’s nil/never been initialized. You need to figure out why that’s the case.

Rob

Hi Rob,

This is my function and scene code:

[lua]


– Scene Methods




function scene:create( event )

    local sceneGroup = self.view

physics.start()

physics.setGravity(0,0)

physics.setDrawMode(“normal”)

physics.pause()

local background = display.newImage(sceneGroup, “background.png”)

background.x = display.contentCenterX

background.y = display.contentCenterY

local arms = display.newImage(sceneGroup, “arms1.png”, true)

arms.x = display.contentWidth - 134

arms.y = display.contentHeight - 290

local nail = display.newCircle(sceneGroup, 124.91, 212, 4)

physics.addBody(nail, “static”,{bounce=0, friction=0.2, radius=4})

local wheel = display.newImage(sceneGroup, “wheel1.png”, true)

wheel.x = display.contentWidth - 195

wheel.y = display.contentHeight - 268

physics.addBody(wheel, “dynamic”,{bounce=0, friction=0.2, radius=1})

wheel.angularDamping = 0.4

local skull = display.newImage(sceneGroup, “skull1.png”, true)

skull.x = display.contentWidth - 190

skull.y = display.contentHeight - 379

local joint = physics.newJoint(“pivot”, wheel, nail, 124.91, 212)

    local push1 = PushButton( sceneGroup, centerX, 370, “Back”, onBack,

        { labelColor = {0.8,0.2,0.2}, labelSize = 28 } )

spinObject = function(event)

     local wheel = event.target

     local phase = event.phase

     local stage = display.getCurrentStage()

     if “began” == phase then

           stage:setFocus(wheel, event.id)

           wheel.isFocus = true

           wheel.tempJoint = physics.newJoint(“touch”, wheel, event.x, event.y)

     elseif wheel.isFocus then

         if “moved” == phase then

             wheel.tempJoint:setTarget(event.x, event.y)

         elseif “ended” == phase or “cancelled” == phase then

             stage:setFocus(wheel, nil)

             wheel.isFocus = false

             wheel.tempJoint:removeSelf()

         end

     end

     return true

end

wheel:addEventListener(“touch”, spinObject)

end

    ----------------------------------------------------------------------

    ----------------------------------------------------------------------

function scene:willEnter( event )

    local sceneGroup = self.view

end



function scene:didEnter( event )

    local sceneGroup = self.view

end



function scene:willExit( event )

    local sceneGroup = self.view

end



function scene:didExit( event )

    local sceneGroup = self.view

end



function scene:destroy( event )

    local sceneGroup = self.view

end

    ----------------------------------------------------------------------

    – FUNCTION/CALLBACK DEFINITIONS –

    ----------------------------------------------------------------------

onBack = function ( self, event )

    local options =

    {

        effect = “fromTop”, – See list here: http://docs.coronalabs.com/daily/api/library/composer/gotoScene.html

        time = 500,

        params =

        {

            arg1 = “value”,

            arg2 = 0

        }

    }

    composer.gotoScene( “src.mainMenu”, options  )

    return true

end

    ---------------------------------------------------------------------------------

    – Scene Dispatch Events, Etc. - Generally Do Not Touch Below This Line

    ---------------------------------------------------------------------------------

    function scene:show( event )

        local sceneGroup = self.view

        local willDid = event.phase

        if( willDid == “will” ) then

            self:willEnter( event )

        elseif( willDid == “did” ) then

            physics.start()

            self:didEnter( event )

        end

    end

    function scene:hide( event )

        local sceneGroup = self.view

        local willDid = event.phase

        if( willDid == “will” ) then

            physics.stop()

            self:willExit( event )

        elseif( willDid == “did” ) then

            self:didExit( event )

        end

    end

    function scene:destroy( event )

        local sceneGroup = self.view

    end

    scene:addEventListener( “create”, scene )

    scene:addEventListener( “show”, scene )

    scene:addEventListener( “hide”, scene )

    scene:addEventListener( “destroy”, scene )

    ---------------------------------------------------------------------------------

    return scene 

[/lua] 

But why when i start the game the simulator dosen’t show any error?

The error message comes up only when i come back in game and touch the object.

Thanks

Hi Everybody,

I’m next to find a solution to this issue, let me explain, when in the final part of the code ( in scene hide function ) i put [lua] physics.stop [/lua] the game report the error, but if I put [lua] physics.pause [/lua] everything goes right. But i need to stop the physics engine.

Please help!

Why do you need to stop the physics engine that physics.pause() doesn’t accomplish?

Rob

To learn how to do it

That doesn’t answer the question. physics.pause() stops the simulation temporarily (i.e. while you change scenes to a next level screen). Then when you come back, reposition things with physics being paused, and then call physics.start() to resume the simulation. The only reason to call physics.stop() is to completely end the simulation or if you’re going to remove the scene and have it re-created from scratch the next time you enter it.

What error message are you getting? Can you post your scene:hide() function?

Rob thanks for your explanation and yes i would like to remove the scene when i go back to main menu, you can find my code in the middle of the tread.
Thanks

What error message are you getting? Can you post your scene:hide() function?

[lua] function scene:show( event )
local sceneGroup = self.view
local willDid = event.phase
if( willDid == “will” ) then
self:willEnter( event )
elseif( willDid == “did” ) then
physics.start()
self:didEnter( event )
end
end
function scene:hide( event )
local sceneGroup = self.view
local willDid = event.phase
if( willDid == “will” ) then
physics.stop()
self:willExit( event )
elseif( willDid == “did” ) then
self:didExit( event )
end
end
function scene:destroy( event )
local sceneGroup = self.view

end [/lua]

What is the error?

The error message is:

game.lua:98: attempt to index field ‘tempjoint’ (a nil value)

So look in your file: game.lua and go to line 98. You are trying to use a table/object that you are expecting to have a property named “tempjoint” and it’s nil/never been initialized. You need to figure out why that’s the case.

Rob

Hi Rob,

This is my function and scene code:

[lua]


– Scene Methods




function scene:create( event )

    local sceneGroup = self.view

physics.start()

physics.setGravity(0,0)

physics.setDrawMode(“normal”)

physics.pause()

local background = display.newImage(sceneGroup, “background.png”)

background.x = display.contentCenterX

background.y = display.contentCenterY

local arms = display.newImage(sceneGroup, “arms1.png”, true)

arms.x = display.contentWidth - 134

arms.y = display.contentHeight - 290

local nail = display.newCircle(sceneGroup, 124.91, 212, 4)

physics.addBody(nail, “static”,{bounce=0, friction=0.2, radius=4})

local wheel = display.newImage(sceneGroup, “wheel1.png”, true)

wheel.x = display.contentWidth - 195

wheel.y = display.contentHeight - 268

physics.addBody(wheel, “dynamic”,{bounce=0, friction=0.2, radius=1})

wheel.angularDamping = 0.4

local skull = display.newImage(sceneGroup, “skull1.png”, true)

skull.x = display.contentWidth - 190

skull.y = display.contentHeight - 379

local joint = physics.newJoint(“pivot”, wheel, nail, 124.91, 212)

    local push1 = PushButton( sceneGroup, centerX, 370, “Back”, onBack,

        { labelColor = {0.8,0.2,0.2}, labelSize = 28 } )

spinObject = function(event)

     local wheel = event.target

     local phase = event.phase

     local stage = display.getCurrentStage()

     if “began” == phase then

           stage:setFocus(wheel, event.id)

           wheel.isFocus = true

           wheel.tempJoint = physics.newJoint(“touch”, wheel, event.x, event.y)

     elseif wheel.isFocus then

         if “moved” == phase then

             wheel.tempJoint:setTarget(event.x, event.y)

         elseif “ended” == phase or “cancelled” == phase then

             stage:setFocus(wheel, nil)

             wheel.isFocus = false

             wheel.tempJoint:removeSelf()

         end

     end

     return true

end

wheel:addEventListener(“touch”, spinObject)

end

    ----------------------------------------------------------------------

    ----------------------------------------------------------------------

function scene:willEnter( event )

    local sceneGroup = self.view

end



function scene:didEnter( event )

    local sceneGroup = self.view

end



function scene:willExit( event )

    local sceneGroup = self.view

end



function scene:didExit( event )

    local sceneGroup = self.view

end



function scene:destroy( event )

    local sceneGroup = self.view

end

    ----------------------------------------------------------------------

    – FUNCTION/CALLBACK DEFINITIONS –

    ----------------------------------------------------------------------

onBack = function ( self, event )

    local options =

    {

        effect = “fromTop”, – See list here: http://docs.coronalabs.com/daily/api/library/composer/gotoScene.html

        time = 500,

        params =

        {

            arg1 = “value”,

            arg2 = 0

        }

    }

    composer.gotoScene( “src.mainMenu”, options  )

    return true

end

    ---------------------------------------------------------------------------------

    – Scene Dispatch Events, Etc. - Generally Do Not Touch Below This Line

    ---------------------------------------------------------------------------------

    function scene:show( event )

        local sceneGroup = self.view

        local willDid = event.phase

        if( willDid == “will” ) then

            self:willEnter( event )

        elseif( willDid == “did” ) then

            physics.start()

            self:didEnter( event )

        end

    end

    function scene:hide( event )

        local sceneGroup = self.view

        local willDid = event.phase

        if( willDid == “will” ) then

            physics.stop()

            self:willExit( event )

        elseif( willDid == “did” ) then

            self:didExit( event )

        end

    end

    function scene:destroy( event )

        local sceneGroup = self.view

    end

    scene:addEventListener( “create”, scene )

    scene:addEventListener( “show”, scene )

    scene:addEventListener( “hide”, scene )

    scene:addEventListener( “destroy”, scene )

    ---------------------------------------------------------------------------------

    return scene 

[/lua] 

But why when i start the game the simulator dosen’t show any error?

The error message comes up only when i come back in game and touch the object.

Thanks

Hi Everybody,

I’m next to find a solution to this issue, let me explain, when in the final part of the code ( in scene hide function ) i put [lua] physics.stop [/lua] the game report the error, but if I put [lua] physics.pause [/lua] everything goes right. But i need to stop the physics engine.

Please help!

Why do you need to stop the physics engine that physics.pause() doesn’t accomplish?

Rob

To learn how to do it

That doesn’t answer the question. physics.pause() stops the simulation temporarily (i.e. while you change scenes to a next level screen). Then when you come back, reposition things with physics being paused, and then call physics.start() to resume the simulation. The only reason to call physics.stop() is to completely end the simulation or if you’re going to remove the scene and have it re-created from scratch the next time you enter it.

What error message are you getting? Can you post your scene:hide() function?