PDA

View Full Version : Actionscript execution order with attachMovie statements


scc
07-29-2007, 11:23 AM
Hi I wonder if anyone's got any decent solutions to this...

Basically whenever I have a script generating an interface by dynamically placing movie clips (using the attachMovie method), I often need the actionscript of the attached movie clip to execute before the rest of the code in the main script continues.

According to normal actionscript execution order, the current script finishes before 1st frame actionscript of any attached movies is executed, so say for example I had the following in frame 1 of my root timeline:

trace("start");
this.attachmovie("myMovieClip","movieClip1",this.getNextHighestDepth());
trace("end");

and the movie clip in my library with the linkage identifier "myMovieClip" has the following on frame 1 of its timeline:

trace("middle");

Normally this would output:

start
end
middle

However I want it to output:

start
middle
end


The only appropriate way I've found so far to do this is to have a function that is called on completion of the myMovieClip actions, i.e. in the root timeline:

trace("start");
this.attachmovie("myMovieClip","movieClip1",this.getNextHighestDepth());
movieClip1.onLoaded=function() {
trace("end");
}

and in the timeline of the myMovieClip movie clip:

trace("middle");
this.onLoaded();


But the problem with this is that if I rather than the trace("end"); statement I want a series of other commands, some of them in turn using attachMovie methods, again wanting the scripts to execute in the order above, I'm going to end up with a lot of nested onLoaded functions and it's going to end up looking pretty ugly!


Any thoughts?

neilmmm
07-29-2007, 11:57 AM
i don't know if this is a good solution but you could do this;
function secondTrace() {
trace("middle");
}
function thirdTrace() {
trace("end");
}
trace("start");
var mc:MovieClip = attachMovie("myMovieClip", "movieClip1", this.getNextHighestDepth());
mc = secondTrace();
mc = thirdTrace();

or just

trace("start");
var mc:MovieClip = attachMovie("myMovieClip", "movieClip1", this.getNextHighestDepth());
mc = trace("middle");
mc = trace("end");

scc
07-29-2007, 02:06 PM
The problem is that ideally I want the trace("middle"); function (or whatever it would be replaced with in a more complex scenario) to be part of the movie clip in some way, so that every time that movie clip is created the same actions are performed.

What does the mc = secondTrace(); syntax actually do? I haven't come across that before - I presume it calls the function, does it do it within the scope of mc or something?

I've had the idea of defining a class for the movieclip and storing the relevant actions (i.e. trace("middle");) in the constructor of this class (with the class for the relevant symbol in the library set appropriately in the Linkage options), as these actions are then called when the movie clip is attached rather than on frame 1 of the movie clip, which seems to be producing the desired effect, although I'm still going through my code correcting scopes etc... does this sound like a good idea? Or are there any problems you can think of that might arise from this?

noahgenius
08-06-2007, 07:20 AM
I've had the idea of defining a class for the movieclip and storing the relevant actions (i.e. trace("middle");) in the constructor of this class (with the class for the relevant symbol in the library set appropriately in the Linkage options), as these actions are then called when the movie clip is attached rather than on frame 1 of the movie clip, which seems to be producing the desired effect, although I'm still going through my code correcting scopes etc... does this sound like a good idea? Or are there any problems you can think of that might arise from this?

yes, and i think it's the only solution to do that because attachMovie is equal to build a mc instance and it will not return until constructor finishes.
If you write codes in mc's timeline, it will run later, maybe according to the priority of thread(maybe run behind flash;) )