PDA

View Full Version : [AS3] Textfield in a Subclass


knalpot
08-24-2006, 07:43 PM
Hi
I'm working on a AS3 Project in Flex 2 and I've been looking for hours for a solution to a (I'm sure very simple) problem: it's a pure AS3 Project, so no mxml file, and I try to output text in a textfield created in a subclass. I've read a lot of posts and the documentation and whatever I could get hold of, but it only outputs when I define the textfield in the main application class.

If I define the field in a subclass, which is imported properly in the main class with the "new" statement instantiated, nothing happens. To illustrate it, take these examples:

Works perfect:
package {
import flash.display.Sprite;
import flash.text.*;

public class whynot extends Sprite
{
private var display_txt:TextField;

public function whynot()
{
display_txt = new TextField();
display_txt.text = "a text";
addChild(display_txt);
}
}
}
No output from this:
Filename: whynot.as (the main application class):
package {
import flash.display.Sprite;
import OutputTheThing;

public class whynot extends Sprite
{
private var theText:OutputTheThing;

public function whynot()
{
theText = new OutputTheThing();
}
}
}

Filename OutputTheThing.as:
package
{
import flash.display.Sprite;
import flash.text.*;

public class OutputTheThing extends Sprite
{
private var display_txt:TextField;

public function OutputTheThing()
{
display_txt = new TextField();
display_txt.text = "a text";
addChild(display_txt);
}

}
}

Any ideas? I feel very stupid, I must have missed some basic idea of actionscript programming.
Thanks for any help,
Phil

TheCanadian
08-24-2006, 07:47 PM
public function whynot() {
theText = new OutputTheThing();
addChild(theText);
}

knalpot
08-24-2006, 07:55 PM
good solutions are alway simple.
thank you for the quick reply, it works.
phil