View Full Version : Accessing DynamicText from external class
corvenus
November 28th, 2007, 03:32 PM
Hi everyone,
I know this has been asked countless times, I had a look at a many posts concerning this subject, but it got me confused more than anything. For example, I have looked at Senocular's Tip of the day (http://www.kirupa.com/forum/showthread.php?p=1952513#post1952513), but i'm still not quite sure what code needs to go where.
Here's my setup: I have a DynamicText object on the main timeline, which is instantiated as "txtContent". Now, from an external class (let's call it LanguageManager, stored in LanguageManager.as) i'm trying to get access to this object so i can pass some text (ex: txtContent.text = "some text").
So, which code should i put in a separate class file, and which code do i need to put in the class LanguageManager? Also, do i need to do :
addChild(txtContent);in the main timeline even if the object is already sitting on the stage?
Thanks in advance for any help!
corvenus
November 28th, 2007, 11:40 PM
Ok, I created a TopLevel.as file where I put this code (lifted from Senocular's tips):
package {
import flash.display.DisplayObject;
import flash.display.MovieClip;
import flash.display.Stage;
public class TopLevel extends MovieClip {
public static var stage:Stage;
public static var root:DisplayObject;
public function TopLevel() {
TopLevel.stage = this.stage;
TopLevel.root = this;
}
}
}
Now, in my LanguageManager class i tried a simple "trace(TopLevel.stage.stageWidth);", but i got the following error message: TypeError: Error #1009: Cannot access a property or method of a null object reference.
What am I missing? With the TopLevel class, shouldn't I be able to access the stage from any other class?
Gathan
November 29th, 2007, 01:25 AM
This is the way I do it. First for your class file you can have it setup like
package {
import flash.display.DisplayObject;
import flash.display.Stage;
public class LanguageManager {
public static var stage:Stage;
public static var root:DisplayObject;
public function LanguageManager() {
}
}
}
Then in the main timeline before you make a new LanguageManager object set the values like
LanguageManager.root = this;
LanguageManager.stage = stage;
Then in your class file you can now get the main timeline or the stage.
ajcates
November 29th, 2007, 01:37 AM
All you have to do is pass the stage object to the class. Senocular and Gathan both have good ways of doing it. I like Senocular's way better because it uses less lines of code.
corvenus
November 29th, 2007, 10:30 AM
Gathan: Thanks for the code, it works like a charm! I can finally pass some text with following line
root.txtContent.text = "some text"
The thing which puzzles me is that if i try the same with stage.txtContent.text it doesn't work. Do i need to add the txtContent TextField to the display list to be able to access it through the stage? Sorry if this sounds like a dumb question, but i would have thought that placing a TextField on the main timeline with Flash's Text tool would mean that you can automatically access it with stage.NameofTextField
Gathan
November 29th, 2007, 03:13 PM
The thing which puzzles me is that if i try the same with stage.txtContent.text it doesn't work. Do i need to add the txtContent TextField to the display list to be able to access it through the stage? Sorry if this sounds like a dumb question, but i would have thought that placing a TextField on the main timeline with Flash's Text tool would mean that you can automatically access it with stage.NameofTextField
The hierarchy is this
stage-root-txtContent
Which is why you can't access it like stage.txtContent because its not a child of the stage object its a child of the root or main timeline.
Unless of course you added the txtContent as a child of the stage of the instead
at which point the hierarchy would become
stage-root
-txtContent
And the stage would then have two childs. But anything you put on the stage not through actionscript is always a child of root.
Hope that clears up the confusion.
corvenus
November 30th, 2007, 09:58 AM
Thanks for the explanation! It was much easier to understand than all the other explanations i've seen on the subject elsewhere.
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.