Flash CS3 - Flash CS4

Free tutorials and scripts for all.
Actionscript 3.0

this.root

This is a discussion on this.root within the Tutorials forums, part of the Flash English category; Here we are again with a new tip which could reveal itself very useful. This idea comes from this topic: ...


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
  6 links from elsewhere to this Post. Click to view. #1 (permalink)  
Old 04-10-07, 06:29
Administrator
 
Join Date: Jul 2007
Location: Cesena
Posts: 4,535
Rep Power: 6
Flep is on a distinguished road
this.root

Here we are again with a new tip which could reveal itself very useful.
This idea comes from this topic: Document Class .
We have seen until now that often we need to be able to pass the value of the main root of our application from the Document Class to the classes which build our application.
Sometimes, it is necessary to retrieve a MovieClip placed on stage and without the value of the root passed to the Document Class we would not be able to retrace that Movieclip.
In few words, with Actionscript 2.0 it was an easy task, using simply _root.MovieClipName (the so called Composition) from any classes to get to a MovieClip placed on stage.
With Actionscript 3.0, _root has been removed as the entire concept of levels and timeline have been changed.

As an example:
I have main.fla associated to the Document Class "Main.as" and on stage, I have a MovieClip named "test_mc". In the library of "main.fla", I have another MovieClip associated to a Class (linkage or id) named "Clip.as" that I would like to attach ( ex method attachMovie ) from the Document Class.
The first question to be asked is: how to I retrieve "test_mc" on stage of "main.fla" from "Clip.as"?

Let us look at it"

I create a FLA and save it as "main.fla".
Inside it, I create a MovieClip which I place on stage and I give it an instance name "test_mc".
I create another MovieClip, left in library, named "mc_clip" and I associate it to the Clip class via the linkage (id).
I create a Document Class associated to "main.fla", an AS file saved as "Main.as", implemented the following way:
Code:
package
{
	import flash.display.MovieClip;
	
	public class Main extends MovieClip
	{
		public function Main()
		{
			var clip_mc:MovieClip=new Clip();
			addChild(clip_mc);
			clip_mc.callBack();
		}
	}
}
I also create the class "Clip.as" associated to the MovieClip "mc_clip" left in library of "main.fla":
Code:
package
{
	import flash.display.MovieClip;
	
	public class Clip extends MovieClip
	{
		public function Clip()
		{}
		
		public function callBack():void
		{
			var m:MovieClip=this.root as MovieClip;
			m.test_mc.x=0;
			m.test_mc.y=0;
		}
	}
}
Let us look at what happens once the SWF is published:
- "main.fla" calls the Document Class (Main.as)
- as we have seen in the first lesson of Object Oriented Programming ( create my first Actionscript 3.0 class ), the Main class carries out immediately the code included in the building function:
I instance the "Clip" class associated to "mc_clip" (in library of "main.fla")
var clip_mc:MovieClip=new Clip();
I add it to the stage (otherwise it would not be visibile)
addChild(clip_mc);
I call the method "callBack" implemented in the Clip class
clip_mc.callBack();
- the method "callBack" should change the coordinates of "test_mc". To do it, it uses the property "root" which returns the parent into which is placed "mc_clip" (let us not forget that writing code in "Clip.as" would be the same as writing the code on the timeline of "clip_mc"). In fact, if we try trace(this.root); , the output would be: [object.Main]. Therefore, with "this.root" we get back to Main. In theory, if we would write "this.root.test_mc" we should be able to get back to "test_mc" but it is not like that and Flash would return the following error:
1119: Access of possibly undefined property test_mc through a reference with static type flash.display:DisplayObject.
- Here is the trick:
var m:MovieClip=this.root as MovieClip;
m.test_mc.x=0;
m.test_mc.y=0;
I force "this.root" as a MovieClip type adding it to a MovieClip variable and doing so, "test_mc" is recognised and I can interact with it as wanted.

Stay tuned !
__________________

 


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; 26-08-08 at 21:30..
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

Similar Threads

Thread Thread Starter Forum Replies Last Post
this.root - trucco del giorno Flep Articoli e tutorials 8 10-10-08 16:43
chiamare una funzione sulla root da mc Licoreo Actionscript 3.0 base 6 01-10-08 14:01


All times are GMT. The time now is 12:03.

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