Skip to main content
Participant
December 26, 2016
Question

Call a fieldtext in stage from a Class

  • December 26, 2016
  • 7 replies
  • 380 views

Hello, guys, I can´t figure out this, hopefully somebody can guide me, since all the related topics I could find were pointing in another direction (create a fieldtext in stage from within a Class).

Lets say I already have a dynamic fieldtext in stage, and its instance name is "txt1".

Now, I want to put some text in it from a Class (not the Main Class, but any other one):

package {

    import flash.display.Sprite;

    import flash.display.Stage;

    import flash.text.TextField;

  public class printText extends TextField{

  public function printText () {

  TextField(root).txt1.text = "abc"; // This didn´t work

  this_parent[txt1].text = "abc"; // Neither do this

            }

       }

  }

Any help would be really appreciate it!

Happy hollydays to everyone, thanks in advance!

This topic is closed to new replies. Start a new post to keep the conversation going.

7 replies

kglad
Community Expert
Community Expert
December 26, 2016

printText needs a reference to stage or an object in the display list.  that's always doable because what ever class invokes printText has a parent, grand parent etc that is the document class.

Participant
December 26, 2016

Thanks for your kind answer, kglad.

Sorry if I´m not fully understanding what you answered (kind of newbie in AS3).

So there is no straight way to put some text in a Textfield located on the stage from a Class? I want to use an already existing textfield, I just assumed I could write something like stage.textfieldName.text = "abc"; but I understand from your answer there is no such shortcut, right?

The solution to this is related to understand the concept of nesting in classes? When you say "reference to stage", how could I refer to my stage in order to "talk" directly to my fieldtext?

Thanks again for your time, I really appreciate it

kglad
Community Expert
Community Expert
December 26, 2016

no,, you do not nest classes.  absolutely not.

you have a document class, correct?

there is some class that references PrintText, correct?  (eg, using var pt:PrintText=new PrintText() )

what's the relationship between your doc class and PrintText?