Reputation: 752
The documentation says that clipRect parameter of the BitmapData.draw method sets clipping for the source. Reading this I wrote an AnimatedObject class for my potential flash game. This class draws a frame from a .png animation strip, and doesn't work with Flash scene hierarchy.
var frame : Number;
public function onDraw(backBuffer : BitmapData) : void
{
var frameClipRect : Rectangle = new Rectangle(int(frame) * celWidth, 0, frameWidth, frameHeight); // set a clip rect to pick a particular frame
var matrix : Matrix = new Matrix();
matrix.translate(-frameWidth / 2, -frameHeight / 2);
if ( scale )
matrix.scale(scale, scale);
if ( angle )
matrix.rotate(Math.PI * angle / 180.0);
matrix.translate(frameWidth / 2, frameHeight / 2);
matrix.translate(x, y);
backBuffer.draw(bitmap, matrix, null, null, frameClipRect, true);
}
I expected that this would work, but there is a problem with clipping. This code actually show nothing. I played a bit and figured out that clipRect actually sets clipping for target bitmap, not for source. Which is very strange and doesn't match with the documentation.
What do you think? Does clipRect parameter of the BitmapData.draw method really clips the source? Is there a problem in my code?
Thank you!
Note: if you just omit frameClipRect parameter, then you sprites correctly transformed but showing every frame.
Upvotes: 2
Views: 3889
Reputation: 1865
I just resolve a similar problem by add a higher level display container. You need to transform the source's left-top to origin(0,0) of the higher level display container(in this case, it's wtf), then draw it to a BitmapData:
public function BitmapMC(movieClip:MovieClip)
{
curFrameBitmap = new Bitmap();
var singleBmd:BitmapData;
container.addChild(movieClip);
totalFrame = movieClip.totalFrames;
var wtf:Sprite = new Sprite();
for (var i:int = 0; i < totalFrame; i++)
{
movieClip.gotoAndStop(i);
singleBmd = new BitmapData(container.width, container.height,true, 0xFFFF0000);
var rect:Rectangle = movieClip.getBounds(container);
wtf.addChild(container);
container.x = - rect.x;
container.y = - rect.y;
singleBmd.draw(wtf);
bmdArray.push(singleBmd);
}
singleBmd = null;
}
Upvotes: 0
Reputation: 4187
I agree that the documentation seems confusing, as all other parameters to the draw() call apply to the target instead of source.
To really clip the source, you can do like
backBuffer.copyPixels(bitmap.bitmapData, sourceRect, destPoint)
Upvotes: 0
Reputation: 11
I had the same problem as you. Upon coming across your post. I tried what you said. Treating the cliprect is actually clipping the target instead of the source. My program works as intended immediately. So yes, in a way it does seems that the cliprect is for clipping the target instead of the source.
Upvotes: 1
Reputation: 6715
I never had any glitches with draw() and its clip rect. It is by the way implemented very well and gives you an important performance boost if done correct. The Flash rasterizer will only draw the parts of the clipRect.
Regarding your code: I would actually try it with and without the matrix. This could be the problem. You translate and clip. Basically clipping should happen before translation but I am not quite sure about that one.
Upvotes: 0