2011-02-04 108 views
0

我对如何在纯AS3项目中渲染文本有点困惑。有像flash.text.StaticText这样的类,但这些只是设计者,你不能在代码中创建它们。我曾半期待Graphics课有文字渲染选项,但唉,不。在AS3中渲染文本

具体来说,我打算在每个玩家的精灵上面添加一个标签,其中包含他们的名字,健康%等。因此,我期望以某种方式使用Graphics来添加子文本元素或绘制文本...它是只读的并且不应该支持用户输入,我只想在屏幕上绘制文本。

回答

3

您可以使用TextField类。请检查参考。所有的领域和方法都是自我解释的。

一个可能的例子。

 
var myField:TextField = new TextField(); 
myField.text = "my text"; 
myField.x = 200; 
myField.y = 200; 
addChild(myField); // assuming you are in a container class 
+1

不要忘记选择= false时,它通常是忽视。 – alxx 2011-02-04 14:38:23

0

如果文本字段不工作,你可以使用这种方法创建的文字:

var format:ElementFormat = new ElementFormat(); 
format.fontSize = 26; 
format.color = 0x0000FF; 

var textElement:TextElement = new TextElement('Hello World!', format); 

var textBlock:TextBlock = new TextBlock(); 
textBlock.content = textElement; 

var textLine:TextLine = textBlock.createTextLine(null, 500); 

textLine.x = (stage.stageWidtht - textLine.width)/2; 
textLine.y = (stage.stageHeight - textLine.height)/2;  

addChild(textLine); 

看: Creating and displaying textActionScript 3.0 Developer’s Guide