As3: Draw overlapping rectangles to a sprite and apply alpha

五迷三道 提交于 2019-12-20 04:18:26

问题


I need to draw independet rectangles to a sprite. But the overlapping areas will get visible if I apply alpha to my sprite (the sprite will be fade in and out):

        var spBox:Sprite = new Sprite();
        this.addChild(spBox);

        spBox.graphics.beginFill(0x123456)
        spBox.graphics.drawRect(100, 100, 50, 50);
        spBox.graphics.endFill();

        spBox.graphics.beginFill(0x123456)
        spBox.graphics.drawRect(125, 125, 50, 50);
        spBox.graphics.endFill();

Is there a way to compine/flatten/merge the rectangles of my sprite? I want a seamless aplha appearence.


回答1:


I suspect that the graphics object does not support this kind of functionality for parts of its data.

If both boxes are individual DisplayObjects, you can set the .blendMode of the DisplayObjectContainer to BlendMode.LAYER, which gives the desired result. Here's some example code that refactors the drawing of a rectangle into a Box class:

var spBox:Sprite = new Sprite();
this.addChild(spBox);


var a:Box = new Box(50, 50, 0x123456);
a.x = a.y = 100;
spBox.addChild(a);  

var b:Box = new Box(50, 50, 0x123456);
b.x = b.y = 125;
spBox.addChild(b);    

spBox.alpha = .5;
spBox.blendMode = BlendMode.LAYER;

The relevant parts of the Box class look like this:

public class Box extends Shape
{
    public function Box(width:Number = 100, height:Number = 100, color:uint = 0)
    {
        graphics.beginFill(color)
        graphics.drawRect(0, 0,  width, height);
        graphics.endFill();
    }
}


来源:https://stackoverflow.com/questions/30101047/as3-draw-overlapping-rectangles-to-a-sprite-and-apply-alpha

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!