When is Flash frame script executed exactly?
I have a movieclip which has in the actions for frame 1
开发者_运维知识库this["myCustomVar"] = "bla";
I then do this:
var mc:MovieClip = new MyMovieClip();
trace(mc.hasOwnProperty("myCustomVar")); // is false
Why does the movieclip not have myCustomVar
, or to put it more generally:
When are frame scripts in movie clips executed exactly?
if you are using flash 10 then there are 7 events per frame:
- Event of event type Event.ENTER_FRAME dispatched
- Constructor code of children MovieClips is executed
- Event of event type Event.FRAME_CONSTRUCTED dispatched
- MovieClip frame actions are executed
- Frame actions of children MovieClips are executed
- Event of event type Event.EXIT_FRAME dispatched
- Event of event type Event.RENDER dispatched
so you can listen to the EXIT_FRAME event, at which point the frame script should have run and the var should be set.
Source
I don't think the timing of frame scripts is really at the heart of your question. In the case of your code snippits above, you're getting into a question of class versus instance. Let's look at this line:
this["myCustomVar"] = "bla";
There, you've defined a new variable called myCustomVar
in the root timeline instance. An instance is a single thing which exists individually and can be customized. However, customizing one instance does NOT change the Class that originally defined it. Think of a widget factory: if you take a finished widget from the end of the assembly line and paint it red, that does not mean that the factory will now produce red widgets... you've just altered one widget instance that rolled off the end of the line. In order to make your factory produce red widgets, you'd need to alter the factory itself – or the Class definition. So, I assume you have a custom class written for MyMovieClip
? If not, you'd need to do this in MyMovieClip.as
:
package
{
import flash.display.MovieClip;
public class MyMovieClip extends MovieClip
{
public var myCustomVar:String = "";
public function MyMovieClip():void {
super();
}
}
}
Once you've modified the object's class definition to include your custom variable, then all new instances of that class will be constructed with that variable. Hope that helps.
精彩评论