attempt to call method 'removeSelf' (a nil value)

I keep getting this error and I can’t fix it . I’ve been looking on here and stack overflow for answer but none help me . I keep getting this error :

attempt to call method ‘removeSelf’ (a nil value)

It started when I was trying to add a reset button to my code . When I click the button it runs the startGame() function . Please help me 
 

function startGame(event) titleScreenGroup:removeSelf() appodeal.show("interstitial") --\<-------- move this to scene:show()'s "did" phase resetScore() -- in the case that this is a rematch placePlayerOnePaddle() placePlayerTwoPaddle() placePuck(puckAvalLocation.center) Runtime:addEventListener( "postCollision", onPostCollision ) Runtime:addEventListener( "collision", onCollision ) end function resetScore() p1ScoreText.text = "0" p2ScoreText.text = "0" end local function gameRestart(event) if ("ended" == event.phase) then --code here when touch begin startGame() end end function setUpGroundGraphics() local graphic = display.newImageRect( "bg.png", 768, 1024 ) graphic.x = display.contentWidth / 2 graphic.y = display.contentHeight / 2 local graphic = display.newImageRect( "score.png", 122, 144 ) graphic.x = topLeft.x + 90 graphic.y = display.contentHeight / 2 graphic = display.newImageRect( "centerLine.png", 768, 9 ) graphic.x = display.contentWidth / 2 graphic.y = display.contentHeight / 2 graphic = display.newImageRect( "centerCircle.png", 198, 198 ) graphic.x = display.contentWidth / 2 graphic.y = display.contentHeight / 2 -- top goal line graphic = display.newImageRect( "goalLine.png", 497, 203 ) graphic.x = display.contentWidth / 2 graphic.y = topLeft.y + wallThickness \* 2 + 70 -- bottom goal line graphic = display.newImageRect( "goalLine.png", 497, 203 ) graphic.x = display.contentWidth / 2 graphic.y = bottomRight.y - wallThickness \* 2 -70 graphic.rotation = 180 resetButton = display.newImageRect( "reset.png", 508, 224 ) resetButton.x = display.contentWidth / 4.1 resetButton.y = display.contentHeight - display.contentHeight / 2.5 resetButton:scale( 0.4, 0.4 ) resetButton:addEventListener("touch", startGame) end

The object was already removed and the function ‘removeSelf’ is no longer valid.

Better to use ‘display.remove( obj )’ if you’re not 100% sure your code is bug free.

ex:

display.remove( titleScreenGroup )

I already tried that .

Then you must have replaced the wrong line of code, because you won’t get that message if you’re not calling removeSelf()

i.e. You can’t get an error message for a function you’re not calling.
 
Note: You will also get this message from widget.* objects if handled incorrectly.

Try grepping your project for removeSelf

If you don’t know what grepping is, google grep.

If you’re on a Windows machine a great ‘grepping’ tool is ‘Agent Ransack

If you’re on an OS X machine grep is available on the ‘command line’.

  1. Open terminal

2.  cd to folder where main.lua exists in your project

  1. Type this:

    grep -rl “removeSelf” .

Note: grep is case-sensitive, so watch your spelling.

In the director.lua I got three results .

curGroup:removeself()

curGroup:removeself()

fade:removeSelf()

When I run this code display.remove( titleScreenGroup )

The only thing that happens is the scene just builds on the other one and doesn’t reset

Fine. so you uncovered another problem with your game.  That doesn’t mean you throw away the fix to the initial problem you’re complaining about.

The real question you need to answer here is, “Why is your game destroying that object twice?”

Also, why are you using director?  You seem to be jumping all over the place and you’ll never learn anything that way.

One week you’re using composer, another you’re using storyboard, now director…

I think what is really going on here is that you’re game isn’t working and you were hoping whatever was causing the error message (your initial post) would be the cause of all your troubles.

I keep saying this, but you need a mentor, or at least an experienced teammate/friend who can help you hands-on and face-to-face.

yea i know but no one I know knows this stuff

I found that you post your question on stackoverflow.com and here. So I decide to answer also here:)

I think you try remove _titleScreenGroup _group more than one time. This should help:

function startGame(event) &nbsp; &nbsp; if titleScreenGroup then &nbsp; &nbsp; &nbsp; &nbsp; titleScreenGroup:removeSelf() &nbsp; &nbsp; &nbsp; &nbsp; titleScreenGroup = nil &nbsp; &nbsp; end &nbsp; &nbsp; resetScore() -- in the case that this is a rematch &nbsp; &nbsp; placePlayerOnePaddle()&nbsp; &nbsp; &nbsp; placePlayerTwoPaddle() &nbsp; &nbsp; placePuck(puckAvalLocation.center)&nbsp;&nbsp; &nbsp; &nbsp; Runtime:addEventListener( "postCollision", onPostCollision ) &nbsp; &nbsp; Runtime:addEventListener( "collision", onCollision ) &nbsp; &nbsp; appodeal.show("interstitial")&nbsp; &nbsp; --\<-------- move this to scene:show()'s "did" phase end

