Object:removeself()

Hello! Before the function:scene:createScene (event) I added a function that removes an object when it is touched.

function l82\_2( self, event )     if event.phase == "began" then         clickover2 = audio.play( click2 )         aa=1         l82:removeSelf( ) storyboard.gotoScene( "scene10", "crossFade", 75  )         end         return true end function scene:createScene( event )     local screenGroup = self.view l82 = display.newImage("level4\_82.png") l82.x = display.contentWidth / 1.9 l82.y = display.contentHeight / 1.9 screenGroup:insert( l82 ) l82.touch = l82\_2 l82:addEventListener( "touch", l82 ) end  

After the image is pressed it is removed and storyboard goes to next scene; however if I try to come back from a scene to this scene if I press again the image I get the following error:

attempt to call method ‘removeSelf’ <a nil value>

Thank you.

If you don’t purge the scene on exit, the method “createScene” will not be called. Try to include those statements in “enterScene” or purging the scene on exit.

Hello! I am using storyboard.purgeAll() inside the enterScene of each scene.

Mmmm. Did you tried to remove the touch event listener? If you are using globals maybe it is still running when you enter again in the scene… Try to remove it or using a print in the “began” event, just to check how many events are triggered when you touch.

storyboard.purgeAll() will not purge the current scene, which is the one you need purged.  What might be the best practice is if you know you’re going to a scene named “level1” to do a storyboard.purgeScene(“levell1”) just before you do the storyboard.gotoScene(“level1”). 

That way you’re not purging excessive scenes, you’re targeting the one you are going to.  Be aware that if you create any varaibles outside of the scene functions, i.e.

[lua]

   local x = 10

   function scene:createScene(event)

      …

   end

[/lua]

And something changes the value of x during the scene’s run, it will still be that last value when you return regardless of if you purge or not.  You have to remove the scene to reset those variables defined in the main chunk.  You can create them outside of the createScene() type functions for scope purposes, but you should initialize them inside of createScene() to make sure they reset properly.

If you don’t purge the scene on exit, the method “createScene” will not be called. Try to include those statements in “enterScene” or purging the scene on exit.

Hello! I am using storyboard.purgeAll() inside the enterScene of each scene.

Mmmm. Did you tried to remove the touch event listener? If you are using globals maybe it is still running when you enter again in the scene… Try to remove it or using a print in the “began” event, just to check how many events are triggered when you touch.

storyboard.purgeAll() will not purge the current scene, which is the one you need purged.  What might be the best practice is if you know you’re going to a scene named “level1” to do a storyboard.purgeScene(“levell1”) just before you do the storyboard.gotoScene(“level1”). 

That way you’re not purging excessive scenes, you’re targeting the one you are going to.  Be aware that if you create any varaibles outside of the scene functions, i.e.

[lua]

   local x = 10

   function scene:createScene(event)

      …

   end

[/lua]

And something changes the value of x during the scene’s run, it will still be that last value when you return regardless of if you purge or not.  You have to remove the scene to reset those variables defined in the main chunk.  You can create them outside of the createScene() type functions for scope purposes, but you should initialize them inside of createScene() to make sure they reset properly.