glushcha
glushcha

Reputation: 3

Drawing small sprite to large bitmap

I found this question on Adobe's forums, but I'm not happy with the answer there:

I have a sprite circSprite that I draw on a large bitmap 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);

I would like to move the circSpirte to a different position ( 100,100 ) - how can I do that?

Thanks!

Upvotes: 0

Views: 154

Answers (2)

Bennett Keller
Bennett Keller

Reputation: 1714

It looks like you are trying to use blitting. If that's the case there are basically two routes to go. You can either use the bitmap data draw() ( looks like you are using drawWithQuality() )or copyPixels(). copyPixels() is many times faster than using draw(), if you are planning on moving your objects around a lot, I'd recommend going the copyPixels() route. And here is how I would do that:

You should store a BitmapData representation of your sprite so you only have to call draw() once.

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

The other way state in the previous answer looks something like this, but if you are doing a lot of transformations this will be quite a bit slower:

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

Here's a link to a tutorial explaining blitting which talks about "Dirty Rectangles" (noted in my comments):

AS3 Blitting Tutorial

Upvotes: 1

fixmycode
fixmycode

Reputation: 8506

you can use the matrix that you created to perform a translation on the object, I would recommend that you read the documentation on Matrix but it goes like this:

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

Upvotes: 1

Related Questions