0

I'm having a bit of a problem copying the masked only pixels from one bitmap onto another. Basically I'm masking bitmap A with bitmap B, which is working fine, but I'm unsure how to copy just the masked pixels onto Bitmap C which is the only one I want to keep.

//all this works fine

var _texture:Bitmap = new Bitmap(new Dirt_Bitmap);      
var _mask:Bitmap = new Bitmap(new Mask_Bitmap);     
var _planter:Bitmap = new Bitmap(new Planter_Bitmap);

_texture.cacheAsBitmap = _mask.cacheAsBitmap = true;
_texture.mask = _mask;

//This is where things get weird :[

var newBitmap:Bitmap = new Bitmap(new BitmapData(50, 50, true));
newBitmap.bitmapData.copyPixels(_texture.bitmapData, _texture.bitmapData.rect, new Point());

_planter.bitmapData.copyPixels(_newBitmap.bitmapData, _newBitmap.bitmapData.rect, new Point());

how would I go about just copying or drawing or maybe merg() just the masked texture so its copied over the planter graphic where the dirt should be? Any and all help will be greatly appreciated! :]

user1369030
  • 1
  • 1
  • 1

1 Answers1

1

When you use copyPixels, you are actually copying the content of a bitmap without anything that's added by environment (no masking or transforms).

Use draw() instead.

Here's a sample:

var texture:Bitmap = new Bitmap(new BitmapData(200, 200, false, 0xFFFF0000));      
var imageMask:Bitmap = new Bitmap(new BitmapData(200, 200, true, 0));
var rect:Rectangle = new Rectangle(0, 0, 10, 10);
imageMask.bitmapData.fillRect(rect, 0xFF000000);
rect.x = 50;
rect.y = 50;
imageMask.bitmapData.fillRect(rect, 0xFF000000);

texture.cacheAsBitmap = true;
imageMask.cacheAsBitmap = true;
texture.mask = imageMask;

addChild(imageMask);
addChild(texture);

var planter:Bitmap = new Bitmap(new BitmapData(200, 200, true, 0));

// that's it
planter.bitmapData.draw(texture);

addChild(planter);
planter.x = 100;
Michael Antipin
  • 3,522
  • 17
  • 32
  • thank you Nox! i had to go a step further by Draw()-ing the masked bitmap onto a temp Sprite then I used Draw() to copy everything onto the planter to give the illusion of dirt being placed in the planter. Thanks again! – user1369030 May 03 '12 at 02:28