Scene Change Conundrum

Hello friends, I have a bug I can’t seem to work out.

local img = scene:newPortrait() self.view:insert(img) local destX = display.contentWidth if (img.direction == "left") then destX = -258 end local function finished() print("Finished animating") if (img ~= nil) then print("Image: " .. img.direction) img:removeSelf() img = nil end end transition.to( img, { time=math.random(2000,5000), x=destX, onComplete=finished } )

The code above is supposed to move an image in a specified direction. This works fine. However, a few seconds after I switch scenes, an error is thrown because of the img:removeSelf() bit. The console says that there is an error because the attempt to call removeSelf failed. Clearly, the display object of the portrait that I attached to the scene’s view was purged upon the scene change. However, for some reason, the reference to img itself was not destroyed, only the actual display image was removed.’

Can someone suggest a method to fix this bug? I’m thinking that I might have to stop the onComplete listener, but I don’t know how to do that.

Nevermind, I was being silly. I just had to add a transition.cancel() when the scene exited

function scene:didExitScene(inEvent) storyboard.removeScene("mainMenuScene") transition.cancel() --ADD ME timer.cancel(mainMenuScene.animator) print("cancelled animator") --utils:log("gameScene", "didExitScene()"); end -- End didExitScene().

Nevermind, I was being silly. I just had to add a transition.cancel() when the scene exited

function scene:didExitScene(inEvent) storyboard.removeScene("mainMenuScene") transition.cancel() --ADD ME timer.cancel(mainMenuScene.animator) print("cancelled animator") --utils:log("gameScene", "didExitScene()"); end -- End didExitScene().