Copy link to clipboard
Copied
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!
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
Thanks for your answer, kglad. You guide me brilliantly in the right direction!
I don´t have a document class, but after thinking about your last message, I write this on frame 1 of the stage timeline:
var newPt = new printText ();
txt1.text = printText.stopPoints[0]; //"stopPoints" is an array I have in my class with some values in it.
This works like a charm. And if I put that inside a document class, it works perfect too, no need to write anything in the timeline. So far, so good.
Now, suppose I want every line of code inside my Class printText. No timeline, no document class.
Is that doable? Or I have no option, but to call the Class from the document class? I´m sorry, I´m still a little confused here, but I think I´m closer to match the pieces now.
Thanks again!
Copy link to clipboard
Copied
the only class automatically included in your project is you document class or classes you import on your timeline.
if have a simple project, coding on the timeline is fine. but as a project increases in complexity, the benefits of using a class structure increase.
putting all your significant code in one class (which will have to be your document class) is not much better than timeline coding. the big benefits accrue as your projects increase in complexity and you increasingly compartmentalize your code in different classes.
Copy link to clipboard
Copied
Crystal clear, kglad, thank you very much for your time!
Copy link to clipboard
Copied
you're welcome.
(p.s when using the adobe forums, please mark helpful/correct responses, if there are any.)
Find more inspiration, events, and resources on the new Adobe Community
Explore Now