Flash CS3 - Flash CS4

Free tutorials and scripts for all.
Actionscript 3.0

attachMovie and addChild from the Timeline

This is a discussion on attachMovie and addChild from the Timeline within the Tutorials forums, part of the Flash English category; Ciauz! Here is another article dedicated to the less experts but with so much desire to learn. After having seen ...


Go Back   Forum Flash CS3 Flash CS4 > Flash CS3 Flash CS4 > Flash English > Tutorials

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  3 links from elsewhere to this Post. Click to view. #1 (permalink)  
Old 27-09-07, 09:25
Administrator
 
Join Date: Jul 2007
Location: Cesena
Posts: 4,535
Rep Power: 6
Flep is on a distinguished road
attachMovie and addChild from the Timeline

Ciauz! Here is another article dedicated to the less experts but with so much desire to learn.
After having seen how to use the ex method attachMovie of the classes in various ways (also seen in the article DuplicateMovieClip removed ), I have received many messages saying that it was too difficult and that many of you had problems to migrate to Actionscript 3.0.
Follow this article because with few lines of code I will very easily explain how to attach a MovieClip from the library of Flash CS3 to the Stage and how to communicate with it.

Let"s get into it"

I create a FLA and save it as "attach_movie.fla", into which I create a MovieClip kept in library as well as placed on Stage.
To the one in library, I assign the name of "mc_container" and to its instance on Stage, the name of "container_mc".
We will see how to attach a MovieClip placed in library inside that first MovieClip created.
I now create another MovieClip in library named "mc_clip". That MovieClip will be the one used with the method attachMovie.

With Actionscript 2.0, we were assigning a linkage ID to the clip to be attached and were then using the method attachMovie passing it the name of the MovieClip.
With Actionscript 3.0, things have changed a bit but do not be scared as it is easier to do that you could think.

To do it with Flash CS3, we will need to create an AS file associated to the MovieClip we would like to attach. In this example, we will assign to it "mc_clip".
I create an AS file and save it as "Clip.as", implemented the following way:
Code:
package
{
	import flash.display.MovieClip;
	
	public class Clip extends MovieClip
	{
		public function Clip()
		{
			
		}
	}
}
Back to the FLA, I select with the right click "mc_clip" placed in library and select "linkage" in the menu. In the new opened windows, I select the option "export for Actionscript" and add in the first input text field "Clip", leaving the second one as it is.

identificatore MovieClip in Flash CS3

Let"s click "ok" to save those options, open next the action panel and write:
Code:
var clip_1_mc:Clip=new Clip();
this.addChild(clip_1_mc);
clip_1_mc.x=100;
clip_1_mc.y=container_mc.y;

var clip_2_mc:Clip=new Clip();
container_mc.addChild(clip_2_mc);






As you can see, we have attached clip_mc from the library. To the left, it has been attached to the Stage and to the right, inside the container_mc already on Stage.

Let"s analyse the code.


I create an instance of the Clip class (Clip.as)
var clip_1_mc:Clip=new Clip();
if we would publish the swf now, the clip that we would like to attach would not be visible as we need the method addChild to add it to the Stage. The Clip class extends the MovieClip and addChild wants as a parameter a MovieClip, we add it to the Stage with "this" being our Stage.
this.addChild(clip_1_mc);
now we can refer to clip_1_mc and interact within all its properties.
I position "clip_1_mc" using its x and y properties
clip_1_mc.x=100;
clip_1_mc.y=container_mc.y;
The following two lines will be to attach "mc_clip" placed in library inside "container_mc" placed on Stage of the FLA. Once again, I create an instance of the Clip class (Clip.as) the following way:
var clip_2_mc:Clip=new Clip();
instead of using the addChild with the Stage, we will do it with "container_mc"
container_mc.addChild(clip_2_mc);

PS: If you do not create Clip.as, Flash will do it for you. But you will not have a AS file to write code that refers at that MovieClip. If so, you have to write code on Timeline by calling clip_2_mc.

Source files:
Attached Files
File Type: zip ex_attachMovie.zip (6.5 KB, 62 views)

__________________

 


I recommend: Essential Actionscript 3.0

- I do not reply technicians pvt messages. Open a thread !
- Non rispondo ai messaggi privati con domande tecniche. Apri una discussione sul forum !

Last edited by Flep; 28-08-08 at 06:19..
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #2 (permalink)  
Old 18-06-08, 23:18
Member
 
Join Date: Sep 2007
Posts: 37
Rep Power: 0
manheman is on a distinguished road
Re: attachMovie and addChild from the Timeline

