Forum Topic: AS3: Access root

(172 views • 4 replies)

This topic is 1 page long.

<< < > >>
None

Celtrum

Reply To Post Reply & Quote

Posted at: 10/22/09 05:18 PM

Celtrum LIGHT LEVEL 08

Sign-Up: 10/26/07

Posts: 592

Is it possible to access the root in a class extending a DisplayObject?
I have this code, but it brings the following error:

1061: Call to a possibly undefined method dead through a reference with static type flash.display:DisplayObject.
Main Timeline:
function dead():void{
	trace("This traces dead for no particular reason.");
}
var myShoe:Bla = new Bla();
addChild(myShoe);
myShoe.init();

Bla Class:
package{
	import flash.display.MovieClip;
	
	public class Bla extends MovieClip{
		public function Bla(){}
		public function init():void{
			root.dead();
		}
	}
}

Thanks.


None

RyanPridgeon

Reply To Post Reply & Quote

Posted at: 10/22/09 05:48 PM

RyanPridgeon LIGHT LEVEL 11

Sign-Up: 12/07/05

Posts: 1,982

You will need to pass the stage to the class in a function (such as the constructor).

For example

import flash.display.Stage;
public class Foo{
    private var root:Stage;
    public function Foo(rootpass:Stage){
        root = rootpass;
    }
    public function Bar(){
        root.dostuff();
    }
}

Then create it in the main timeline

Foo thing = new Foo(this.stage);

I make flashes because I can.
PM me for anything flash or web related or visit my blog here on NG!
Also, here's my DICK

BBS Signature

None

billowillo

Reply To Post Reply & Quote

Posted at: 10/23/09 12:24 AM

billowillo DARK LEVEL 15

Sign-Up: 04/21/08

Posts: 691

What i do is declar a variable called _root. and have it equal MovieClip(root)

that makes it so you can use _root. to connect to the .fla

BBS Signature

None

blah569

Reply To Post Reply & Quote

Posted at: 10/23/09 02:18 AM

blah569 DARK LEVEL 21

Sign-Up: 01/18/05

Posts: 2,706

Another option could be, in your document class (minimal example)

package
{
    import flash.display.MovieClip;
    import flash.display.Stage;

    public class MainClass extends MovieClip
    {
        public static var stageRef:Stage;
        
        public function MainClass():void
        {
            stageRef = stage;
        }
    }
}

and, to refer to the stage, you could make a new instance of it in your other classes:

public var cur:Stage = MainClass.stageRef;

cur.addChild(self);

or just by

MainClass.stageRef.addChild(self);

Good luck!

PHP: Main | AS3: Main | Get Firefox | Host large files (fast and free)!
"Thank you for learning me English."

BBS Signature

None

henke37

Reply To Post Reply & Quote

Posted at: 10/23/09 04:53 AM

henke37 NEUTRAL LEVEL 23

Sign-Up: 09/10/04

Posts: 3,665

Cast the object reference in "root" to the DocumentClass, bang, you got a reference to the Document class instance.

Each time someone abuses hittest, God kills a kitten. Please, learn real collision testing.


All times are Eastern Standard Time (GMT -5) | Current Time: 05:36 PM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!