将小精灵绘制到大位图

时间:2014-01-19 15:04:56

标签: actionscript-3

I found this question on Adobe's forums,但我对那里的答案不满意:

  

我有一个sprite circSprite,我在一个大位图drawingBitmap上绘制。

drawingBitmapData = new BitmapData(800, 600, true,0);
drawingBitmap = new Bitmap(drawingBitmapData);
stage.addChild(drawingBitmap);

var circSprite:Sprite = new Sprite();
circSprite.graphics.beginFill(0xFF0000,0);
circSprite.graphics.drawRect(0,0,160,160);
circSprite.graphics.endFill();
circSprite.graphics.beginFill(0xFF00FF,1);
circSprite.graphics.drawCircle(40,40,40);
circSprite.graphics.endFill();

var mat:Matrix = new Matrix();

drawingBitmapData.drawWithQuality(circSprite,mat,null,null, null,true,StageQuality.MEDIUM);
     

我想将circSpirte移到另一个位置(100,100) - 我该怎么做?

谢谢!

2 个答案:

答案 0 :(得分:1)

你可以使用你创建的矩阵来对对象进行翻译,我建议你阅读documentation on Matrix,但它是这样的:

var mat:Matrix = new Matrix();
mat.translate(100,100);

答案 1 :(得分:1)

看起来你正试图使用​​blitting。如果是这种情况,基本上有两条路线可以去。您可以使用位图数据draw()(看起来就像使用drawWithQuality())或copyPixels()copyPixels()比使用draw()要快许多倍,如果您计划大量移动对象,我建议您使用copyPixels()路线。以下是我将如何做到这一点:

您应该存储精灵的BitmapData表示,这样您只需拨打draw()一次。

//create the BitmapData variable for your sprite somewhere in member access.
private var spriteBmd:BitmapData = new BitmapData( circSprite.width, circSprite.height );
//draw after the circSprite is created
spriteBmd.draw( circSprite );

//then wherever you are doing the movement to a new position
drawingBitmapData.unlock();

//fill rect here can be replaced with a copyPixels of your background, but
//this just redraws the background to "clear" the blitting, this can be 
//optimized with a technique known as "dirty rectangles" which I'll link a tutorial to
drawingBitmapData.fillRect( stage.getBounds(stage), 0 );

//copy our sprite to a new destination point ( see new Point( 100, 100 ) )
drawingBitmapData.copyPixels( spriteBmd, spriteBmd.rect, new Point( 100, 100 ) );

//done blitting, lock the bitmapdata
drawingBitmapData.lock();

上一个答案中的另一种状态看起来像这样,但如果你进行了大量的转换,这将会慢得多:

var mat:Matrix = new Matrix();
mat.translate( 100, 100 );  
drawingBitmapData.draw( circSprite, mat );

这是一个解释blitting的教程的链接,其中讨论了“Dirty Rectangles”(在我的评论中注明):

AS3 Blitting Tutorial