2

我知道如何画一个正方形:

graphics.moveTo(0, 0);
graphics.beginFill(0x00ff00, 1);
graphics.lineTo(point, 0);
graphics.lineTo(point, point);
graphics.lineTo(0, point);
graphics.lineTo(0, 0);
graphics.endFill();

现在我将如何在第一个正方形内绘制另一个 alpha 为 0.5 的正方形?

4

3 回答 3

6

以下代码段将绘制一个绿色正方形,然后在其中心减去一个正方形(现场演示)。

// Size of the main square
var point:uint = 100;

// Create two sprites
var s1:Sprite = new Sprite();
var s2:Sprite = new Sprite();

// Shortcuts to graphics objects
var g1:Graphics = s1.graphics;
var g2:Graphics = s2.graphics;

// Draw the main square
g1.beginFill(0x00ff00, 1.0);
g1.drawRect(0, 0, point, point);
g1.endFill();

// Draw the eraser square 
g2.beginFill(0x000000, 0.5);
g2.drawRect(0, 0, point/2, point/2);
g2.endFill();

// Configure blend modes to erase the center of the first square
s1.blendMode = BlendMode.LAYER;
s2.blendMode = BlendMode.ERASE;

// Add the eraser square to the first one and adjust its position
s1.addChild(s2);
s2.x = s2.y = point/4;

// Add the first square to the stage
addChild(s1);
于 2012-10-04T13:11:01.467 回答
2
var squ1:Sprite = new Sprite();
with(squ1.graphics){
    beginFill(0xFF0000);
    drawRect(0,0,100,100);
    endFill();
}
addChild(squ1);
var inSq:Sprite = new Sprite();
with(inSq.graphics){
    beginFill(0x00FF00,.5);
    drawRect(0,0,25,25);
    endFill();
}
squ1.addChild(inSq);
inSq.x = squ1.width/2 - inSq.width/2;
inSq.y = squ1.height/2 - inSq.height/2;
于 2012-10-04T13:15:28.407 回答
2

无需使用任何混合模式,您可以通过在结束填充之前绘制(擦除)所需部分来排除绘制形状的部分。

这是一个例子:

import flash.display.Shape;

var squares:Shape = new Shape();
squares.graphics.beginFill(0xFF0000, 1.0);
squares.graphics.drawRect(0, 0, 200, 200);
squares.graphics.drawRect(50, 50, 100, 100); //exclude
squares.graphics.endFill();
squares.graphics.beginFill(0x0000FF, 0.5);
squares.graphics.drawRect(75, 75, 100, 100);
squares.graphics.endFill();

addChild(squares);

在此处输入图像描述

于 2012-10-08T00:10:18.107 回答