actionscript 3.0可见遮罩对象上的可见遮罩

时间:2015-05-01 13:43:37

标签: actionscript-3 mask

我的游戏中有一辆移动的卡车,我希望城市的灯光反射(不是背景)在它移动时可以看到它的身体。

卡车后面的东西无关紧要,一些树木和建筑物。

所以, 我添加背景(不重要); 然后我把卡车加到舞台上; 然后我添加另一个整个舞台大小的电影剪辑(城市灯光和东西),我把它设置为我卡车的遮蔽物。

我不希望卡车消失,我只是希望屏蔽物体显示出来并具有一定的透明度;

我知道屏蔽并将我的影片剪辑作为位图,但它不适用于我的情况;因为这里卡车完全可见,面罩半透明。

请帮助我,我已经没时间了:/

修改

这是我当前的代码基于其中一个asnwers:

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

var Mask: Sprite = new Sprite()
Mask = Sprite(buildingHolder);
Mask.alpha = 1;
Mask.cacheAsBitmap = true;
this.addChild(Mask);

var reflection: MovieClip = new nightSky1Mask;
reflection.x = 0;
reflection.y = 480;
reflection.alpha = .6;
reflection.scaleX *= 320 / reflection.width;
reflection.scaleY = reflection.scaleX;
reflection.cacheAsBitmap = true;
reflection.mask = Mask;
this.addChild(reflection);

for (var i: int = 0; i <= 29; i++){
//some codes are deleted here

    var image: MovieClip = new level1Images[i];
    image.x = // deleted
    image.y = // deleted
    image.scaleX *= 30 / image.width * 2;
    image.scaleY *= 5 / image.width * 2;
    buildingHolder.addChild(image)
    currLevelImages.push(image)
}

1 个答案:

答案 0 :(得分:0)

DON&#39; 想掩盖卡车。你要掩饰的是城市之光。

你需要创建一个卡车的副本,并用它掩盖城市的灯光。

假设您的库中有一个位图,导出的类为TruckImage,并且您的灯被导出为动作脚本为Lights,您可以执行以下操作:

var truck:Bitmap = new Bitmap(new TruckImage());

//scale and position however you'd like
truck.scaleX = truck.scaleY = .25;
truck.y = 100;
addChild(truck);    

var theMask:Bitmap = new Bitmap(truck.bitmapData);
theMask.cacheAsBitmap = true;//!very important
theMask.scaleX = theMask.scaleY = truck.scaleX;
addChild(theMask); //also important

lights = new Bitmap(new Lights());
lights.cacheAsBitmap = true;//!very important
lights.mask = theMask;
addChild(lights);

//let's move the truck and the mask every frame
addEventListener(Event.ENTER_FRAME,enterFrame);
function enterFrame(e:Event):void {
    truck.x = (truck.x + 2) % stage.stageWidth;
    theMask.x = truck.x;
}

修改

尝试这样的事情:(创建建筑物的位图副本)

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

    //create a bitmap data object and draw the building holder sprite into it (making a copy)
    var maskBMD:BitmapData = new BitmapData(buildingHolder.width,buildingHolder.height,true,0x00000000);
    maskBMD.draw(buildingHolder);

    var msk:Bitmap = new Bitmap(maskBMD,"auto",true);
    msk.cacheAsBitmap = true;
    this.addChild(msk);

    var reflection:MovieClip = new nightSky1Mask;
    reflection.x = 0;
    reflection.y = 480;
    reflection.alpha = .6;
    reflection.scaleX *= 320 / reflection.width;
    reflection.scaleY = reflection.scaleX;
    reflection.cacheAsBitmap = true;
    reflection.mask = msk;
    this.addChild(reflection);

    for (var i: int = 0; i <= 29; i++){
    //some codes are deleted here

        var image: MovieClip = new level1Images[i];
        image.x = // deleted
        image.y = // deleted
        image.scaleX *= 30 / image.width * 2;
        image.scaleY *= 5 / image.width * 2;
        buildingHolder.addChild(image)
        currLevelImages.push(image)
    }

或者,您可以执行以下操作来制作建筑物的两个副本:

    var buildingHolder:Sprite = createBuilding();
    var buildingHolderMask:Sprite = createBuilding(false);

    function createBuilding(addToArray:Boolean = true):Sprite {
        var building:Sprite = new Sprite();

        for (var i: int = 0; i <= 29; i++){
        //some codes are deleted here

            var image: MovieClip = new level1Images[i];
            image.x = // deleted
            image.y = // deleted
            image.scaleX *= 30 / image.width * 2;
            image.scaleY *= 5 / image.width * 2;
            building.addChild(image)
            if(addToArray) currLevelImages.push(image)
        }
        return building;
    }