开发者

Diffrent values of alpha for different regions of a sprite(Flex/Actionscript)

开发者 https://www.devze.com 2022-12-21 00:51 出处:网络
Here is my problem : I have a Sprite covering the entire scr开发者_JS百科een. Other than a certain rectangle of this sprite

Here is my problem :

I have a Sprite covering the entire scr开发者_JS百科een.

Other than a certain rectangle of this sprite

I want the rest of the sprite to have alpha = 0.5.

And for that particular rectangle, I want alpha = 0.

Is it possible? How?


Perhaps using a combination of LAYER and ERASE BlendMode will do what you want:

Example:

import flash.display.BlendMode;
import flash.display.Graphics;
import flash.display.Sprite;
import flash.geom.Rectangle;

public class PunchTest extends Sprite {
    private var punchSprite:Sprite = new Sprite();

    public function doPunch(target:Sprite, rect:Rectangle):void {
        var g:Graphics = punchSprite.graphics;
        g.clear();
        g.beginFill(0, 1);
        g.drawRect(rect.x, rect.y, rect.width, rect.height);
        g.endFill();
        punchSprite.blendMode = BlendMode.ERASE;
        target.blendMode = BlendMode.LAYER;
        target.addChild(punchSprite);
    }

    public function PunchTest() {
        super();
        var g:Graphics = graphics;

        // draw a red backgound
        g.beginFill(0xff0000, 1);
        g.drawRect(0, 0, 500, 500);
        g.endFill();

        // add a blue coverring sprite at 0.5 alpha
        var coverSprite:Sprite = new Sprite();
        g = coverSprite.graphics;
        g.beginFill(0x00ff00, 0.5);
        g.drawRect(50, 50, 300, 300);
        g.endFill();
        addChild(coverSprite);

        // do a hole into the cover sprite
        doPunch(coverSprite, new Rectangle(80, 80, 100, 100));
    }
}


Well, most simple solution is to give the sprite a semi-transparent mask, and set cacheAsBitmap to true for both the mask and the Sprite.

0

精彩评论

暂无评论...
验证码 换一张
取 消