Create Image in AS3
Is it possible to create an image in Flash AS3 that c开发者_StackOverflow中文版ontains a specifik bagroundcolor and som text that the user as entered? And then use this image in runtime?
// nicke
Here is a bit of pseudo-code that could help you draw a the graphics and create a bitmap object of it.
w = 400;
h = 400;
var shape:Shape = new Shape();
shape.graphics.beginFill( 0xFF0000, 1 );
shape.graphics.drawRect( 0, 0, w, h );
shape.graphics.endFill();
var field:TextField = new TextField();
field.text = someUserInput;
shape.addChild( field );
var bitmap:Bitmap = new Bitmap();
var bitmapData:BitmapData = new BitmapData( w, h );
bitmapData.draw( shape );
bitmap.bitmapData = bitmapData;
addChild( bitmap );
From here you can use the Adobe core libs to create an actual jpeg or png file:
var jpgEncoder:JPGEncoder = new JPGEncoder( quality );
var jpgStream:ByteArray = jpgEncoder.encode( bitmapData );
Sure:
function createTextBox
(
width : Number,
height : Number,
bgColor : uint,
text : String,
texFormat : TextFormat ) : Sprite
{
var sprite : Sprite = new Sprite();
var shape : Shape = new Shape();
var textField : TextField = new TextField();
//background
shape.graphics.beginFill( bgColor );
shape.graphics.drawRect( 0, 0, width, height );
sprite.addChild( shape );
//textfield
textfield.width = width;
textfield.height = height;
textfield.multiline = true;
textfield.wordWrap = true;
//up to you if it serves your needs
textfield.embedFonts = true;
textField.selectable = false;
textField.defaultTextFormat = textFormat;
sprite.addChild( textField );
return sprite;
}
var format : TextFormat = new TextFormat();
format.color = 0xFF0000;
format.size = 12;
var myText : String = "A woman is just a woman but a good cigar is a smoke!"
var sprite : Sprite = createTextBox( 200, 200, 0x999999, myText, format );
addChild( sprite );
//if you want, you can covert this to a bitmap:
var bitmapData : BitmapData = new BitmapData( 200, 200, false );
bitmapData.draw( sprite );
var bitmap : Bitmap = new Bitmap( bitmapData );
bitmap.x = 200;
addChild( bitmap );
精彩评论