hello,
Is it possible de place this part of code :

Code:
var clip_1_mc:Clip=new Clip();
this.addChild(clip_1_mc);
clip_1_mc.x=100;
clip_1_mc.y=container_mc.y;

var clip_2_mc:Clip=new Clip();
container_mc.addChild(clip_2_mc);
in a as in order to have the same effect ?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 09-07-08, 20:18
Junior Member
 
Join Date: Jul 2008
Posts: 2
Rep Power: 0
dustinsmith08 is on a distinguished road
Re: attachMovie and addChild from the Timeline

which 'Clip' is code, and which is the name of the movie clip, its confusing to call both 'Clip' - other then that thanks for the tutorial!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 09-07-08, 21:17
Junior Member
 
Join Date: Jul 2008
Posts: 2
Rep Power: 0
dustinsmith08 is on a distinguished road
Re: attachMovie and addChild from the Timeline

oops, im dumb i get it - they are both the name of the clip, for some reason I thought one was a code command to get a new 'clip'
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 21-07-08, 03:11
Junior Member
 
Join Date: Jul 2008
Posts: 5
Rep Power: 0
thawootah is on a distinguished road
Re: attachMovie and addChild from the Timeline

AS3 is ridiculous. I put off using it for awhile now. I'm still not sure if i want to switch from AS2.

Why do we need to use classes and package everything in separate AS files for? I put all my code on the first frame in as2 and always used listeners to begin with. I never had a problem with doing things that way and it was a whole lot easier when reusing code. and made more sense.

And if they wanted to move in that direction why not just make it a simple include file?

Now they changed all the names and you cant even point variables to MCs in a simple way you have to go through all these obstacles for such an easy task. People complained XML and Dynamic Apps were hard in AS2 compared to this? BS!! You have to write twice as much code and everything is ass backwards. You spend most of your time in reference manuals than actually developing anything, And why oh why flex? I know once you get the hang of it you will like it, No probably not.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 21-07-08, 03:25
Junior Member
 
Join Date: Jul 2008
Posts: 5
Rep Power: 0
thawootah is on a distinguished road
Re: attachMovie and addChild from the Timeline

sorry for the rant ive been messing around with trying to make a "simple" XML gallery in AS3 all day. all i kept thinking is damn it would take me 30 mins in AS2 and 20 mins in php.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 21-07-08, 08:44
Onsitus's Avatar
CSS.FlepStudio.org
 
Join Date: Jul 2007
Location: Nettuno Beach
Posts: 1,060
Rep Power: 3
Onsitus is on a distinguished road
Re: attachMovie and addChild from the Timeline

Quote:
Originally Posted by thawootah View Post
sorry for the rant ive been messing around with trying to make a "simple" XML gallery in AS3 all day. all i kept thinking is damn it would take me 30 mins in AS2 and 20 mins in php.
No worry. I am actually in the same situation so I can understand your rant...little by little we will get there (hopefully).
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 23-07-08, 10:06
Junior Member
 
Join Date: Jul 2008
Posts: 5
Rep Power: 0
thawootah is on a distinguished road
Re: attachMovie and addChild from the Timeline

Quote:
Originally Posted by Onsitus View Post
No worry. I am actually in the same situation so I can understand your rant...little by little we will get there (hopefully).
Yeah one step at a time i guess just like when i switched from swish to flash 5 years ago.

Hey I think i figured out the new way of handling movieclips.
heres how you give it a unique id and interact with it.
MC.name = "myClip" + i;
var MC:MovieClip = getChildByName("myClip" + i);

the only thing i don't like is if you addchild(myClip); and base your code around that then you decide you want a parent clip to hold that first child you have to rewrite the whole damn thing instead of just right clicking the clip and converting it.. Unless I'm missing something. hmmm back to the docs.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads

Thread Thread Starter Forum Replies Last Post
Actionscript 3 AddChild contro AttachMovie Doctor Stones Actionscript 3.0 avanzato 2 27-10-08 11:59
Actionscript 3 attachMovie su linea milo Actionscript 3.0 avanzato 1 09-09-08 23:56
attachMovie...removed ! Flep Tutorials 5 19-06-08 20:39
AttachMovie e addChild dalla Timeline Flep Articoli e tutorials 6 11-05-08 08:01
attachMovie and co tatone7602 Actionscript 3.0 base 6 27-08-07 09:22


All times are GMT. The time now is 12:18.

Powered by vBulletin version 3.7.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC4
Forum SiteMap