As3: Class Id - Attachmovieclip

5,681 Views | 6 Replies
New Topic Respond to this Topic

As3: Class Id - Attachmovieclip 2007-05-04 13:19:46

In AS3, there are no "linkage ids" for your movieclips.
But that doesn't mean that you can't attach MovieClips (or Sprites) anymore.
Instead, you have to use a "Class id;" to set it, just go to your library, right-click on the object you are interested in and click on "linkage," tick the "export to actionscript" box and where it says
Class" just insert the name of the class (it might be a bit different with flash CS3)... You can set it to anything you like, but by convention, class names are capitalined as such: MyClass, TheBestMovieClipEver... etc...

Now, let's say that you set your movieclip's class to "MyClass."
In the frame action bar, to attack your movieclip, just write this:

var mymc:MyClass = new MyClass();

if you know much about classes; MyClass is basically a sub-class of movieclip/sprite, which means that it inherits all its properties and methods. You don't actually see it, but when you run your application, flash internally creates a class named "MyClass" which extends either Sprite or MovieClip... Now if you create an external .as class file named "MyClass," you can alter the standard implementation of "MyClass" by adding methods and properties as you like, but be sure it either extends Sprite or MovieClip or maybe even SimpleButton depending on what you set it to be.

Ok, so once you created an instance of "MyClass," you can use it like a MovieClip/Sprite/whateverdisplayobjectyouchos e... You can also store them in an Array like this:

// creates 10 instances of MyClass

var movieclips:Array = [];
for(var i:int=0; i<10; i++) {
movieclips.push(new MyClass());

and you can call them by diectly invoking the appropriate index var of the array.

So; "movieclips[0]" would call the first movieclip that was stored in the "movieclips" Array.

There are not duplicateMovieClip functions in AS3, so you'll have to create new instances like I showed you... If you need to copy a DisplayObject's properties, you'll just have to do the extra work. Just figure out which properties you would like to apply to the duplicate DisplayObject and put together a function to deal with it.

Hope that helped :)


Response to As3: Class Id - Attachmovieclip 2007-05-04 13:27:50

Oh, and if you want to actually display the class.
Just go (following from the array example):


You could just set up a for loop to attach them iteratively.
Also, please note that you don't need to have "stage." at the start; this is just to let you know that you are in fact adding the child to the stage. If you do not include the "stage." at the start, it will still add the mc to the stage because AS3 automatically sets the scope of a method to the class in which it is invoked in unless otherwise specified.


Response to As3: Class Id - Attachmovieclip 2007-05-04 13:28:51

At 5/4/07 01:27 PM, Alphabit wrote: Oh, and if you want to actually display the class.

hrruugg... Not the class, I meant instances of the class.


Response to As3: Class Id - Attachmovieclip 2007-05-04 13:37:00

interesting... i wondered how to do that in as3, good tut


Response to As3: Class Id - Attachmovieclip 2007-05-04 13:43:35

Yeah cool, I just found this out myself. Posted this in the rant thread, may as well post it here too

import flash.display.*;

for(var i=1;i<100;i++){
mc.graphics.lineStyle(1,0,1); //JUST AN EXAMPLE OF HOW TO DRAW API
mc.graphics.moveTo(0,0); //WITHIN THE MC
mc.graphics.lineTo(0,-50); //IF YOU WANT TO
mc.name="xh"+i; //GIVE IT A NAME
mc.x=Math.random() *stage.stageWidth; //RANDOM X
mc.y=Math.random() *stage.stageHeight; //RANDOM Y
stage.addChild(mc); //PUT IT ON THE STAGE

- - Flash - Music - Images - -

BBS Signature

Response to As3: Class Id - Attachmovieclip 2007-05-11 16:17:19

The 2 lines it works well without are :
import flash.display.*;
var mymc:MyClass = new MyClass();

are they required?


BBS Signature

Response to As3: Class Id - Attachmovieclip 2007-05-29 00:16:14

var movieclips:Array = [];
for(var i:int=0; i<60; i++) {
movieclips.push(new down());

All this code works, but I'm trying to understand the logic and it seems like it should cause an error because shouldn't it read clip.movieclips[i] as opposed to just movieclips[i].x and .name?

I'm thinking it's something to do with classes in general, or maybe I'm just an idiot :P