我正在制作一个模拟泛行业的游戏,其中一个过程是绘画。
我想要做的是让玩家绘制平底锅,但我不希望使用 FILL 很容易,我希望玩家用刷子绘制平底锅,然后游戏检测是否所有区域都被绘制并让玩家前进。
对于这幅画,我打算使用那个图书馆:http ://www.nocircleno.com/graffiti/
但我不知道如何检测是否所有区域都被绘制了。有人可以告诉我一些方法吗?
我正在制作一个模拟泛行业的游戏,其中一个过程是绘画。
我想要做的是让玩家绘制平底锅,但我不希望使用 FILL 很容易,我希望玩家用刷子绘制平底锅,然后游戏检测是否所有区域都被绘制并让玩家前进。
对于这幅画,我打算使用那个图书馆:http ://www.nocircleno.com/graffiti/
但我不知道如何检测是否所有区域都被绘制了。有人可以告诉我一些方法吗?
One of the ways would be - you make a shielding BitmapData that has transparency and is opaque in place which you need your player to paint. (Color it as needed, but make sure the color is fully opaque). Then gather histogram()
then query alpha vector for 255th value, this will be the initial value for zero percent filled. These range from 0-255, so you can't use 100 or any other fixed value. Then, while the player is painting, you draw the brush over that BitmapData with blendMode
parameter set to BlendMode.ERASE
, this will net your BitmapData to gain transparency where the brush was drawn. After your player finishes drawing by any means (say, the paint is used up), you run another histogram()
over the BitmapData, and query the 255th value of alpha channel vector. 0 means the bitmap is fully transparent (or at least, only a small amount of pixels is left opaque), thus you can count a zero as 100% fill, for anything greater use the proportion.
var bd:BitmapData=new BitmapData(w,h,true,0x0); // fully transparent initial bitmap
bd.draw(yourPaintBase); // a shape that designates area to be painted. Must be fully opaque
var bm:Bitmap=new Bitmap(bd);
// position it as needed, so the area which should be painted is aligned to wherever you need
addChild(bm);
addEventListener(Event.ENTER_FRAME,doPaint);
var baseValue:int=bd.histogram()[3][255]; // Vector #3 contains alpha, #255 contains
// percentage of those pixels that have alpha of 255 = fully opaque
function doPaint(e:Event):void {
if (!areWePainting) return;
var sh:Shape=getBrush(); // shuold return an existing Shape object reference for performance
sh.x=e.localX;
sh.y=e.localY; // we are drawing where the mouse is
bd.draw(sh,null,null,BlendMode.ERASE);
decreasePaint(); // we have used some paint
if (noMorePaint()) {
e.target.removeEventListener(Event.ENTER_FRAME,doPaint);
var endValue:int=Math.floor(100*(1-bd.histogram()[3][255]/baseValue));
// aligning to percentage. This is the value you seek
reportFilledPercentage(endValue);
}
}
您可以遍历您的像素BitmapData
并用于getPixel()
检查所有这些像素的颜色是否不是白色。如果找到白色,则图像未完全绘制。
像这样的东西:
function containsWhite(bitmapData:BitmapData):Boolean
{
for(var c:int = 0; c < bitmapData.width; c++)
{
for(var r:int = 0; r < bitmapData.height; r++)
{
// Check if pixel is white.
if(bitmapData.getPixel(c, r) >= 0xFFFFFF)
{
return true;
}
}
}
return false;
}
您本质上是在处理碰撞检测问题。通过查看他们的 API,您可以尝试使用 getColorAtPoint 进行 for 循环之类的操作,并尝试确定它们已在每个像素处绘制。
如果所有其他方法都失败,请查看库使用对象的 .hitTestObject 方法生成的对象之间的冲突。
看看有人如何处理与像素的碰撞:http ://www.emanueleferonato.com/2010/08/05/worms-like-destructible-terrain-in-flash-part-2/