views:

37

answers:

1

Hi,

Is there a way to add text programmatically in as3 to a Sprite or a MovieClip without using the class TextField

TextField inherits from InteractiveObject which is kind of heavy for what I want to do: just display text (i.e. I don't want to interact with the text).

Note: I'm aware that there is a property selectable to make the textfield not selectable. This is not the point.

Thank you

+5  A: 

There are two ways that come to mind. The first being this whereby a string is written to bitmap data: http://stackoverflow.com/questions/3435543/how-to-draw-a-string-on-bitmapdata

Second, you could try this fast font rendering library (although I have not tried it myself) lab.polygonal.de/2009/12/15/font-rendering-with-the-fp10-drawing-api/

Both these solutions seem to bypass the need for creating a textfield (except the first where it gets used to create but then is discarded).

Allan
@Allan. +1 for the second link. Just a note. If anyone wants to use the first option and plans to call this method repeatedly, it'd be a good idea to set the `TextField` intance as a parameter. That way you could reduce the number of instances created and control font type, color and other properties from the calling code. I'm not sure this method is going to give a significant performance gain, though (just wrote it to show how you could draw text to a Bitmap). Since the OP seems to be looking for performance, the second option, although more involved, should be more appropriate, I think.
Juan Pablo Califano
I agree that using a bitmap is the best solution, but I'd further note that there is usually no need to actually draw into your own bitmap. Just create your textfield and set the `cacheAsBitmap` flag, and you get nearly all the benefits of a bitmap with none of the inconveniences.
fenomas