Function keeps executing after exiting screen (storyboard)

Hello,
I got a problem with one of my functions. I am using audio in this function and the onComplete option.

When someone answers a question an audiofile plays that says good job or sorry wrong answer. I use onComplete to go to the next question.
The thing is if I exit the scene before the audio finishes, it still wants to go to the function I told it to go to when it finishes.
The problem is the function creates an image and it inserts the image in a group.
Somehow, the function gets called the image does not get created (storyboard clears all the display objects for you when you exit the scene) But it still wants to insert the image in the group. Which is not possible cause it has been removed.

But leaving the page before the audio ends gives this error
bad argument #-1 to ‘_newImageRect’ (Proxy expected, got nil)

Thuis is what I do.

questions[1] = {question = "pics/appel.png", answer = Aa,audioFile="audio/a.aac"}  
theQuestion=questions[i].question  
uppercaseLetter=display.newImageRect(group,theQuestion,241,256)  

Any ideas?
What I do now is just disable the exit button so people can’t leave the page before the audio finishes. But that is crude. [import]uid: 100901 topic_id: 23052 reply_id: 323052[/import]

Actually I don’t think that’s crude at all. It’s reasonable to keep the app in a certain state until it can properly exit.

But what you can do is have a flag variable, declared at the top:

local hasExitedScene = false.  

In your enterScene() function (in case you come back without reloading the module)

hasExitedScene = false  

Then in your exitScene() function:

hasExitedScene = true  

Then in your onComplete function:

if hasExitedScene then  
 return true  
end  

That will prevent your onComplete handler from actually doing anything. [import]uid: 19626 topic_id: 23052 reply_id: 92167[/import]

Thanks for the tip. [import]uid: 100901 topic_id: 23052 reply_id: 92399[/import]

It seems to be working. No crashes so far. Thanks.
Not that I doubted you :slight_smile: [import]uid: 100901 topic_id: 23052 reply_id: 94487[/import]