removing an object but its not removing

So I make an objects when the game is paused. But when the game is unpaused, i tried to remove the objects I made, but it doesn’t removed. I already tried to use obj:removeSelf(), obj = nil, and display.remove(obj) but nothing works. Here is my code.

if event.keyName == "back" or event.keyName == "escape" then
                world.pause = true
                local pausemenu = display.newImage( sceneGroup,"assets/menu/pause-menu.png", 180, 180)
                pausemenu.x, pausemenu.y = 400, 150
                
                local txtSound = display.newText("Sound ", 350, 120, "assets/fonts/Shadow of the Deads.ttf", 15)
                txtSound.fill = {color.hex2rgb("#dba400")}

                local txtBlood = display.newText("Blood ", 347, 155, "assets/fonts/Shadow of the Deads.ttf", 15)
                txtBlood.fill = {color.hex2rgb("#dba400")}

                local txtResume = display.newText("Resume ", 362, 190, "assets/fonts/Shadow of the Deads.ttf", 15)
                txtResume.fill = {color.hex2rgb("#dba400")}

                local txtBack = display.newText("Back", 342, 225, "assets/fonts/Shadow of the Deads.ttf", 15)
                txtBack.fill = {color.hex2rgb("#dba400")}

                txtBlood:addEventListener("tap", blood)
                txtResume:addEventListener("tap", resume)
                txtSound:addEventListener("tap", sound)
                txtBack:addEventListener("tap", goBack)

                print(world.pause)
                print("in the event")
                if not world.pause then
                    pausemenu = nil
                    txtBack:removeEventListener("tap")
                    txtBlood:removeEventListener("tap")
                    txtSound:removeEventListener("tap")
                    txtResume:removeEventListener("tap")
                    display.remove(pausemenu)
                    display.remove(txtBack)
                    display.remove(txtBlood)
                    display.remove(txtSound)
                    display.remove(txtResume)
                end
            end

and this is the function I made

local function goBack()
    fx.fadeOut( function()
        composer.gotoScene( "scenes.menu")
    end )
end
local function resume()
    print(world.pause)
    print("before resume")
    world.pause = not world.pause
    print(world.pause)
    print("after resume")
end
local function sound()
    
end
local function blood()
    bblood = not bblood
end

I put the functions above before function:scene:create()

It seems you’re creating your pause menu objects without any conditions, thus they are created every time back or escape keys are pressed regardless whether pause is true or not.

There are some problems with your code at the moment which will probably prevent your pause logic to work correctly.

  • You are switching the world state to paused every time user presses escape so the lines where you try to remove the objects won’t ever get called because world.pause = true.
  • There is also a scoping issue where in that case you will face issues when removing the exact objects you created. You won’t be able to reference those objects correctly once the if clause is closed.
  • What I suggest is creating two separate functions where in the first one you create your pause menu and push those objects into a separate display group(pauseGroup?). Then, a second one where you remove that pauseGroup objects.

So when the escape or back is pressed, instead of making the object inside if, its better if I call a function to made this new display group pauseGroup. And then if I want to resume my game, I add addeventListener('tap', resume) to my txtResume and then inside this function resume() I put pauseGroup:removeSelf() ?

I tried to modify your code without moving things around. I think this should work. Please give it a try. Don’t forget to create the display group menuGroup by the way.

local function removePauseMenu()
    print "removePauseMenu"

    for i = menuGroup.numChildren, 1, -1 do
        display.remove(menuGroup[i])
    end

    world.pause = false
end

local function resume(event)
    if ( event.phase == "ended" ) then
        removePauseMenu()
    end
    return true
end


local function createPauseMenu()
    print "createpauseMenu"

    local bg = display.newRect( menuGroup, display.contentCenterX, display.contentCenterY, display.contentWidth, display.contentHeight )
    bg:setFillColor( 1, 1, 1 )

    local resumeButton = display.newText( menuGroup, "Resume", display.contentCenterX, display.contentCenterY, native.systemFont, 150 )
    resumeButton:setFillColor( 0, 0, 0 )
    resumeButton:addEventListener( "touch", resume )

    world.pause = true
end

-- Back button behavior for Android
local function onKeyEvent( event )
    if ( event.keyName == "back" and event.phase == "up" ) then
        if ( world.pause ) then
            removePauseMenu()
        else
            createPauseMenu()
        end

        return true
    end
end

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.