[SOLVED] Spawned Objects and storyboard.removeScene issue

Hey guys,

I started today working with scene transitions, I have a button in my “gameplay” scene, that when pressed, it takes you to the “main menu” scene.

Problem is, I get some errors on the spawned visual objects that try to destroy themselves when transition is complete .

I spawn the objects (in my “gameplay” scene) using a timer like so:

[lua]
  timer.performWithDelay (500, function () – make sparkle particles

        if (powerUpBoosterCount>0) then

            local tmpShine = display.newImageRect(“images/ShineParticle.png”, 32, 16)

            groupHUD:insert(tmpShine)

            tmpShine.x = 80

            tmpShine.y = 200

            transition.to (tmpShine, {time=2000, alpha=0,

                            onComplete = function(target) target:removeSelf(); end} ) 

        end

  end , 0)
[/lua]

Now when I press the button it takes me to the main menu scene with this command:

[lua] storyboard.gotoScene(“initScene” , {time=400 , effect = “fromLeft”}) [/lua]

And I want to remove the gameplay scene entirely so I have this set up (in the “gameplay” scene):

[lua]
function scene:didExitScene(e)

    storyboard.removeScene(“gameplay”)

end

scene:addEventListener(“didExitScene” , scene)

[/lua]

Problem is im getting an error :

“gameplay.lua:1034: attempt to call method ‘removeSelf’ (a nil value)”

Meaning the sparkle object that was spawned is trying to remove itself, but I guess there is nothing to remove… Any ideas how to handle this?

Roy.

Your timer function is still being called even after the scene ends. You can stick a clause in there to cancel the timer when the scene ends.

FWIW creating objects explicitly inside of a timer function isn’t the best idea. You might want to create a spawning function, and call that function on a timer. Just my two cents.

EDIT:  Problem solved

Hey @Panc software, 

Do you mind explain what do you mean by “stick a clause in there to cancel the timer” 

What I did is I made the spawning function out of the timer and I call it from the timer like you suggested, Also ive added  timer.cancel() to cancel the timer on the “didExitScene”

But Im still getting this error:

gameplay.lua:1061: attempt to call method ‘removeSelf’ (a nil value)

Please help…

Roy.

Your timer function is still being called even after the scene ends. You can stick a clause in there to cancel the timer when the scene ends.

FWIW creating objects explicitly inside of a timer function isn’t the best idea. You might want to create a spawning function, and call that function on a timer. Just my two cents.

EDIT:  Problem solved

Hey @Panc software, 

Do you mind explain what do you mean by “stick a clause in there to cancel the timer” 

What I did is I made the spawning function out of the timer and I call it from the timer like you suggested, Also ive added  timer.cancel() to cancel the timer on the “didExitScene”

But Im still getting this error:

gameplay.lua:1061: attempt to call method ‘removeSelf’ (a nil value)

Please help…

Roy.