您需要命名您的对象,以便您可以针对它们进行编程。因为空格会导致错误的变量名称,所以我们将对象命名如下:
- 第 1 层 =
blue
- 第 2 层 =
green
- 第 3 层 =
person
解决方案
您需要一个遮罩,但与普通遮罩不同,您需要遮罩形状的反转(即,内部是隐藏,而不是内部是显示) .您可以使用BlendMode.ERASE 实现此目的。然后,只需匹配person 的位置和尺寸;每当您移动 person 时,也要更新遮罩的位置。
function make(color:uint, width:Number, height:Number, x:Number = 0, y:Number = 0, existingShape:Sprite = null):Sprite {
// Helper function to make squares. Will create a new one, or draw on one provided.
var s:Sprite;
if (existingShape != null) {
s = existingShape;
} else {
s = new Sprite();
}
s.graphics.beginFill(color, 1);
s.graphics.drawRect(x, y, width, height);
s.graphics.endFill();
return s;
}
// Layer 1
var blue:Sprite = make(0x3f48cc, 250, 400);
addChild(blue);
// Layer 2
var green:Sprite = make(0x00cc99, 250, 400);
addChild(green);
green.blendMode = BlendMode.LAYER;
// Layer 3
var person:Sprite = make(0xea0502, 20, 100, 20); // arms
make(0xea0502, 60, 20, 0, 20, person); // head and legs
addChild(person);
person.x = 95;
person.y = 150;
// This is the mask. It must be a child of the object it is masking.
var boundary:Sprite = make(0xff00e4, person.width, person.height);
green.addChild(boundary);
boundary.x = person.x;
boundary.y = person.y;
boundary.blendMode = BlendMode.ERASE;
结果