views:

2912

answers:

3

I have a movie clip that is attached with attachMovieClip, and it has a function in it called test()

///

function test()
{
   trace('!');
}

after attaching the movie clip i was trying to call that function, but I simply couldn't. Is there a simple solution for this? I've read something about you you cannot call the function until the clip is fully loaded... is there a simple solution for this?

A: 

Make sure your function "test" is defined on the first frame of the MovieClip timeline. Then use the MovieClip.onLoad handler to capture when the MovieClip is ready to receive function calls. After that you should be able to call "test" on your clip.

Good luck!

seedpod
+1  A: 

You could define the function outside the movieclip, in the main Timeline. Then, attaching it (via attachMovie), you can pass it through the initObject (4th parameter):

function test(p) {
    trace("called with "+p);
}
this.attachMovie("lib_clip","clip_mc",3,{func:test, mp:"my parameter"});
clip_mc._x = 100;
clip_mc._y = 100;
clip_mc.onRelease = function() {
    this.func(this.mp);
};

Vyger

A: 

It has been a while since I used AS2, as AS3 rocks, but I believe that you need to give an instance ID to the attached movie clip. And you also need to wait until the movie is ready.

//MovieClip with function hello() in it.
hello():Void
{
    trace('Hello world!');
}

//Root timeline of main movie.
container.attachMovieClip("nameInLibraryToAttach", "instanceID", depth);

//The following will not trace because it happens to soon.
container.instanceID.hello();

//The following works
var runOnceNumber:Number = 0;
var interval:Number = setInterval(someFunction, 1000);

someFunction():Void
{
    container.instanceID.hello();
    runOnceNumber++;

    clearInterval(interval);
}

What you need to do is either setup the MovieClip with the embedded function to broadcast an event when ready and to listen to it from the main, or something like setInterval which only probably needs one tick. I tested it with setInterval and it worked first go. Again you need to wait until it is "INITIALIZED" meaning all of its code on frame 1 is loaded and available. Consider actionscript 3 because with its event driven flow, things like this are very very easy.

Brian Hodge
hodgedev.com blog.hodgedev.com

Brian Hodge