Reputation: 327
Say we have a flash file, and all the code and objects on the file are described below:
Scene One:
only one symbol with the instance name "char" is on the stage
Code for Scene One:
import flash.events.Event;
stage.addEventListener(Event.ENTER_FRAME,update);
function update(e:Event){
trace(char);
}
play();
Scene Two:
only one symbol with the instance name "char" is on the stage
Code for Scene Two:
stop();
If you try this out yourself, you will find that the Flash traces the object char to be "null" for a split moment, and then traces it properly once it has discovered the symbol on the second scene. Does anyone know why this is, since there is a symbol with the instance name "char" on both consecutive scenes?
Upvotes: 0
Views: 143
Reputation: 2554
I could not find any specific documentation related to this other than that which is provided by Adobe for the Event class, but I believe what you are experiencing is related to the differences between the events Event.ENTER_FRAME
and Event.EXIT_FRAME
and how the Flash runtime initlializes objects for use.
I ran a test using your code with Event.ENTER_FRAME
, and experienced the same results you encountered; however, when I used the Event.EXIT_FRAME
event, the display object did NOT trace as null at all.
Then I took it a step further and set up my timeline exactly as yours; however, changed the event code in Scene 1, Frame 1 to be:
import flash.events.Event;
import flash.display.MovieClip;
stage.addEventListener(Event.ENTER_FRAME,update);
stage.addEventListener(Event.EXIT_FRAME,update);
function update(e:Event){
if( e.type == Event.ENTER_FRAME ) {
trace("ENTER FRAME: " + currentScene.name);
}
else if( e.type == Event.EXIT_FRAME ) {
trace("EXIT FRAME: " + currentScene.name);
}
trace(char);
}
play();
And when executed noticed something interesting:
EXIT FRAME: Scene 1
[object MovieClip]
ENTER FRAME: Scene 2
null
EXIT FRAME: Scene 2
[object MovieClip]
ENTER FRAME: Scene 2
[object MovieClip]
EXIT FRAME: Scene 2
[object MovieClip]
...
Event.ENTER_FRAME
event was never called on Scene 1. Probably because that event had already occurred prior to the code on Scene 1, Frame 1 being executed. null
reference was actually in relation to the char
instance not yet initialized on Scene 2. Once the playhead was exiting the frame, probably when the instance was able to be referenced, it read as a MovieClip.These behaviors are [probably] the reason why so many people recommend using a document class to add objects to the stage as necessary, attaching listeners to the Event.ADDED_TO_STAGE
to know when they were added, so that you can handle functionality at the proper point in time that they are actually added to the stage; instead of waiting for the object to be able to be referenced via the timeline. My best guess is that if the ENTER_FRAME event had fired on Scene 1, it too probably would have traced null
for char
, just like it traced null
on Scene 2. It may be null
because the display object on the stage isn't yet initialized and so the code reference to that object isn't yet initialized either.
I wish I had more time to afford to investigating this for you, but this is the best test and explanation I could come up with to describe the behavior you are experiencing.
Cheers!
Upvotes: 1