How do I draw text in an ActionScript 3 sprite?

29,709

Solution 1

I would go with something more low-level than Label. Use a TextField and add it as a child to the Sprite:

var text:TextField = new TextField();
text.text = "hello world";
addChild(text);

Note: your text will not show up if the Sprite is rotated and the fonts are not embedded.

Solution 2

you should read about the display list

var s:Sprite = new Sprite(); 
var txt:TextField = new TextField(); 
txt.text ="here is same text"; 
s.addChild(txt); 
Share:
29,709
PEZ
Author by

PEZ

A screen evangelist. Nay for SO:s grumpy old men closing and deleting good programming related content out of pure grumpyness.

Updated on July 18, 2022

Comments

  • PEZ
    PEZ almost 2 years

    I have some sprites that the users can manipulate, drag around and resize. Now I'd like to be able to display text in those sprites. I've tried lots of, probably stupid, ways. Like inheriting from Label and adding a Label child to the sprite, but no text shows up.

    One disturbing thing: Inheriting from Label I get the text to show up if I run in the debugger and inspect my Label subclass instance.

    I have this feeling I'm missing something really obvious. How is this done, the proper way?

  • PEZ
    PEZ about 15 years
    Thanks! This was starting to drive me nuts. Any idea why it doesn't work using a Label?
  • PEZ
    PEZ about 15 years
    That doesn't work for some reason. Adding a TextField works, but has some side effects that I now must sort out.