Skip to main content
miquael
Inspiring
September 30, 2009
Answered

Accessing "root" in AS 3

  • September 30, 2009
  • 3 replies
  • 893 views

I realize that "_root" no longer exists in AS 3, yet how do I access objects on the timeline from within a class?

I have "myMovieClip" on the main timeline.  It has an associated class "MyMovieClip.as"

I have another class that I want to pass variable values from to be placed in text fields nested inside the "myMovieClip" on the timeline: "myMovieClip.title.text"

I've tried passing values thru a static function method, yet this is even limited because I cannot seem to pass the value outside of that function into the general scope of the class.

This works if I just place it in a general function inside MyMovieClip class:

private function init() {

     title.text = "TEST";

}

yet something more dynamic like calling this static function from an outside class gets an error message:

public static function updateCallTag(d:String) { 

     title.text = d;

}        

1120: Access of undefined property title.

Seem like this should be pretty simple and straight forward.  What am I missing here?

This topic has been closed for replies.
Correct answer gopal73thakkar

miquael wrote:

I realize that "_root" no longer exists in AS 3, yet how do I access objects on the timeline from within a class?

I have "myMovieClip" on the main timeline.  It has an associated class "MyMovieClip.as"

I have another class that I want to pass variable values from to be placed in text fields nested inside the "myMovieClip" on the timeline: "myMovieClip.title.text"

I've tried passing values thru a static function method, yet this is even limited because I cannot seem to pass the value outside of that function into the general scope of the class.

This works if I just place it in a general function inside MyMovieClip class:

private function init() {

     title.text = "TEST";

}

yet something more dynamic like calling this static function from an outside class gets an error message:

public static function updateCallTag(d:String) { 

     title.text = d;

}        

1120: Access of undefined property title.

Seem like this should be pretty simple and straight forward.  What am I missing here?

try 

Object(root).myMovieClip.title.text

or

Object(root).title.text

I am not sure but I think it's work because i'll use this type of code in my project.

3 replies

Muzak
Inspiring
September 30, 2009

Rather than trying to access objects outside the class - which you shouldn't - look into dispatching events.

gopal73thakkarCorrect answer
Participant
September 30, 2009

miquael wrote:

I realize that "_root" no longer exists in AS 3, yet how do I access objects on the timeline from within a class?

I have "myMovieClip" on the main timeline.  It has an associated class "MyMovieClip.as"

I have another class that I want to pass variable values from to be placed in text fields nested inside the "myMovieClip" on the timeline: "myMovieClip.title.text"

I've tried passing values thru a static function method, yet this is even limited because I cannot seem to pass the value outside of that function into the general scope of the class.

This works if I just place it in a general function inside MyMovieClip class:

private function init() {

     title.text = "TEST";

}

yet something more dynamic like calling this static function from an outside class gets an error message:

public static function updateCallTag(d:String) { 

     title.text = d;

}        

1120: Access of undefined property title.

Seem like this should be pretty simple and straight forward.  What am I missing here?

try 

Object(root).myMovieClip.title.text

or

Object(root).title.text

I am not sure but I think it's work because i'll use this type of code in my project.

miquael
miquaelAuthor
Inspiring
October 2, 2009

Yes, I also thought that "stage.mc" should work--but it does not (!).

Dispatch events sound interesting and efficient, yet in this situation, I have not ben able to take the time to figure out how I would implement them here.

This method:

Object(root).myMovieClip.title.text

Is very straight forward and useful ... and it works!  Nice tip!

Thanks guys!

Perfect_Nemus
Known Participant
September 30, 2009

stage gives you the ...ehm.. "gunlayer" of the main timeline:

a mc placed on the stage will be foundable:

stage.mc

Generally, if you want to go on the upper level, you need to use parent keyword:

this.parent drag you on the upper MC