Monday, April 22, 2013

Proper "Clean Up" after Swapping Scenes

Now that I have some control buttons at the top of the frame I can experiment with jumping forward/backward between scenes...

And I found some problems.

And fixed them.

The two main problems: First, when you jump BACK into a scene that you already left, the scene comes back JUST AS YOU LEFT IT the first time.... This mean that, when I jump back into the "log-in" screen it was already displaying "Logging In, Please Wait" and other problems occurred because of it. Second, after I left the first scene there seemed to be some left-over code running in the background. (When I pressed the "Enter" key while in Scene One it ACTED like I was STILL in Log In scene, and it let me log in again!!!??? The button_press event listeners from the PREVIOUS scene were still running during the next scene.)

Solutions...

First, in the main scene-handler program I made some modifications to allow the student/user to move forward and backward between scenes.

Here is the code that moves the scene FORWARD:
The above code will advance scenes from "LogIn" to "Scene One". It removes the Log In scene, then sets it to null (which basically deletes it... However, evenlisteners keep running). Then I make a new Scene One object, and place it on the stage. (It then sets _sceneOnStage to 1 to let "everyone" know that Scene One is on the stage) 

Here is the code that moves the scene BACKWARDS:

The above code checks to see which scene is on stage (scene one) then it removes it, deletes it (null) and makes a NEW "Log In" scene then adds it to the stage. It then sets _sceneOnStage to zero once again. (This code is run when the student presses the "Back" button.

Second, I added some code in each scene to DELETE EVENTLISTENERS whenever the given scene is removed from the stage.

The above code is added to each scene to listen for the time when the scene is removed from the stage. It then runs the "removeFromStageHandler" function:

The function above will be the last function ran in the given stage. It is triggered by the REMOVED_FROM_STAGE event. In this example the KEY_UP and KEY_DOWN KeyboardEventListeners are removed. Then the REMOVED_FROM_STAGE event listener itself is removed. THIS KEEPS THE BUTTON PRESSING EVENTS FROM ONE SCENE FROM EFFECTING THE NEW SCENE. (Before I did this the student was able to hit "ENTER" to re-log in WHILE he was already into the next scene... The event listener was running in the background because I had not deleted it.)

I added similar "removeEventListener" code to other objects, like the Button objects that each have mouse event listeners on them. (There is no reason for the computer to keep listening for mouse events on buttons that were DELETED because the student has moved on to a new scene!)

NOTE: In the final versions of these interactive tutorials the student will NOT ACTUALLY be allowed to "back" into the log-in screen again. The furthest back they could go is Scene One. I am only doing this right now to see HOW to make this happen. I figured that if I can do it with the Log In screen I can probably do it with ANY scene that I create in the future.

Time: 2 hours

No comments:

Post a Comment