Function not defined immediately after create MovieClip instance

Hello kirupa flashers,

I create a new MovieClip, call it Somehow, and link it to the name “MyClip”, for example.
Inside of the Clip, there must be a Function like this:

 
this.sayHello = function() { return("Hello"); } 

(In fact, my script should do different things, but this should be enough at first :slight_smile:

In my root frame, i do something like that:

 
attachMovie("MyClip", "MyClip01", 1);
trace(MyClip01);
trace(MyClip01.sayHello());

For the first trace i get “_level0.MyClip01”, which is correct.
But i cant call the function sayHello(), it says: “Undefined”.
And when i put the last line in the second frame, or as a button action, it is working, but
this is not what i want.

The reason is, that i get a big struct object where all the information is stored about
the Clips i have to create and how to initialize them and so on.

It LOOKS LIKE this:


for(i=1; i<=100; i++) {
 attachMovie("MyClip", "MyClip"+i, i);
 theClip = eval("MyClip"+ i);
 theClip.tellSomething(i*i);
}

They must be MovieClips, because the functions not only response with hello, like in the example.

So I want to create the Clip copies and call the functions in one step. This example is also not working when i put it inside of a onLoad, or onEnterFrame and so on…

And ideas? I doubt that its so difficult…

Eclipse2k

you’ll just need to wait. The (complete) script you call attachMovie in happens before the first frame of the movie attached. This means nothing within that first frame is accessible from the script attaching it.

What you might want to do is just make the movieclip a class in oop and have what you need created as a method of that class (or even more simply just make it a movieclip prototype function) - that way, you will be guaranteed its availability directly after the attachMovie call.

Well, thanks so far! I am interested in the second method. I am java programmer for years (haha maybe this is the reason why i just dont understand why methods are not available after creating an instance of the object) but i am new to flash.

Could you tell me a basic example (just with example functions like mine) or any good web source for it?

Its essential that my Object will have all MovieClip functions, and it should be able to attach another movies as well and also pass functions directly.

just imagine the following case (its similar to mine):
My Root Movie is a “Tree” (yes, that green one, nothing with folders :slight_smile: ) and i create some “Branch” Objects in a loop, and each “Branch” Object must be a MovieClip displaying the branch itself but has also the functions “createLeafs()” and “createBranches()”.

If i would need to wait at least one frame for each level of objects i could never get the whole tree in one step.

well, its not really waiting a frame, but waiting until after the script finishes. The first frame of the movieclip still plays in that frame, just completely after all of the timeline script completes. So the first frame of the attached movieclip runs before a frame refresh, just after the frame script that attached it thereby making definitions within that movieclips first frame unaccessible by that frame script.

A simple solution (though a little hacked) is running a callback defined in the destination timeline’s script from the first frame of the movieclip attached. This means you can define what you want to happen prior or directly after the attachMovie and have it run after the first frame of the movie attached (called from the bottom of the first frame script of that clip). For example:

//Actionscript:
// 1st frame of "sq" symbol:
foo = 10;
bar = "ten";
attach_callback();

// main timeline:
function retrieve(){
	trace(this.foo);
	trace(this.bar);
}
clip = this.attachMovie("sq","sq",1);
clip.attach_callback =  retrieve;
/* traces:
10
ten
*/

Above, you can see that the retrieve function (defined as attach_callback in the attached clip) runs based on the its definition in that script. Mind you, this is still the same frame in Flash, just after the rest of that script has completed.

Now, the “better” way is through using an associated class with the movieclip and just using methods/properties there. That way, all methods will be immediately accessible from the same script used to attach the clip. For more on that, see the oop tutorials here:

http://www.kirupa.com/developer/oop/index.htm (basic foundation [using AS 1.0])
http://www.kirupa.com/developer/oop2/AS2OOPindex.htm (AS 2.0)

^ just skip ahead to the movieclip portion. The thing to take note of is script execution order (touched upon here) and that the constructor of movieclips associated with custom classes 1) do not use the new keyword to be created - they are instantiated solely through attachMovie and 2) the constructor of such classes is not called with any arguments, hense the necessity of the init method.

You can probably work out things from there, if not, ask and we can help :slight_smile:

Hello senocular,

Wow, thanks, that should be enough to solve the problem. Just opened the AS 2.0 OOP Section and Inheritance on MovieClips (Bouncing Balls) and i am impressed! (this one). You wrote that! :slight_smile: Congratulations! Looks like that would be exactly what i want to do, becauseh the callback functions might be much easier but they muddle the running sequence completely. (I used similar callback methods to synchronize web sites in frames and such things in javascript…)

Gonna take some time now to write some Classes (since that is something i know already from Java and it looks quite similar). Ok, the visitors will need the newest version of the Flash Plugin i guess (because AS2?) but i am quite sure now that i wont go back anymore.

Thanks again for the very qualified help, i really appreciate it :thumb:

Yeah, AS 2.0 in Flash MX 2004 was designed for people like you :wink: However, you can use Flash Player 6 in movies that use AS 2.0 just so long as you dont use methods and objects which are available to 7 only - you’ll get a little more detail when you set this in Flash preferences (using AS 2.0 and exporting to Flash 6). Nevertheless, it is preferable to use 7 (it gets people updated and its actually much faster than 6).