PDA

View Full Version : The way of the MovieClip


Darwin226
03-27-2008, 09:21 PM
I thought about how to explain the problem but failed every time so I'll just give you the code I would write in AS2.0 and ask you to translate it.

AS2.0:
for (i=1;i<50;i++){
duplicateMovieClip(original,"inst" add i,i)
this["inst" add i].onEnterFrame= function (){
this._y++
}
}

Things I know:
- I need to export the movieClip I make (and want to duplicate) for actionscript and making it into a class or something like that :confused:. Well never mind, I know how to do that.
- I need to be able to add custom names to duplicated movies as well as giving them their own onEnterFrame functions.

Help would be greatly appreciated.

allenrabinovich
03-27-2008, 09:55 PM
Things I know:
- I need to export the movieClip I make (and want to duplicate) for actionscript and making it into a class or something like that :confused:. Well never mind, I know how to do that.
- I need to be able to add custom names to duplicated movies as well as giving them their own onEnterFrame functions.

Help would be greatly appreciated.

Let's say you have a MovieClip in the library that's exported for AS as class called Circle.

So every time you want to create a new copy of Circle, you would:

1. Create a new instance of circle:

var newinstance:Circle = new Circle();

2. You could give it a name if you want to find it by name later:

newinstance.name = "myCircle";

3. You need to add it to the display list so it shows up:

this.addChild(newinstance);

4. If you want to add a custom onEnterFrame listener to it, you would say:

newinstance.addEventListener(Event.ENTER_FRAME, onEnterFrameFunction);

And then define the onEnterFrameFunction:

function onEnterFrameFunction (evt:Event) {

do something;...

}

Oh, and to find it by name later, you can call --

this.getChildByName("myCircle")

That would return the pointer to the specific instance of Circle that has that name -- as long as it's in the display list of *this* (which could be your top level movie clip, or another one -- depends on where this code goes.)

So your code will now look approximately like this:


for (var i:int = 0; i < 50; i++) {
var mymovie:Circle = new Circle();
mymovie.name = "inst" + i;
function onEnterFrame(evt:Event) : void {
evt.target.y++;
}
mymovie.addEventListener(Event.ENTER_FRAME, onEnterFrame);
addChild(mymovie);
}

}

Does that answer all your questions? :)