Hello!
Do you remember the operator "instanceof" in Actionscript 2.0"
It has not been removed but Adobe advice to use the operator "is" with
Actionscript 3.0 and Flash CS3 .
What"s the use of it"
I would say that it can be very useful if we need to know the type of object contained into a MovieClip.
As an example, it happened to me to need it to develop a e-shopping cart.
At a precise event, I had to separate MovieClip from buttons from text field and even from shapes.
Let us see together how to do it"
I create a FLA and save it as "main.fla".
Into which, I create an empty MovieClip, I drag an instance on stage and I name it "container_mc".
Inside "container_mc", I create more MovieClips, text field, buttons and graphic symbols.
As many and of any type I want. For example:
I create the Document Class, an AS file saved as "Main.as", implemented the following way:
Code:
package
{
import flash.display.MovieClip;
import flash.display.SimpleButton;
import flash.display.Shape;
import flash.text.TextField;
public class Main extends MovieClip
{
public function Main()
{
countAndSeparate();
}
private function countAndSeparate():void
{
for(var i:int=0;i < container_mc.numChildren;i++)
{
if(container_mc.getChildAt(i) is MovieClip)
{
var clip:MovieClip=container_mc.getChildAt(i) as MovieClip;
trace('i am a MovieClip and my name is: '+clip.name);
}
if(container_mc.getChildAt(i) is TextField)
{
var field:TextField=container_mc.getChildAt(i) as TextField;
trace('i am a TextField and my name is: '+field.name);
}
if(container_mc.getChildAt(i) is SimpleButton)
{
var button:SimpleButton=container_mc.getChildAt(i) as SimpleButton;
trace('i am a SimpleButton and my name is: '+button.name);
}
if(container_mc.getChildAt(i) is Shape)
{
var shape:Shape=container_mc.getChildAt(i) as Shape;
trace('i am a Shape and my name is: '+shape.name);
}
}
}
}
}
and I obtain the following output:
Quote:
i' am a MovieClip and my name is: clip_0_mc
i' am a MovieClip and my name is: clip_1_mc
i' am a MovieClip and my name is: clip_2_mc
i' am a MovieClip and my name is: clip_3_mc
i' am a MovieClip and my name is: clip_4_mc
i' am a MovieClip and my name is: clip_5_mc
i' am a TextField and my name is: field_0_txt
i' am a SimpleButton and my name is: button_0_btn
i' am a SimpleButton and my name is: button_1_btn
i' am a TextField and my name is: field_1_txt
i' am a TextField and my name is: field_3_txt
i' am a TextField and my name is: field_2_txt
i' am a SimpleButton and my name is: button_2_btn
i' am a Shape and my name is: instance7
i' am a Shape and my name is: instance8
i' am a Shape and my name is: instance9
|
Let us analyse the code
I create a cycle with the number of child placed container_mc as a maximum iteration
for(var i:int=0;i < container_mc.numChildren;i++)
Using container.getChildAt(i), I retrace a child of container_mc based on the iteration of the cycle "ciclo(i)" and with the operator "is", I ask Flash the type of the found object
if(container_mc.getChildAt(i) is MovieClip)
if(container_mc.getChildAt(i) is TextField)
if(container_mc.getChildAt(i) is SimpleButton)
if(container_mc.getChildAt(i) is Shape)
To each found condition, I create a local variable of the same type found by the "if" condition at that moment and I assign to it the instance
var clip:MovieClip=container_mc.getChildAt(i) as MovieClip;
var field:TextField=container_mc.getChildAt(i) as TextField;
var button:SimpleButton=container_mc.getChildAt(i) as SimpleButton;
var shape:Shape=container_mc.getChildAt(i) as Shape;
I carry out the trace, asking for the instance name found
trace('i' am a MovieClip and my name is: '+clip.name);
trace('i' am a TextField and my name is: '+field.name);
trace('i' am a SimpleButton and my name is: '+button.name);
trace('i' am a Shape and my name is: '+shape.name);
See you next!