How To >> Browse Articles >> Flash
Adding MovieClips to the Stage (AS 3.0)
Eric Hoanshelt
Hello!
This is a continuation of adding Actionscript to your designs.
I got alot of questions about adding MovieClips to the stage using ActionScript 3.0. So i thought i would wirte up a tutorial on how to do that and show you some neat tricks that I have learned over the years. :-)
ADDING MOVIECLIP:
Lets get this part done with so we can get to the cool stuff lol. To add a movieclip Go to Insert at the top and Click New Symbol...
Name it whatever you want and have the Movie clip radio button selected. In the Linkage section of the window, check Export for Actionscript. Keep base Class the same and change the class name to button (or whatever you like just remember what you called it). So design a button real quick on the stage it provides you.
After that, you should see your MC in the library and nothing on the stage. click the first key frame and press F9 (Mac option +9). In your actionscript panel put this:
var myBtn:button = new button;
myBtn.x = stage.stageWidth /2;
myBtn.y = stage.stageHeight /2;
addChild(myBtn);
That will add the movieclip you made on the center of the stage. (if you didnt name the class of your MC button then replace button in the code withwhat ever your class name was).
Now you might be wondering why you would want to do this...Simply put, it will keep your file size down. Also you can do some really cool things with it. For example you can add multiple buttons very easily with this method. Real quick snippet of code to do that would be this:
var numButton:Number = 6;
for (var i:Number = 0; i < numButton; i++){
var mc:button = new button();
mc.y = 75 * i;
mc.x = stage.stageWidth / 2;
addChild(mc);
}
That piece of code will add 6 of your buttons to the stage...if you only want 4 all you need to do is change the numButton = 4. Using this you can dramitcally cut down file size (and work time) which allows faster loading times on the internet and that is always a good thing :-). I dont have time to explain the all the code but if you want it explained you can email me or just send me a message and I'll help you out. Next time I'll throw in a tutorial on how to make a cool Web 2.0 button (nice a shinny)
Thanks for reading!