as3 - render text straight to Sprite.graphics?

醉酒当歌 提交于 2020-01-01 18:19:16

问题


Because I'm a terrible, bad person who likes to do things differently for no reason, I'd love to be able to do something like mySprite.graphics.drawText(...).

As I understand things, the only way to get text currently is to create a TextField and add it as a child of mySprite. In my particular situation I'd rather not do that.

Any advice appreciated!

ooo


回答1:


If you wanted to wrap it up in a method of Sprite I would simply extend the Sprite class, add the function drawText to it. Within that method add the TextField etc. But it doesn't sound like you want that.

In which case have a look here http://lab.polygonal.de/?p=916

They have created a collection of code that can render fonts without, well, fonts. There are disadvantages like file size, but it is possible.




回答2:


Wrong bzzzzzzzt create a new BitmapData object, create a bitmap, adding the bitmapData to it and then adding that bitmap as a child to your sprite. Example:

var myTextImage:BitmapData = new BitmapData(textField.width, textField.height, true, 0x000000ff);

myTextImage.draw(textField);

mySprite.addChild(new Bitmap(myTextImage));

stage.addChild(mySprite);

I just made that code up so you'll have to adapt it but the principle should be more than clear enough to adapt it to your project.




回答3:


Create a new BitmapData object : bitmapdata = new BitmapData(txt.width, txt.height, true, 0x000000ff);

Draw your textfield on it : bitmapdata.draw(txt);

And then use graphics class and it works !

sprite.graphics.beginBitmapFill(bitmapdata);
sprite.graphics.drawRect(0,0,txt.width,txt.height);
sprite.graphics.endFill();

C ya



来源:https://stackoverflow.com/questions/5722561/as3-render-text-straight-to-sprite-graphics

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!