I noticed also that _startGame _function is called twice when user touch play button. Quick fix:

function setUpTitleScreen() &nbsp; &nbsp; ... &nbsp; &nbsp; playButton:addEventListener("touch", gameRestart) end

Below code you provide on stackoverflow:

function startGame(event) titleScreenGroup:removeSelf() resetScore() -- in the case that this is a rematch placePlayerOnePaddle() placePlayerTwoPaddle() placePuck(puckAvalLocation.center)&nbsp;&nbsp; Runtime:addEventListener( "postCollision", onPostCollision ) Runtime:addEventListener( "collision", onCollision ) appodeal.show("interstitial")&nbsp; &nbsp; --\<-------- move this to scene:show()'s "did" phase end local function gameRestart(event) if ("ended" == event.phase) then &nbsp; &nbsp; --code here when touch begin &nbsp; &nbsp; startGame() end end function setUpGroundGraphics() local graphic = display.newImageRect( "bg.png", 768, 1024 ) graphic.x = display.contentWidth / 2 graphic.y = display.contentHeight / 2 local graphic = display.newImageRect( "score.png", 122, 144 ) graphic.x = topLeft.x + 90 graphic.y = display.contentHeight / 2 graphic = display.newImageRect( "centerLine.png", 768, 9 ) graphic.x = display.contentWidth / 2 graphic.y = display.contentHeight / 2 graphic = display.newImageRect( "centerCircle.png", 198, 198 ) graphic.x = display.contentWidth / 2 graphic.y = display.contentHeight / 2 -- top goal line graphic = display.newImageRect( "goalLine.png", 497, 203 ) graphic.x = display.contentWidth / 2 graphic.y = topLeft.y + wallThickness \* 2 + 70 -- bottom goal line graphic = display.newImageRect( "goalLine.png", 497, 203 ) graphic.x = display.contentWidth / 5 graphic.y = bottomRight.y - wallThickness \* 2 -70 graphic.rotation = 180 resetButton = display.newImageRect( "reset.png", 508, 224 ) resetButton.x = display.contentWidth / 4.1 resetButton.y = display.contentHeight - display.contentHeight / 2.5 resetButton:scale( 0.4, 0.4 ) resetButton:addEventListener("touch", gameRestart) end

local lastForce -- Used to calculate the volume of a the collision sound effects local titleScreenGroup function main() display.setStatusBar( display.HiddenStatusBar ) setUpTable() setUpTitleScreen()&nbsp;&nbsp; end function setUpTable() setUpGroundGraphics() setUpPaddleWalls() setUpPuckWalls() setUpScoreText(); end function setUpTitleScreen() titleScreenGroup = display.newGroup() local background =&nbsp; display.newImageRect("rink.jpg",display.contentWidth,display.contentHeight) background.x = display.contentCenterX background.y = display.contentCenterY titleScreenGroup:insert(background) title = display.newImageRect( "title.png", 530, 196 ) title.x = display.contentWidth / 2 title.y = display.contentHeight / 4 titleScreenGroup:insert(title) playButton = display.newImageRect( "play.png", 508, 224 ) playButton.x = display.contentWidth / 2 playButton.y = display.contentHeight - display.contentHeight / 4 titleScreenGroup:insert(playButton) display.getCurrentStage():insert(titleScreenGroup) playButton:addEventListener("touch", startGame) end

I still get the same results 

local function gameRestart(event) if ("ended" == event.phase) then --code here when touch begin startGame() end end function setUpTitleScreen() titleScreenGroup = display.newGroup() local background = display.newImageRect("rink.jpg",display.contentWidth,display.contentHeight) background.x = display.contentCenterX background.y = display.contentCenterY titleScreenGroup:insert(background) title = display.newImageRect( "title.png", 530, 196 ) title.x = display.contentWidth / 2 title.y = display.contentHeight / 4 titleScreenGroup:insert(title) playButton = display.newImageRect( "play.png", 508, 224 ) playButton.x = display.contentWidth / 2 playButton.y = display.contentHeight - display.contentHeight / 4 titleScreenGroup:insert(playButton) display.getCurrentStage():insert(titleScreenGroup) playButton:addEventListener("touch", gameRestart) end function startGame(event) if titleScreenGroup then titleScreenGroup:removeSelf() titleScreenGroup = nil end resetScore() -- in the case that this is a rematch placePlayerOnePaddle() placePlayerTwoPaddle() placePuck(puckAvalLocation.center) Runtime:addEventListener( "postCollision", onPostCollision ) Runtime:addEventListener( "collision", onCollision ) appodeal.show("interstitial") --\<-------- move this to scene:show()'s "did" phase end

