# removing an object but its not removing

**URL:** <https://forums.solar2d.com/t/removing-an-object-but-its-not-removing/356084>\
**Category:** Games\
**Created:** [October 24, 2023, 5:28pm UTC](https://forums.solar2d.com/t/removing-an-object-but-its-not-removing/356084 "2023-10-24T17:28:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![albert0310](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/a/2bfe46/32.png) [@albert0310](https://forums.solar2d.com/u/albert0310)\
**Post date:** [October 24, 2023, 5:28pm UTC](https://forums.solar2d.com/t/removing-an-object-but-its-not-removing/356084/1 "2023-10-24T17:28:19Z")

</div>

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.

```auto
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

```auto
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()

---

<div class="post-metadata">

**Author:** ![Siu](https://forums.solar2d.com/user_avatar/forums.solar2d.com/siu/32/2129_2.png) [@Siu](https://forums.solar2d.com/u/Siu)\
**Post date:** [October 24, 2023, 11:31pm UTC](https://forums.solar2d.com/t/removing-an-object-but-its-not-removing/356084/2 "2023-10-24T23:31:34Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![bgmadclown](https://forums.solar2d.com/user_avatar/forums.solar2d.com/bgmadclown/32/490_2.png) [@bgmadclown](https://forums.solar2d.com/u/bgmadclown)\
**Post date:** [October 24, 2023, 11:32pm UTC](https://forums.solar2d.com/t/removing-an-object-but-its-not-removing/356084/3 "2023-10-24T23:32:50Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![albert0310](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/a/2bfe46/32.png) [@albert0310](https://forums.solar2d.com/u/albert0310)\
**Post date:** [October 25, 2023, 5:03pm UTC](https://forums.solar2d.com/t/removing-an-object-but-its-not-removing/356084/4 "2023-10-25T17:03:24Z")

</div>

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()` ?

---

<div class="post-metadata">

**Author:** ![bgmadclown](https://forums.solar2d.com/user_avatar/forums.solar2d.com/bgmadclown/32/490_2.png) [@bgmadclown](https://forums.solar2d.com/u/bgmadclown)\
**Post date:** [October 25, 2023, 10:41pm UTC](https://forums.solar2d.com/t/removing-an-object-but-its-not-removing/356084/5 "2023-10-25T22:41:07Z")

</div>

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._

```auto
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

```

---

<div class="post-metadata">

**Author:** ![system](https://forums.solar2d.com/uploads/default/original/1X/1bf6a947166df24e92aaa3358558f4b82d7f3d5a.png) [@system](https://forums.solar2d.com/u/system)\
**Post date:** [April 22, 2024, 10:41pm UTC](https://forums.solar2d.com/t/removing-an-object-but-its-not-removing/356084/6 "2024-04-22T22:41:13Z")

</div>

Closed on April 22, 2024, 10:41pm UTC

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