Flash CS3 - Flash CS4

Free tutorials and scripts for all.
Actionscript 3.0

Operator is of Actionscript 3.0

This is a discussion on Operator is of Actionscript 3.0 within the Tutorials forums, part of the Flash English category; Hello! Do you remember the operator "instanceof" in Actionscript 2.0" It has not been removed but Adobe advice to ...


Go Back   Forum Flash CS3 Flash CS4 > Flash CS3 Flash CS4 > Flash English > Tutorials

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 31-10-07, 06:44
Administrator
 
Join Date: Jul 2007
Location: Cesena
Posts: 4,535
Rep Power: 6
Flep is on a distinguished road
Operator is of Actionscript 3.0

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!
__________________

 


I recommend: Essential Actionscript 3.0

- I do not reply technicians pvt messages. Open a thread !
- Non rispondo ai messaggi privati con domande tecniche. Apri una discussione sul forum !

Last edited by Flep; 28-08-08 at 06:51..
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 18:13.

Powered by vBulletin version 3.7.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC4
Forum SiteMap