我有一个扩展Sprite类的Avatar类。我还有一个Room类,其中包含“可步行”区域和非步行区域的位图,以及房间的艺术品本身。
我需要确保用户只能在房间的白色/透明部分“面具”上行走,而不会实际显示黑白面具。我可以使用哪些功能来确保用户不会快速进入面罩的黑色部分?
答案 0 :(得分:1)
你只需要这个' walkable'内存中的位图
private const ALLOWANCE:Number = .1;
private function isTurnAllowed(maskBMD:BitmapData, position:Point):Boolean
{
//0xAARRGGBB
var color32:uint = maskBMD.getPixel32(int(position.x), int(position.y));
var alpha:uint = color32 >> 24 & 0xff;
var red:uint = color32 >> 16 & 0xff;
var green:uint = color32 >> 8 & 0xff;
var blue:uint = color32 & 0xff;
var color24:uint =color32 >> 8 & 0xffffff;
/*
if (alpha == 0 || color24 == 0xffffff) return true
strictly speaking this string is enough but in real your bitmap mask after resampling can have some interpolation artifacts so you need some allowance to pass not strictly white.
*/
var absoluteLightness:Number = red + green + blue + (0xff - alpha);//transparent is 0x00
var maximalLight:Number = 0xff * 4;
var lightness:Number = absoluteLightness / maximalLight;
if (lightness > 1 - ALLOWANCE)
return true
else
return false
}
答案 1 :(得分:1)
让我们说你的头像:精灵在它自己的(0,0)点附近正确对齐。然后创建一个Walkable:Sprite,其形状为可步行区域。 必须附加到显示列表,但不一定可见,您可以设置Walkable.visible = false。
function moveBy(dx:Number, dy:Number):void
{
var aPoint:Point = new Point();
aPoint.x = Avatar.x + dx;
aPoint.y = Avatar.y + dy;
// hitTestPoint works with Stage coordinates.
aPoint = Avatar.parent.localToGlobal(aPoint);
if (Walkable.hitTestPoint(aPoint.x, aPoint.y, true))
{
Avatar.x += dx;
Avatar.y += dy;
}
}
此代码非常简单,只是不允许将您的Avatar移出Walkable地图。