| loading mc into empty mc
• | | | |
 | loading mc into empty mc
by Ben Mettler on May 4, 2012 at 5:29:33 pm |
I did this many years ago and I tried searching for this but did not find what I was looking for. I basically have a group of buttons and I want the buttons to load movie clips into an empty mc and then have the movie clip close again when the user clicks somewhere else. Do I actually have to save the movie clips as swf files first and then load them in to the empty mc?
Thanks!
| | | | |
• | | | |  | Re: loading mc into empty mc by Pieter Helsen on May 4, 2012 at 8:19:51 pm |
Hi Ben,
Could you tell me first what version of Actionscript you are using? AS2 or AS3?
There's several ways to achieve what you want without having to resort to saving the MCs as separate swf files.
Kind regards,
Pieter
General notice: from now on, I would like to ask everyone to put [AS2] or [AS3] (corresponding to the version of actionscript you are using on your project) in front of their post titles when the question is actionscript related! Please help us help you faster. Thank you.
| | | | |
• | | | |  | Re: loading mc into empty mc by Ben Mettler on May 4, 2012 at 8:34:32 pm |
AS3
| | | | |
• | | | |  | Re: loading mc into empty mc by Pieter Helsen on May 4, 2012 at 8:53:00 pm |
Good, 'cause my AS2 skills are rusty :)
First thing you'll want to do is go into the Library and create a class for all movie clips that you want to load.
Here's a screenshot of what it should look like:
http://screencast.com/t/f1awsbWik
Next, you'll want to put the following code on your buttons:
import flash.events.MouseEvent;
import flash.display.MovieClip;
function clearContainer():void {
while(myContainerMC.numChildren > 0) {
myContainerMC.removeChildAt(0);
}
}
myButton.addEventListener(MouseEvent.CLICK, myButtonClick, false, 0, true);
function myButtonClick(e:MouseEvent):void {
// if the container is not empty (i.e. if you have previously
// loaded another MC), the container's contents will be cleared.
clearContainer();
// create a new instance of your movieclip
// and add it to the container
var myMC:MovieClip = new MyTestMC();
myContainerMC.addChild(myMC);
}
Kind regards,
Pieter
General notice: from now on, I would like to ask everyone to put [AS2] or [AS3] (corresponding to the version of actionscript you are using on your project) in front of their post titles when the question is actionscript related! Please help us help you faster. Thank you.
| | | | |
• | | | |  | Re: loading mc into empty mc by Ben Mettler on May 4, 2012 at 9:23:51 pm |
Sorry, so I'm not sure what part of the code I'm replacing with what? So should myContainerMC be renamed with the name of my empty mc or the mc I want to load? Do I change myButtonClick to the name of my button?
It's been a while since I've used Flash so apologies for the newb questions.
Thanks!
| | | | |
• | | | |  | Re: loading mc into empty mc by Pieter Helsen on May 4, 2012 at 9:26:27 pm |
Yeah, pretty much :) Since I didn't have your code I used some made-up names.
myContainerMC should be replaced with the instance name of your empty movieclip
myButton should be replaced with the instance name of your button
Kind regards,
Pieter
General notice: from now on, I would like to ask everyone to put [AS2] or [AS3] (corresponding to the version of actionscript you are using on your project) in front of their post titles when the question is actionscript related! Please help us help you faster. Thank you.
| | | | |
• | | | |  | Re: loading mc into empty mc by Ben Mettler on May 4, 2012 at 9:33:20 pm |
Gotcha, and where does the movie clip that I'm trying to load into the empty mc come in to play?
| | | | |
• | | | |  | Re: loading mc into empty mc by Pieter Helsen on May 4, 2012 at 9:37:10 pm |
well, the movieclip is actually a class now. So what the following lines do is creating a new instance of the movieclip and adding it to your container (it's like dragging a movieclip from the library onto the stage):
var myMC:MovieClip = new MyTestMC();
myContainerMC.addChild(myMC);
So 'MyTestMC' is the name of the class that you gave to your movieclip in the library.
Kind regards,
Pieter
General notice: from now on, I would like to ask everyone to put [AS2] or [AS3] (corresponding to the version of actionscript you are using on your project) in front of their post titles when the question is actionscript related! Please help us help you faster. Thank you.
| | | | |
• | | | |  | Re: loading mc into empty mc by Ben Mettler on May 4, 2012 at 9:48:21 pm |
So I tried adding the as to one of my buttons and I got an error when I tried preview: "1046: Type was not found or was not a compile-time constant: empty_mc."
This is what my AS looks like:
import flash.events.MouseEvent;
import flash.display.MovieClip;
function clearContainer():void {
while(empty_mc.numChildren > 0) {
empty_mc.removeChildAt(0);
}
}
btn_2.addEventListener(MouseEvent.CLICK, btn_2Click, false, 0, true);
function btn_2Click(e:MouseEvent):void {
// if the container is not empty (i.e. if you have previously
// loaded another MC), the container's contents will be cleared. clearContainer();
// create a new instance of your movieclip
// and add it to the container
var myMC:MovieClip = new bio_mc(); empty_mc.addChild(myMC); }
| | | | |
• | | | |  | Re: loading mc into empty mc by Pieter Helsen on May 5, 2012 at 12:20:43 am |
The problem is that you added that code onto the button.
You should not put code on buttons in AS3 :)
Instead, give everything an instance name (I think you've already done that) and put your code on the timeline.
Basically what the error is saying is: "hey, I don't know anything about this empty_mc thing..." and that's because the button only knows about it's children, so unless empty_mc is a child of (is inside of...) your button, this code won't work.
Place the code on the timeline that contains your button and your empty_mc and it should work just fine.
It does look like you commented out the call to 'clearContainer' though. Make sure clearContainer is on a new line and not part of the comment (// loaded another MC), the container's contents will be cleared. clearContainer();)
Kind regards,
Pieter
General notice: from now on, I would like to ask everyone to put [AS2] or [AS3] (corresponding to the version of actionscript you are using on your project) in front of their post titles when the question is actionscript related! Please help us help you faster. Thank you.
| | | | |
• | | | |  | Re: loading mc into empty mc by Ben Mettler on May 7, 2012 at 3:26:14 pm |
So I put all the as on an actions layer at the top of all my layers but I'm still getting that same error? Now just to be clear, should I have an instance of the empty mc on the stage or not? Also, when I first gave the empty mc and bio mc a class name it said "A definition for this class could not be found in the classpath, so one will be automactically generated in the SWF file upon export." I'm not sure what that means or if it's effecting anything.
| | | | |
• | | | |  | Re: loading mc into empty mc by Pieter Helsen on May 9, 2012 at 10:41:36 am |
Woops, looks like I missed this one...
Yes, you should definitely have an instance of the empty MC on the stage.
// this means that you are creating an instance of a class
// or movieclip in the library...
var myMC:MovieClip = new MyTestMC();
// this means that there is already an instance of the container MC
// on the stage and the instance name is 'myContainerMC'.
myContainerMC.addChild(myMC);
If you DON'T have an instance on the stage, you could do something similar as we did with the MyTestMC:
var myContainerMC:MovieClip = new MyContainerMC();
addChild(myContainerMC);
// myButton would have to be an instance on the stage...
myButton.addEventListener(MouseEvent.CLICK, myButtonClick, false, 0, true);
function myButtonClick(e:MouseEvent):void {
// if the container is not empty (i.e. if you have previously
// loaded another MC), the container's contents will be cleared.
clearContainer();
// create a new instance of your movieclip
// and add it to the container
var myMC:MovieClip = new MyTestMC();
myContainerMC.addChild(myMC);
}
Kind regards,
Pieter
General notice: from now on, I would like to ask everyone to put [AS2] or [AS3] (corresponding to the version of actionscript you are using on your project) in front of their post titles when the question is actionscript related! Please help us help you faster. Thank you.
| | | | |
| |
|