Thursday, 27 August 2009

Attach movie clip dynamically in Actionscript 3

In this tutorial you will learn how to attach a movie clip dynamically using Actionscript 3.0. I will be using a default Flash button to attach movie clips randomly on to the stage. This tutorial uses the same attachMovie method from the AttachMovie in Actionscript article.


Attach movie clip dynamically in Actionscript 3

Step 1

Open a Flash AS3 file and set the stage size to whatever size you wish.
Select Insert > New Symboland give your symbol an appropriate name select movie clip as the type and click ok.



Select any of the shape tools and create your object on the stage. I have created a simple circle shape with #FF0000 colour and a 2pt stroke, but you can create whatever kind of shape you wish.


Step 2

In the library right click on your movie clip and select ‘Properties’ then check the ‘Export for Actionscript’ checkbox like below. Make sure you have given an appropriate class name as this will be needed later on. Then click ok, if you receive a Actionscript class warning click ok.




Step 3

Select Window > Components and drag the button component onto the stage. Alternatively you can create your own button on the stage at this point.


Then give your button the instance name: attachMovie_btn


Step 4

On the timeline create a new layer called “Actions’. Select the first frame and hit F9 to open up the Actions panel and enter the following code:

attachMovie_btn.addEventListener(MouseEvent.CLICK, attachMovieclip);

function attachMovieclip(event:MouseEvent):void {
var addCircle:Circle = new Circle();
addCircle.x= Math.random()* stage.width;
addCircle.y= Math.random()* stage.height;
addChild(addCircle);

}

Code:
1. This adds an event listener to the ‘attachMovie_btn’ button with the click event.
2. This function creates a new instance of Circle class which is the object created in step 1. The position of the circle object is placed randomly on the stage and will be added when the user clicks on the button.


Step 5


Test your movie clip Ctrl + Enter. Now click the button and you should see random circles on the stage.




You should now be able to attach Movie clip dynamically in Actionscript 3.0.

11 comments:

Jnz77 25 November 2009 06:35  

My problem is how to create USEABLE movieClips dinamically, which can be accessed after creating.

iliketo 25 November 2009 09:52  

@Jnz77

What do you mean useable, you can use the dot syntax to access the movie clips properties.

samsara 2 December 2009 20:45  

@iliketo

I think Jnz77 meant how to make reference to the individual movie clips, as they are not assigned instance names during creation.

iliketo 3 December 2009 04:27  

@Samsara

Uou can use the dot syntax to access the movie clips properties.

Fahim Akhter 21 January 2010 00:56  

My problem is if all these circles are created they all have been created with the variable name addCirlce. Now if I want to reference them individually how would I go along doing that?

iliketo 21 January 2010 03:11  

@Fahim Akhter

You can use the 'name' property.

Kinetic 1 March 2010 19:50  

Great post, thanks a lot. I've busted my nuts looking for a means of creating MC instances, the export for action script option is hidden really well.

nutless neo 31 December 2011 15:54  

ho would i modify this code to call my movieclip only once when the button is pushed? I don't want a bunch of instances of the same MC.

iliketo 1 January 2012 02:38  

@Nutless neo

Remove the event listener.

Jimbo 22 April 2013 01:04  

hey sir, your blog is very useful, anyway, i just want to ask you, can u teach how to click at specific part..the movie clip will come out at there..

sorry for my bad english..

iliketo 23 April 2013 12:39  

@jimbo

You need to add an click event.

  COPYRIGHT © 2014 · ILIKE2FLASH · Theme by Ourblogtemplates

Back to TOP