AS:Components
This tutorial will cover creating the component .fla and .as class file
Availability:
Flash MX 2004
Flash 8
-------------------
the .fla file
1)open a new .fla file and save it with the name of your component
2)create a new movieclip on the stage with linkage for action script (but not export on first frame) linkage name should be the name of your component
you must also set the AS2.0 class (explained in step 6)
3)on the first frame of the movieclip draw a white box 100*100 and convert to a movieclip named bounding_box : give an instance name of bounding_box
4)on the first frame actions add stop();
5)add a new frame on the second frame:
In this frame is where you will place all the movieclips needed for the component:
each movieclip must be set for linkage to actionscript and not export in first frame (it is not needed since the movieclips are inside of the first movieclip)
each linkage name should have a unique pre-fix so that it wont clash with any other linked movieclips the user might use:
for example you may use a prefix like abcd_ or whatever
6)In the library right click on the first movieclip and go to 'Component Definition...'
In here you must set a few things:
#the first is the 'AS 2.0 Class' variable - this is the name and directory of where the class file is in relation to the .fla
(if it isnt in the same directory then you must specify the directory but for some reason, you dont use '\' to seperate like classfolder\classfile for some reason you use a '.' instead so that 'classfolder\classfile' becomes 'classfolder.classfile' (note it does not have .as on the end)
#tick 'Lock Parameters in Instance' and 'display in components panel' and set 'tooltip text' to the name of your component
#at the top is the parameters table, here you must set all the extra parameters you're component is using (not neccesary)
-------------------
the .as file
i recomend that if you have no idea what a class is that you read a tutorial on OOP either from AS:Main or from elsewhere
first off:
1) open a new .as file and save it with whatever name you gave and in what ever directory you gave in relation to the .fla in the 'Component Definition...'
NOTE THAT THE CLASS FILE NAME MUST BE THE SAME AS THE CLASS NAME AND CLASS FUNCTION OF THE CLASS FILE
2) for tutorials sake i am going to call the class 'RainyDays' (dont ask why its the first thing that came to my head)
class RainyDays extends mx.core.UIComponent
{
}
there are 4 main functions a component class must have
function RainyDays() {} (this can just be left empty if you want but it must be included)
private var bounding_box:MovieClip;
private function init():Void
{
super.init();
bounding_box.swapDepths(1);
bounding_box.removeMovieClip();
/*
create and set whatever vars you may want to set when initialized
*/
}
private function createChildren():Void
{
/* here is where you attach all your movieclips to the component from the first stage */
attachMovie("abcd_hello","mc_hello",1);
attachMovie("abcd_happy","mc_happy",2);
/* this is also where you should place all the code that may be applied to these movieclips - any button code etc */
}
all variables and mc's must be declared in the class i.e.
private var mc_hello:MovieClip;
private var mc_happy:MovieClip;
finnaly:
private function draw():Void
{
/*this is the main visual function where everything is placed and scaled and is extremely important*/
/*a useful part of components is that you can resize them, by resizing the component, we must scale and re-place all movieclips inside*/
/*we can do this by accessing width and height which are two auto-declared variables storing the width and height of the component movieclip*/
var min:Number = Math.min(width,height); // these are 3 useful variables for scaling and position movieclips correctly
var offsetX:Number = (width-min)/2;
var offsetY:Number = (height-min)/2;
mc_hello._width = mc_hello._height = min;
mc_hello._x = offsetX;
mc_hello._y = offsetY;
mc_happy._width = mc_happy._height = min/2;
mc_happy._x = offsetX+min/4;
mc_happy._y = offsetY+min/4;
}
another standard function to have in a component is setSize(width:Number,height:Number) for setting the size of the movieclip through AS
public function setSize(w:Number,h:Number):Void
{
__width = w; //note how its __width and not width or _width
__height = h;
invalidate();
}
invalidate() is an important function call which basicly simply calls the draw() function along with other hidden functions from UIComponent
//////
so alltogether so far, the class file looks like this:
class RainyDays extends mx.core.UIComponent
{
private var mc_hello:MovieClip;
private var mc_happy:MovieClip;
private var bounding_box:MovieClip;
function RainyDays(){};
private function init():Void
{
super.init();
bounding_box.swapDepths(1);
bounding_box.removeMovieClip();
}
private function createChildren():Void
{
attachMovie("abcd_hello","mc_hello",1);
attachMovie("abcd_happy","mc_happy",2);
mc_happy.onPress = function()
{
_parent.mc_hello._visible = false;
}
mc_happy.onRelease = function()
{
_parent.mc_hello._visible = true;
}
}
private function draw():Void
{
var min:Number = Math.min(width,height);
var offsetX:Number = (width-min)/2;
var offsetY:Number = (height-min)/2;
mc_hello._width = mc_hello._height = min;
mc_hello._x = offsetX;
mc_hello._y = offsetY;
mc_happy._width = mc_happy._height = min/2;
mc_happy._x = offsetX+min/4;
mc_happy._y = offsetY+min/4;
}
public function setSize(w:Number,h:Number):Void
{
__width = w; //note how its __width and not width or _width
__height = h;
invalidate();
}
}