Be a Supporter!

As3 Document Class Tut! Root Lives!

  • 3,893 Views
  • 5 Replies
New Topic Respond to this Topic
JoSilver
JoSilver
  • Member since: Sep. 21, 2007
  • Offline.
Forum Stats
Member
Level 16
Melancholy
As3 Document Class Tut! Root Lives! Aug. 19th, 2009 @ 06:08 AM Reply

AS3 Main

I notice that there is almost no mention of the Document Class here on Newgrounds so I decide to help you guys out.

Why is the Document Class important?

Most people believe that the "_root" property in flash has been removed and that using "stage" instead works fine... WRONG!

never use stage as root. you'll completely screw up your flash. "_root" is not gone, it's just changed!
"_root" is now "root".

Slow down there partner. Don't go thinking you can just go around using "root".

In the properties panel of an AS3 movie you will see the "Document Class" text field. whatever you put there will be your root.

Why is this still not working?!?!??!1111

The way root is referenced in AS3 has change a lot but isn't any less useful.

In Order to reference root in AS3 using the document class, you need to write this...

MyRoot(root);

say you make your Document Class... "POOP" you would do this.

POOP(root);

And yes the Document Class is a Class that you can write a definition for. But you'll have to learn OOP first.

Other ways to Reach the ROOT!

you'll notice that if you try this...

trace(root.x);

It works, but if you do this...

//Defined on main timeline.
var A:int = 0;

//Put in any MovieClip.
trace(root.A);

It won't.

what you need to do is create an Object that is equal to root.

//This is movieClip.

var ROOT:Object = root;

now you can use "ROOT" as root exactly as you would use "_root" in AS2.

You can even do the same for "parent"!

var PARENT:Object = parent;

var ROOT:Object = root;

Well that just about raps thing up... YOU HAVE LEARNED!!!!!

JoSilver
JoSilver
  • Member since: Sep. 21, 2007
  • Offline.
Forum Stats
Member
Level 16
Melancholy
Response to As3 Document Class Tut! Root Lives! Aug. 19th, 2009 @ 06:16 AM Reply

Also, Remember... This works...

trace(MyRoot(root).x);

and this works too if you don't feel like add a document class...

trace(MovieClip(root).x);
Yannickl88
Yannickl88
  • Member since: Nov. 2, 2008
  • Offline.
Forum Stats
Member
Level 11
Game Developer
Response to As3 Document Class Tut! Root Lives! Aug. 19th, 2009 @ 06:27 AM Reply

Nice post!

What I always do it create a singelton-ish (http://en.wikipedia.org/wiki/Singleton_
pattern
) instance of the document class. The code for it would be:

package
{
	import flash.display.MovieClip

	public class Main extends MovieClip
	{
		private static var instance
		
		public static function getInstance()
		{
			return instance
		}
		
		public static function setInstance(obj)
		{
			instance = obj
		}
		
		public function Main()
		{
			Main.setInstance(this)
		}
	}
}

Now from anywhere in your code you can use the following to get the reference to your document class:

Main.getInstance()

Note: there is a small period of time that there is no reference, which is when everything is instantiated. Objects within the documentclass on the timeline are made first before the documentclass is created.


You can ask me anything about AS3, PHP, HTML, CSS, JavaScript, Ruby, Python, Java, XML, JSON, Photoshop, and some more....

GustTheASGuy
GustTheASGuy
  • Member since: Nov. 2, 2005
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
Response to As3 Document Class Tut! Root Lives! Aug. 19th, 2009 @ 06:52 AM Reply

At 8/19/09 06:27 AM, Yannickl88 wrote: What I always do it create a singelton-ish (http://en.wikipedia.org/wiki/Singleton_
pattern) instance of the document class. The code for it would be:

That's the right way to do it. Although you don't need setInstance.

Code on the timeline is executed after the constructor, it's hooked with addFrameScript.


BBS Signature
JoSilver
JoSilver
  • Member since: Sep. 21, 2007
  • Offline.
Forum Stats
Member
Level 16
Melancholy
Response to As3 Document Class Tut! Root Lives! Aug. 19th, 2009 @ 07:04 AM Reply

At 8/19/09 06:52 AM, GustTheASGuy wrote: Code on the timeline is executed after the constructor, it's hooked with addFrameScript.

I love that function.... =3

I should make a post about it.....

Yannickl88
Yannickl88
  • Member since: Nov. 2, 2008
  • Offline.
Forum Stats
Member
Level 11
Game Developer
Response to As3 Document Class Tut! Root Lives! Aug. 19th, 2009 @ 07:05 AM Reply

At 8/19/09 06:52 AM, GustTheASGuy wrote: That's the right way to do it. Although you don't need setInstance.

Code on the timeline is executed after the constructor, it's hooked with addFrameScript.

Hmm, well I was talking about objects withing the stage that are also linked to classes. When you want to reference the main from their constructor, there is not yet a reference set. At least that was the case a while back.

Pfeew, gust didn't kill me for my sloppy code style :D

You can ask me anything about AS3, PHP, HTML, CSS, JavaScript, Ruby, Python, Java, XML, JSON, Photoshop, and some more....