Categories
AS3 AS3 migration Flash

From AS2 to AS3 – Where did it go – attachMovie

Where did attachMovie go in AS3?

Once I started using attachMovie instead duplicateMovieClip my AS2 life became a lot easier.

What has the ActionScript 2.0 Migration to say about this subject:

ActionScript 2.0  ActionScript 3.0  Comments
attachMovie() Method Removed In ActionScript 3.0, use addChild() to add child display objects.

Removed… wtf? and you can read the documentation till you are old and gray, but you won’t find a answer there.

In AS2 I know 3 ways to attach a movie to the stage:

AS2

For this example you need a movieClip in the library (right-click >> Linkage… >> activate ‘Export for ActionScript’) with the Indentifier ‘ImageMovie‘ and this AS2 code placed in the root

I used the name ‘ImageMovie‘ because it also use in an example from Brendan Dawes that I use in AS3 examples. And to keep the movieClip the same through all code I kept the name.

example #1
[as]// #1
this.attachMovie(‘ImageMovie’, ‘ImageMovie2’ , 10);
[/as]

example #2
[as]// #2
var myImageMovie:MovieClip = this.attachMovie(‘ImageMovie’, ‘ImageMovie2’ , 10);
myImageMovie._x = 20;
myImageMovie._y = 20;
[/as]

example #3 (my personal favorite)
[as]// #3
this.attachMovie(‘ImageMovie’, ‘ImageMovie2’ , 10 , {_x:20,_y:20});
[/as]

Oke, now in AS3:

AS3

For this example you need a movieClip in the library (right-click >> Linkage… >> activate ‘Export for ActionScript’) with the Indentifier ‘ImageMovie‘ and this AS3 code placed in the root

example #1
[as]// #1
this.addChild(new ImageMovie());
[/as]

example #2
[as]// #2
var myImageMovie = this.addChild(new ImageMovie());
myImageMovie.x = 20;
myImageMovie.y = 20;
[/as]

Update #1: I’m using this a lot these days, so I will let you see how I use it now

example #2a
[as]// #2a
var myImageMovie:ImageMovie = new ImageMovie();
myImageMovie.x = 20;
myImageMovie.y = 20;
this.addChild (myImageMovie);
[/as]

example #3
This code is a copy of the code from brendandawes.com and you should visit as3 for lazy bastards (like me)

you need a AS file with the name “ImageMovie.as” with this code in there
[as]
// #3
package {

import flash.display.MovieClip;

public class ImageMovie extends MovieClip {

function ImageMovie(xPos:Number = 0,yPos:Number = 0) {
this.x = xPos;
this.y = yPos;
}
}
}
[/as]
and you need this code in the timeline, because the movieClip in the Library is linked to this AS file you can talk to it directly:
[as]
/*
How to “attachMovie” in as3
*/
var imageMC:ImageMovie = new ImageMovie(50,50);
this.addChild(imageMC);
[/as]
and the it will work the same as option 3 in AS2

Here you can find another explanation about this subject: by Airtight Interactive

I hope this will clear up some issues you have, happy coding 😉

6 replies on “From AS2 to AS3 – Where did it go – attachMovie”

How can I make more duplicate in the same time? How to use with an “i” variable?

for (var i:int = 0; i <= 4; i++) {

// who can fix like nmyImageMovie+i

}

myImageMovie1:ImageMovie = new ImageMovie();
myImageMovie2:ImageMovie = new ImageMovie();
myImageMovie3:ImageMovie = new ImageMovie();
myImageMovie1.x = 40;
myImageMovie1.y = 20;
myImageMovie2.x = 80;
myImageMovie2.y = 20;
myImageMovie3.x = 120;
myImageMovie3.y = 20;
this.addChild(myImageMovie1);
this.addChild(myImageMovie2);
this.addChild(myImageMovie3);

// myImageMovie1.number.text = i;
// this is not working only if I add 1, 2 or 3

You mean something like this?

for (var i:int = 0; i <= 4; i++) { var myImageMovie:ImageMovie = new ImageMovie(); myImageMovie.x = 40 * i; myImageMovie.y = 20; myImageMovie.number.text = i; this.addChild(myImageMovie); }

“for (var i:int = 0; i < = 4; i++) {
var myImageMovie:ImageMovie = new ImageMovie();
myImageMovie.x = 40 * i;
myImageMovie.y = 20;
myImageMovie.number.text = i;
this.addChild(myImageMovie);
}"
to super dziala ale jak sie pozniej odniesc do poszczegolnych zdjec? np jak kodem zrobic zeby konkretnie drugie zdjecie po kliknieciu zniknelo?

ah sorry english.
i meant how to do something with exactly 3rd picture in the row to disapear after click

Hi,

i need to understand how to duplicate more MC but being able to control them:

var i:Number=0
for(i=1;i<=3;i++){
var rectangle[i]:MovieClip = new my_mc();
addChild(rectangle[i]);
}

something like this, so that later i can control the 3 MC by calling rectangle[1], ecc…

In as2 i had the instance name that i could control while creating them with attachmovieclip. How can i do the same with as3?

Comments are closed.