我有一个由 BitmapData 组成的字段,用于像素精确的命中检测。
但是,BitmapData 自然地为每个像素存储 2^32(或 2^24 没有 alpha?)的可能性。我只需要 2 - 黑色或白色。
但我仍然需要使用 .draw 将其他对象绘制到该 BitmapData 上。它不需要可见。
提取一个像素以进行命中检测似乎并不难——但在不循环遍历每个像素的情况下进行绘制似乎很难。可能吗?
解决这个问题的正确方法是什么?
我有一个由 BitmapData 组成的字段,用于像素精确的命中检测。
但是,BitmapData 自然地为每个像素存储 2^32(或 2^24 没有 alpha?)的可能性。我只需要 2 - 黑色或白色。
但我仍然需要使用 .draw 将其他对象绘制到该 BitmapData 上。它不需要可见。
提取一个像素以进行命中检测似乎并不难——但在不循环遍历每个像素的情况下进行绘制似乎很难。可能吗?
解决这个问题的正确方法是什么?
If you depend on having your bitmap data to be black or white only, you can employ BitmapData.threshold()
after drawing a new mask over that bitmap. To turn your existing BitmapData
to black and white with a threshold of half red channel do the following:
bd.threshold(bd,bd.rect,new Point(),"<",0x00800000,0x0,0x00ff0000,true);
bd.threshold(bd,bd.rect,new Point(),">=",0x00800000,0x00ffffff,0x00ff0000,true);
The first call with turn all points that have red below 0x80 black, the second will turn all the remaining points white. Change the mask and threshold value to use green or blue channels if you want. Consider applying a properly channeled ColorTransform
object to your draw
calls to make the mask correctly applied to a newly drawn object.