Here's a really frustrating Actionscript 2 problem I have with movie clips associated with classes that are created on the timeline and need to be referenced in code right away:
-- I have a movie clip "C" in my library associated with a class "C".
-- Class "C" extends MovieClip.
-- I have a movie clip on the stage with frame labels "off" and "on". This movie clip has an instance name of "mc".
-- On the "on" frame has an instance of the class "C" with instance name "inst".
-- The constructor for class "C" includes a trace statement to output "C constructor!" to let me know when the instance on the stage is created.
Now let's say I run this code:
mc.gotoAndPlay("on");
var inst_mc:MovieClip = mc.inst;
if (inst_mc){
trace("inst_mc found!");
}else{
trace("inst_mc NOT FOUND!");
}
var inst_c:C = C(mc.inst);
if (inst_c){
trace("inst_c found!");
}else{
trace("inst_c NOT FOUND!");
}
It seems that the creation of any object under a class such as C will not happen until after all code for the current frame has finished executing, because the output will be this:
inst_mc found!
inst_c NOT FOUND!
C constructor!
What the heck is going on here? I've explicitly told the Flash authoring environment that movie clip C is associated with class C, and that class C is a MovieClip derivative. So in my code, the gotoAndPlay("on") will create the movie clip "inst" which is on the "on" frame. It is able to find the instance alright, but when I treat it as type C, it fails. And then the constructor happens AFTER all of this. How do I fix this? I would hope that once you change something on the timeline, the corresponding objects would be created immediately -- and they are, except not as their explicit class types. I can reference my instance, but only as a MovieClip. How the heck do I fix this? It should output:
C constructor!
inst_mc found!
inst_c found!
Thanks for any help!
* UPDATE * Thanks for the responses! It is unfortunate that there's no easy fix for my project, which is large now and can't be easily restructured (it is also too large to be converted to AS3). I thought about keeping the MCs on frame 1 and hiding them, but I figure that needlessly adds overhead. Even if _visible is set to false, isn't it still going to use up resources? (That's a different but related issue -- is performance any different if you have a complex, stationary MC that is not _visible versus not having it there at all?)
My current strategy goes something like this:
mc.gotoAndPlay("on");
var inst_mc:MovieClip = mc.inst;
var inst_c:C = C(mc.inst);
if (inst_c){
// Even though I moved to the "on" frame,
// the object was already initialized/existed already
// so i can use its class code now
inst_c.do_something_now();
}else{
// The class is not accessible, so set a boolean flag
// which will get dynamically assigned to the *movie clip*.
// The constructor in class C will look to see if the flag
// has already been set. If so, it calls do_something_now()
// within C's constructor.
// In class C, trigger_do_something_now is a defined as a
// Boolean with no default value.
// It is not set in the constructor.
inst_mc.trigger_do_something_now = true;
}
This approach bothers me. It is messy and confusing. But, I think it is a reasonable workaround. What do you guys think? Thanks!