2012-09-14 68 views
1

我没有任何成功,获得背景以鼠标悬停的0.5 alpha显示,Sprite作为TextArea的父级。我能得到的最好的结果是出现在MouseOver的0.5透明度上的文本,这完全不是我正在寻找的。无论鼠标状态如何,我都希望文本处于最大阿尔法值,并且只有背景(Sprite)在MouseOver上以半透明度显示。如果可能,我宁愿避免补间。这是我的代码:As3鼠标悬停后台无法按预期方式工作

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

回答

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属性和放弃绘画手动背景。

+0

@John虽然这是真的,但最好在textSprite的TextField下添加另一个sprite,并将其更改为alpha。 – Gio

+0

我不认为它真的很重要。从性能角度来看,这实际上是“更好”,无论如何。也就是说,如果这是我自己的项目,我可能会将它作为下面的一个不同的精灵,让它接收鼠标事件而不是textField或父项。但问题不在于...... – BadFeelingAboutThis

+0

作为一个常常用ActionScript搞笑的人,我不知道你们在谈论什么。这工作正常,但@LondonDrugs_MediaServices。谢谢! –

相关问题