0
votes

I have a moving truck in my game and I want the city lights reflections (NOT the background) to be visible over its body as it moves around.

What stands behind the truck does not matter, some trees and buildings.

So, I add the background (not important); then I add the truck to the stage; then I add another MovieClip with the size of the whole stage (city lights and stuff) and I set it as a masking object for my truck.

I dont want the truck to disappear, i just want the masking object show up over it with some transparency;

I know about masking and also catching my movieclips as bitmaps but it wont work in my case; because here the truck stays totally visible and the mask semi-transparent.

Please help me, I'm running out of time:/

EDIT

Here is my current code based off one of the 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
Show your current code. You need to create a mask for the city lights MovieClip because you don't want to mask the truck, you want to mask the lights. If your truck is a png or bimtap type object with transparency, it's pretty easy to create a copy that can be your mask for the lights. If you show your code, a specific example can be given. - BadFeelingAboutThis
I think the problem here is that this Mask is the buildingHolder itself, I mean when i set its alpha to 0.1 I cannot see the buildingHolder behind it. They are exactly the same not copies! @LDMS - innerUFOmaker
Yes, casting an object doesn't copy it. What you are doing is creating a new sprite (in the Mask var), then right after that assigning the var the same object that is in the buildingHolder var. One object, two vars referencing it. - BadFeelingAboutThis

1 Answers

0
votes

You DON'T want to mask the truck. What you want to mask are the city lights.

You need to create a copy of the truck, and mask the city lights with that.

Assuming you have a Bitmap in your library with exported class as TruckImage and your lights are exported for actionscript as Lights, you can do something like this:

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

EDIT

Try something like this: (creating a bitmap copy of your building)

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

Or, you could do something like this to make two copies of your building:

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