1

我没有任何成功,让背景在鼠标悬停时显示为 0.5 alpha,Sprite 作为 TextArea 的父级。我能得到的最好的结果是在 MouseOver 上以 0.5 透明度出现的文本,这完全不是我想要的。无论鼠标状态如何,我都希望文本处于最大 alpha 值,并且只有背景(Sprite)在 MouseOver 上以半透明度显示。如果可能的话,我宁愿避免补间。这是我的代码:

var textSprite:Sprite  = new Sprite();

    public function Main()
    {
        textSprite.graphics.beginFill(0x000000, 0);
        textSprite.graphics.drawRect(94.95, 80.95, 390, 130); 
        textSprite.graphics.endFill();
        textSprite.addChild(picArea1);//textarea added on stage, same dimensions, transparent background
        textSprite.buttonMode = true;
        textSprite.useHandCursor = true;
        stage.addChild(textSprite);


        textSprite.addEventListener(MouseEvent.MOUSE_OVER, applyAlpha);
        textSprite.addEventListener(MouseEvent.MOUSE_OUT, noApplyAlpha);
    }

    function applyAlpha(event:MouseEvent):void {
       textSprite.alpha = 0.5;
    }

    function noApplyAlpha(event:MouseEvent):void {
       textSprite.alpha = 0;
    }
4

1 回答 1

0

设置精灵的 alpha 也会影响它的所有子项(按设计),这是您当前的问题。

您当前的操作方式(使用图形对象绘制背景),您必须在鼠标悬停/移出时重新绘制精灵的图形。

这是您可以使用的方法:

public function Main()
{
    drawTextBG(); //a new function I made to avoid duplicating code
    textSprite.addChild(picArea1);//textarea added on stage, same dimensions, transparent background
    textSprite.buttonMode = true;
    textSprite.useHandCursor = true;
    stage.addChild(textSprite);


    textSprite.addEventListener(MouseEvent.MOUSE_OVER, applyAlpha);
    textSprite.addEventListener(MouseEvent.MOUSE_OUT, noApplyAlpha);
}

//new function I made, this draws the background and makes the transparency the value passed in. I set the default to 1 (totally visible)
function drawTextBG(bgAlpha:Number = 1):void {
    textSprite.graphics.clear();
    textSprite.graphics.beginFill(0x000000, bgAlpha);
    textSprite.graphics.drawRect(94.95, 80.95, 390, 130); 
    textSprite.graphics.endFill();
}

function applyAlpha(event:MouseEvent):void {
   drawTextBG(.5); //redraw the background with half (.5) alpha
}

function noApplyAlpha(event:MouseEvent):void {
   drawTextBG(); //redraw the background with the default value (1 - totaly visible)
}

现在,如果您使用 TLF(文本布局框架)文本字段,您可以只使用文本字段的 backgroundColor 和 backgroundAlpha 属性,而无需手动绘制背景。

于 2012-09-14T21:13:10.653 回答