Lapys
Lapys

Reputation: 87

Change Alpha on Rollover; Not Changing Back on Mouse Out?

So I have a Tile class, with a sprite variable that holds the graphic for the tile. On mouse over, I ColorTransform the graphic. Seems to work fine. On mouse out, I try to change it back. Nada. And in fact, rolling over the same tile twice will increase the alpha until eventually it fades totally. Not sure how to fix it. Here's my code.

In the Tile class:

this.addEventListener(MouseEvent.MOUSE_OVER, thisMouseOver);
this.addEventListener(MouseEvent.MOUSE_OUT, thisMouseOut );

public function thisMouseOver( e:Event ):void {
    tileGraphic.bitmapData = setAlpha(tileGraphic.bitmapData);
}

public function thisMouseOut( e:Event ):void {
    tileGraphic.bitmapData = resetAlpha(tileGraphic.bitmapData);
}

private function setAlpha( bmd:BitmapData ):BitmapData {
    var rec:Rectangle = new Rectangle( 0, 0, bmd.width, bmd.height );
    var ct:ColorTransform = new ColorTransform();
    ct.alphaMultiplier = .65;
    bmd.colorTransform( rec, ct );
    return bmd;
} //end function setAlpha

private function resetAlpha( bmd:BitmapData ):BitmapData {
    var rec:Rectangle = new Rectangle( 0, 0, bmd.width, bmd.height );
    var ct:ColorTransform = new ColorTransform();
    ct.alphaMultiplier = 1;
    bmd.colorTransform( rec, ct );
    return bmd;
} //end function resetAlpha

Can anyone point me in the right direction? Thanks!

Upvotes: 0

Views: 232

Answers (2)

Vesper
Vesper

Reputation: 18747

You should better change alpha of the container instead of playing with the BitmapData pixels. For example, in your case if your tile bitmap will initially have transparent pixels (fill it with 0x00ff0000 prior to drawing a thing and check if so), they will become opaque with codingbuddha's answer. So, change the listeners to the following:

public function thisMouseOver( e:Event ):void {
    tileGraphic.alpha=0.65;
}

public function thisMouseOut( e:Event ):void {
    tileGraphic.alpha=1;
}

Upvotes: 1

codingbuddha
codingbuddha

Reputation: 707

Replace the resetAlpha with

    private function resetAlpha( bmd:BitmapData ):BitmapData {
        var rec:Rectangle = new Rectangle( 0, 0, bmd.width, bmd.height );
        var ct:ColorTransform = new ColorTransform();
        ct.alphaOffset = 255
        bmd.colorTransform( rec, ct );
        return bmd;
    } //end function resetAlpha

Upvotes: 1

Related Questions