Skip to main content
Inspiring
November 19, 2008
Question

need assistance with buttons pulling in different MC's

  • November 19, 2008
  • 8 replies
  • 731 views
Hi I would like to have a screen with multiple buttons and when the button is clicked different MC's will be imprted into the .fla file. For instance button 1 will import 1_MC, button 2, 2_MC, button 3, 3_MC etc.

Can someone point me to a tutorial or give me a down and dirty example using AS3?
This topic has been closed for replies.

8 replies

New Participant
November 22, 2008
I had a problem with this ages ago - i could import the separate SWFs into the document - but the preloaders would not work.

anyone know why?
Inspiring
November 21, 2008
couple of questions on this...

myButton1.addEventListener(MouseEvent.CLICK, addMovie1);

myButton1 would be the instance name of the button, correct? and I thought that addMovie1 would be the instance name of the movie clip you want to bring in, but I get an error when I test it out "access of undefined property(movieclip). Is this not correct?

one more thing...

in setting the coordinates.

myMovie2.x = ...
myMovie2.y =...

would that be starting from the upper right hand corner of the stage and the number of pixels from that point? eg: 400, 500 etc.

thanks for helping me through this!
Inspiring
December 3, 2008
just wanted to check back in to see if anyone can offer any assistance with this.
Inspiring
November 20, 2008
a few more questions are

if I make buttons to bring in different Movie Clips on clicking would I then need to make the buttons Movie Clips also? (can you do that?)

is there a way to position the Movie Clips on the state via the AS?

It might be helpful if there is a good tutorial on this someone could point me to.

thanks!
Ned Murphy
Brainiac
November 21, 2008
You don't need to make the buttons as movieclips, but you could if you had a reason for it. Like if you wanted to load the images into the buttons themselves... then you'd need them to be movieclips. You can use the same code for the event listeners for movieclips as you would for buttons...

myButton1.addEventListener(MouseEvent.CLICK, addMovie1);

But you don't add this code to the buttons (like you used to be able to sort of do in AS2)... it sits in the actions layer you create in the timeline where most of your code goes. As long as your button in in the same timeline and has the same instance name assigned to it as the code (ex. myButton1 above), the code will play for the button.

You can position instances using AS by setting their x and y coordinates, even before they appear. You can pretty much set any property before you add it to the stage....

function addMovie1(e:MouseEvent):void
{
var myMovie1:Movie1 = new Movie1();
myMovie2.x = ...
myMovie2.y =...
myMovie1.name = ...
myMovie1.alpha = ...
etc...
mcContainer1.addChild(myMovie1);
}

I think I've handled all of your questions with the snippets I just provided, so give it a go. Tutorials are handy, but you're doing the right thing if you find yourself struggling to figure stuff out without them... it's a better learning process... it tends to burn in better and last longer from having to solve it.
Inspiring
November 20, 2008
thanks Ned, that kind of makes sense. I will put together a demo and try it out. So would I add this code to a button to bring in the MovieClip? That would be what I want is to bring in a new MC upon clicking a button.

var myMovie1:Movie1 = new Movie1();
mcContainer.addChild(myMovie1);
Ned Murphy
Brainiac
November 20, 2008
To do that you need to assign a Class ID to each movie you want to pull in that's in the library.

To do this, one at a time, right click on each mc in the library you want to bring in and select Linkage... from the menu that appears.

An interface will appear where you click on "Export for Actionscript" and the Class field will automatically fill in with the name of the mc from the library, automatically removing any spaces it might have.... you can change it if you like. Click OK and you'll get a notification that the Class definition could not be found and will be automatically generated.

When you want to bring the mc into the main movie from the library, you declare an object of the Class and use the addChild() method wherever it needs to be targeted...

Let's say you named the Class: Movie1 and you want to load it into another movieclip that's on the stage named mcContainer. Then your code to bring it into play would be:

var myMovie1:Movie1 = new Movie1();
mcContainer.addChild(myMovie1);

where myMovie1 is whatever identifier you want to give to this object.

And you can add the same one as many times thereafter without having to declare it again...

mcOtherContainer.addChild(myMovie1);
mcOtherOtherContainer.addChild(myMovie1);

Ned Murphy
Brainiac
November 20, 2008
Either way is possible, the latter can save bytes in the main file if they are byte-heavy mc's. So decide and I or someone will offer a little code to help.
Inspiring
November 20, 2008
I would rather do it the pulling various MovieClips that are in the library into a FLA file.
Inspiring
November 19, 2008
Well, I was thinking the MovieClips would be in the Library of the FLA file. Not sure if what I am referring to can be done. Another way would have multiple swf files and a main swf file with buttons. Pressing each button will display another file.
Ned Murphy
Brainiac
November 19, 2008
Where will they be imported from? And don't you mean the swf file (not the fla)--just wanna be sure?