views:

30

answers:

2

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?

A: 

if you are using flash 10 then there are 7 events per frame:

  1. Event of event type Event.ENTER_FRAME dispatched
  2. Constructor code of children MovieClips is executed
  3. Event of event type Event.FRAME_CONSTRUCTED dispatched
  4. MovieClip frame actions are executed
  5. Frame actions of children MovieClips are executed
  6. Event of event type Event.EXIT_FRAME dispatched
  7. 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

shortstick
Hmm, are you sure about #2? I know for a fact that children are instantiated immediately after instantiating the parent, and constructors should be called at that point (or they wouldn't be constructors). Anyway, I need to create the movie clip, extract some stuff out of it and then destroy it, so I guess I'll have to put the init code in the constructors.
Bart van Heukelom
sure thing, but the children are instanced as part of the initialisation part of the parent movieclip, therefore in the same "event", rather than when the frame renders or the movieclip frame actions are called etc.
shortstick
Yeah, but the initialisation of movieclips doesn't happen at a specific point in the frame loop, it happens right when you construct them (I've tested this now and I got my code to work by putting it in the constructor)
Bart van Heukelom
the initialisation of movieclips is classified under #2:Constructor code of children MovieClips is executed
shortstick
A: 

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.

Greg
Thanks for trying to help, but I'm completely aware of classes and instances, having been programming for more than 6 years now :PThe frame script is not executed on the root timeline, but on the movieclip timeline, so `this` refers to the newly created instance.
Bart van Heukelom