【问题标题】:Rendering text in AS3在 AS3 中呈现文本
【发布时间】:2011-02-04 09:48:15
【问题描述】:

我对如何在纯 AS3 项目中呈现文本有些困惑。有像flash.text.StaticText 这样的类,但这些都是设计者专用的,你不能在代码中创建它们。我有一半期望 Graphics 类有文本渲染选项,但可惜,没有。

具体来说,我打算在每个玩家的精灵上方添加一个标签,上面有他们的名字、健康百分比等。所以我希望以某种方式添加一个子文本元素或使用Graphics 绘制文本......它是读取-只有并且不应该支持用户输入,我只想在屏幕上绘制文本。

【问题讨论】:

    标签: actionscript-3


    【解决方案1】:

    您可以为此使用TextField 类。请检查参考。所有字段和方法都是不言自明的。

    一个可能的例子。

    var myField:TextField = new TextField(); myField.text = "我的文字"; myField.x = 200; myField.y = 200; addChild(myField); // 假设你在一个容器类中

    【讨论】:

    • 别忘了 selectable = false,它通常被忽略。
    【解决方案2】:

    如果 TextField 不起作用,您可以使用此方法创建文本:

    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 text in ActionScript 3.0 Developer’s Guide

    【讨论】:

      猜你喜欢
      • 2012-12-13
      • 2011-04-18
      • 1970-01-01
      • 2021-04-17
      • 1970-01-01
      • 1970-01-01
      • 2012-05-03
      • 2012-10-29
      • 2010-11-14
      相关资源
      最近更新 更多