views:

202

answers:

2

Here is my problem :
I have a Sprite covering the entire screen.
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?

A: 

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.

greetz

back2dos

back2dos
A: 

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));
    }
}
Patrick