开发者

Rendering text in AS3

开发者 https://www.devze.com 2023-02-08 06:58 出处:网络
I\'m having a bit of confusion about how to render text in a pure AS3 project. There are classes like flash.text.StaticText but these are designer开发者_开发问答-only, you can\'t create them in code.

I'm having a bit of confusion about how to render text in a pure AS3 project. There are classes like flash.text.StaticText but these are designer开发者_开发问答-only, you can't create them in code. I was half-expecting the Graphics class to have text-rendering options but alas, no.

Specifically I was going to add a label above each player's sprite with their name, health %, etc. So I expected to add a child text-element or draw text using Graphics in some way... it's read-only and should not support user-input, I just want to draw text on-screen.


You can use TextField class for this. Please check the reference. All fields and methods are self explanatory.

A possible example.

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


If TextField doesn't work, you can create text using this method:

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);

Look at: Creating and displaying text in ActionScript 3.0 Developer’s Guide

0

精彩评论

暂无评论...
验证码 换一张
取 消