Full App & Corona Crash - Removing Object Outside Of Function

I have created a Doodle Jump clone. Periodically crates appear on platforms, hitting one of these crates either gives the user a power-up (shield, high jumps, etc) for 10 seconds, or it asks them a question. Answering the question correctly gives the user a ton of points.

My problem is with the questions. When I ask the question the game works fine. It's when I try to return to the game that problems arise. My code flow is as follows:
1) Play hits crate
2) Randomly select power up or question
3) Question
4) Game is paused
5) Overlay on top of the game gives 'fade' effect
6) Question is asked, 3 buttons appear for the answers
7) User selects an answer
8) If correct points added, if wrong do nothing
9) Overlay disappears
10) Game continues

It's all working up until point 9. At this point the game crashes. By debugging heavily, I've worked out that it's because the app can't access the buttons, overlay rectangle, or text.

I have tried a few things, such as defining the variables as empty strings outside of a function so that they're 'global' within this file. I have also tried using a function within a function - which seems to be a non-starter.

I believe this is all down to variable scope, but I'm not really sure why, or how to fix it. The code is below:
http://pastebin.com/rQ0Xp0v3

btnAnswered() - this is the click event
create() - this is called when a collision with a crate occurs, this starts the whole process off
wrongAnswer() & correctAnswer() - called depending on the answer given
cleanQuestion() - I believe this is where it all falls over, the app thinks btnAnswer1,2,3,textQuestion+roundedRect don't exist

Massive thanks in advanced - this has been driving me crazy.

Try my massive remedy for all my problems. Attach your items to your localGroup right after declaration like so: localGroup.button = button. So whenever you try accessing your button through landGroup, you won't have to worry about scoping issues. Also set your variables to nil after removing them.

views:1556 update:2011/10/29 17:13:10
corona forums © 2003-2011