attachMovie doesn’t return a value… in the Twilight Zone

Ok, it must be because I’m down to the wire on this project, but after the 2nd time of having attachMovie not return a value in one of my functions, I just couldn’t take it anymore, and had to solve it.

And I did, MOFO!

…I just don’t understand the results.

My problem:
– attachMovie was not returning a value
– tracing the function call printed the movie clip path in the Output Window
– tracing it with a string in the trace statement printed a NaN in the Output Window; trace(“ref_mc: ” + attachMovie(“MyComponent”, “my_mc”, 0));
– utilizing typeof and instanceof showed undefined in the Output window
– any return value showed undefined

…uh, no, that’s not how attachMovie works. It doesn’t attach a component and then have that component magically work, but not return a value. If I accessed the movie clip, however, it worked. Now, a RAD type of developer would of moved on, simply replacing ref_mc with a pointer to the movie clip’s real name.

Personally, I don’t like shiot breaking and not knowing why. So, after investigation, it turns out that placing a “stop();” in an AS2 constructor/init function will cause the return value to fail. this.stop, using a stop in AS1, or this.stop in AS1 are all fine.

Uh, yeah…ok… sure, that makes tons of sense. :: dar dar dar :: * hits right wrist on chest heavily *

This is important in designing AS2 components because you now place your component’s assets on frame 2 on a non-guided frame. You now have to add a stop to prevent your component from ever reaching that frame since it’s merely there to ensure your component includes all it needs in either travelling to another FLA’s library, or transforming into an SWC. You can place a “stop” command on the timeline on frame 1 if you wish, but most purists don’t believe in any timeline code if possible. Putting it on the constructor/init function is best.

8 Replies to “attachMovie doesn’t return a value… in the Twilight Zone”

  1. putting it on the constructor is best ? hmmm, since that stop is of a ‘timeline’ necessity, why would you mix that with your component’s *init/constructor* code ?

    this is flash … and timeline in flash is still cool ….

    *stares at the evening sky …*


  2. If you put a “stop();” command on frame 1, layer 1, and your component init code on frame 1, layer 2, your component won’t work. Something about “stop” causes your component to not initialize. It doesn’t do this in 2004, so your comment is valid.

    Again, though, the ability to no longer need the timeline is what most developers want; me, I’m fine with it, but since I try to develop code that people can learn from, I try… seriously, I do, I try to follow practices that aim at the best ways to code in Flash, patterns non-withstanding.

  3. “If you put a “stop();” command on frame 1, layer 1, and your component init code on frame 1, layer 2, your component won’t work.”
    doesn’t it has something related to the ‘compile’ order ? (top down, bottom up) ? MX wise…

    whooops! sorry! didn’t want to be aggressive … just that I don’t view Flash as ‘encouraging’ best practices … sometimes, I feel that flash is my demon .. ;)

    I know that mixin code design and timeline is sometimes of a miracle ….

    lame question: would a pattern defining UI implementation be any useful ?

  4. Maybe your right about the top down/bottom up… didn’t check that. Hrm…

    Naw, I don’t think your being aggressive. Hehe, Flash practice is unique in it’s own right; I meant strictly the coding practices I espouse, not Flash itself. I agree, it is a miracle it works sometimes.

    …depends on the pattern. The boyz I used to work with upstairs are all about patterns; so far, I get away with minimal MVC usage as far as coding goes; UI wise, I’m not sure. Every product it’s different, but I may be mis-interpreting your question.

  5. no you aren’t….. I was wondering in an open-way…

    * hmmmm, …, a FLASH , hmmm, a FLASH pattern *

    which wouldn’t be a pattern, just a Flattern !


  6. IMHO this has something to do with the stage draw. A stop() is a method of the timeline and constructor code precedes the timeline, so it makes sense that this cause a hang. How about addingto your construction: this.onload = function(){stop();}

  7. The problem with that is if you expect to do this:

    attachMovie(“MyComponent”, “my_mc”, 0);;

    It won’t work… the onLoad happens after the above code; a frame after. This causes problems if your expecting to be able to go forward a frame.

    …however, IF you know the onLoad executes a frame after, and your methods that cause it to change state can handle an onEnterFrame + execute + kill onEnterFrame, then onLoad works great.

    …still, I just wanna know why. I personally think it’s some sort of scopage issue in AS2 that I’m not understanding. One thing I liked about AS2 was not using “this” everywhere, so I just assumed stop would work too, but obviously not.

  8. “this” is not required for scope, but there’s no avoiding its use sometimes when you need to attach a timeline method.

    AS1 or AS2, the code always precedes the stage draw, so if you think about it, that attached movie isn’t really there until it is loaded and initialized.

    I’ve had a similar dilemma in the past and resolved it by nesting all the execution code in an onEnterFrame event with a conditional stopgap:

    var mc = attachMovie(“box”,”my_mc”,1);

    mc.onEnterFrame = function() {

    if ( !init ) {

    this._rotation = 45;
    init = true;

    } else {

    this.onEnterFrame = null;

Comments are closed.