Since you don’t have anyone near you to help, let me give you some advice.

  1. Stop using scene managers like composer.  - I’ve noticed you always try to make your games with a scene manager and that isn’t necessary.

  2. Instead, make your game mechanic with four script files:

  • config.lua
  • build.settings
  • main.lua
  • game.lua
  1. Place all of the files in the same folder (no sub-folders).

  2. Define the contents of the game.lua and main.lua  files as follows:

game.lua - A module containing your game code:

-- ANY REQUIRE STATEMENTS YOU NEED HERE local physics = require "physics" -- ANY LOCALS you need local gameGroup -- Start the module definition local game = {} -- == -- Game Creation Logic -- == function game.create( parent ) game.destroy() -- parent = parent or display.currentStage gameGroup = display.newGroup() parent:insert(gameGroup) -- -- YOUR GAME CREATION CODE HERE -- YOUR GAME CREATION CODE HERE -- YOUR GAME CREATION CODE HERE -- Tip: Insert all display objects into 'gameGroup' end -- == -- Game Cleanup and Destruction Logic -- == function game.destroy( ) -- YOUR GAME CLEANUP CODE HERE -- YOUR GAME CLEANUP CODE HERE -- YOUR GAME CLEANUP CODE HERE --- display.remove( gameGroup ) gameGroup = nil end return game

main.lua

io.output():setvbuf("no") display.setStatusBar(display.HiddenStatusBar) -- local game = require "game" game.create()

This has all the parts you need.

Later

Later, you can test how bug-free and robust your game is by changing main.lua to this:

io.output():setvbuf("no") display.setStatusBar(display.HiddenStatusBar) -- local game = require "game" game.create() timer.peformWithDelay( 30000, game.create ) -- destroy and re-create game in 30 seconds

If you game crashes, you know you’ve got issues and should fix them.

If not, try doing this again, but try to play the game during those 30 seconds.  If it crashes you’ve got more bugs in your code.  Fix them.

Much Much Much Later

_ Warning: _ If you just right to this step, you’re wasting your time… and will probably fail to make a game.

Much later, you can insert this code into a composer scene as easily as this:

play.gui - Scene file to play game

local composer = require( "composer" ) local scene = composer.newScene() local game = require "game" function scene:create( event ) local sceneGroup = self.view game.create( sceneGroup ) end function scene:destroy( event ) game.destroy() end scene:addEventListener( "create", scene ) scene:addEventListener( "destroy", scene ) return scene

main.lua - update main.lua to load composer scene

io.output():setvbuf("no") display.setStatusBar(display.HiddenStatusBar) -- ============================================================= local composer = require "composer" composer.gotoScene( "play" )

By the way, this is basically how I approach all my projects.  I NEVER EVER EVER start with a scene manager. 

It is a waste of time and energy when prototyping.

Also, for someone who is inexperienced and/or has spotty mastery of Corona features, this is a sure-fire way to make your life more difficult.

Now,  I know that composer.* (and framework.*) before it were designed (among many purposes) as a harness for ‘making your first game’, but time and time again I see new folks completely not understand the basic principles of these libraries.  Thus my advise.

The object was already removed and the function ‘removeSelf’ is no longer valid.

Better to use ‘display.remove( obj )’ if you’re not 100% sure your code is bug free.

ex:

display.remove( titleScreenGroup )

I already tried that .

Then you must have replaced the wrong line of code, because you won’t get that message if you’re not calling removeSelf()

i.e. You can’t get an error message for a function you’re not calling.
 
Note: You will also get this message from widget.* objects if handled incorrectly.

Try grepping your project for removeSelf

If you don’t know what grepping is, google grep.

If you’re on a Windows machine a great ‘grepping’ tool is ‘Agent Ransack

If you’re on an OS X machine grep is available on the ‘command line’.

  1. Open terminal

2.  cd to folder where main.lua exists in your project

  1. Type this:

    grep -rl “removeSelf” .

Note: grep is case-sensitive, so watch your spelling.

In the director.lua I got three results .

curGroup:removeself()

curGroup:removeself()

fade:removeSelf()

When I run this code display.remove( titleScreenGroup )

The only thing that happens is the scene just builds on the other one and doesn’t reset