简体   繁体   中英

Keeping text the same colour when changing colortransform of movieclip AS3

I'm making a card game and have gotten to the point where cards that are "destroyed" change to the team of which they were destroyed by. I've only got the code to check the left grid cell next to a placed card at the moment:

if (SCaL > aR) 
{
    trace("Winner!")
    colorTransform2.color = 0
    for (var i:int = 0; i<buttons[foundL].getChildAt(0).numChildren; i++)
    {
        DisplayObject(buttons[foundL].getChildAt(0).getChildAt(i)).transform.colorTransform = colorTransform2
        trace(DisplayObject(buttons[foundL].getChildAt(0).getChildAt(i)).transform.colorTransform.color)
    }
    colorTransform2.color = selectedCard.color

    DisplayObject(buttons[foundL].getChildAt(0).graphics)


}

This works fine except for one problem: the text inside the movieclip change colour as-well

I've tried changing the text colour transform after I change the cards colour but that does nothing, I also can't find anything relating to this problem online.

When you apply a color transform, it affects all of the children too. It doesn't actually change the color transform of the child objects, so you can't undo it by changing properties of the children. Rather, the rendering system applies any transforms to the a display object's children.

The way to avoid is to not have the text field a child of whatever thing you're changing the color of.

In your case, you could add an extra sprite or movieclip layer, so that the top movieclip/sprite contains the whole card, which then contains two movieclips/sprites, one with things you want to change color and one with things you want to stay the same color.

As already said, colorTransform applies to the whole tree of a DisplayObject, thus all it's children/grand-children, so unwanted transform applied to display objects is the sign of badly designed display hierarchy.

You may change it by adding the descendent to another container, which will cancel the color transform, but you have to be careful with display order, position, scale, when changing parent

In some cases, you might apply an inverse tranformation but generally, colorTransform applies to discreet color values (integer), clamped beetween [0..255] so you will often lose so much information it will be impossible to inverse while keeping acceptable quality (ex you could inverse new ColorTransform(0.5,0.5,0.5) by applying new ColorTranform(2.0,2.0,2.0) but that's pretty much all you can do. Any coefficient 0 or close to 0 will trash (almost) all info from the corresponding color channel, coeff greater than 1 will clip values, etc)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM