0
votes

I'm trying to create a TextField dynamically in ActionScript 3. The code below draws the text box itself (which in this case is just a red rectangle), but the string "Add text here" is not displayed. What can I do to fix this problem?

var d:Number = mc.getNextHighestDepth();

var x:Number = (screenWidth - boxWidth) / 2;
var y:Number = (screenHeight - boxHeight) / 2;

var w:Number = boxWidth;
var h:Number = boxHeight;

notification = mc.createTextField("Text", d, x, y, w, h);
notification.background = true;
notification.backgroundColor = 0xFF0000;
notification.selectable = false;
notification.wordWrap = true;
notification.text = "Add text here";
1
Make sure you embed the font you set for the TextField. You assign a TextFormat object to the defaultTextFormat property of the TextField and also call the setTextFormat(format) method of the TextField when changing fonts. Posting your mc.createTextField method source would also help, as we can't really see what you do in there. As @Discipol answered, you'll also need to add any kind of DisplayObject to the DisplayList to make it visible, but if the red rectangle is drawn I assume you already added it. - Gio
What do you mean post the source of ms.creatyeTextField? It's the standard MovieClip.createTextField method. - Paul Manta
Oh, sorry I couldn't catch that you're actually using AS2 API and I thought you had a child class of MovieClip. So in Actionscript 3 you don't create a TextField like that. The correct way to create a TextField in AS3 is as follows: var tf:TextField = new TextField(); Then you add it as a child to a parent DisplayObjectContainer and set a TextFormat if needed. - Gio

1 Answers

1
votes

you need to do addChild( notification ) so that the textField is added to the display list.