2013-03-05 44 views
4

我的Flash项目中有StaticText字段,当鼠标悬停在它们上方时,我需要运行一些代码。所以我试过这个代码如何检测AS3中的StaticText?

stage.addEventListener(MouseEvent.MOUSE_OVER, mouseRollOver); 
    function mouseRollOver(event:MouseEvent):void { 
    var tf:StaticText = event.target as StaticText; 
    if (tf){ 
    //my code 
    } 
} 

但它不起作用。当我使用动态文本字段并在var tf中用TextField替换StaticText时,它工作正常。我还认为,如果我可以让鼠标检测到不是StaticText作为目标,但是某种具有某些文本属性的对象(如“selectable”设置为true),我可以使用静态文本字段来处理这个事情,但是我不能'弄清楚如何做到这一点。无论如何,我需要以某种方式检测静态文本字段作为目标。任何帮助,将不胜感激。
在此先感谢

+1

最简单的方法:将所有textfirlds封装在movieclip中(全部在一个或一个),并使用它。您可以使用DisplayObject的hitTest函数检测碰撞 – Smolniy 2013-03-05 14:40:21

+0

您可以使用[TextSnapshot](http://help.adobe.com/zh_CN/FlashPlatform/reference/actionscript/3/flash/text/TextSnapshot.html)类来处理静态文本字段,但它们只是只读的,不能设置文本。你仍然可以使用[hitTestTextNearPos](http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/text/TextSnapshot.html#hitTestTextNearPos()) – 2013-03-05 14:54:48

回答

2

您最好的选择是将静态文本框放入动画片段,然后为其分配您的代码。静态文本框没有实例名称,无法操作。

0

这很难做到。看到这个链接enter link description here 正如你所看到的,你可以检查DisplayObject是否为StaticText,以及通过检查mousX和MouseY属性,你可以发现翻转是否与这个字段有关。通过如果你使用动态文本,并取消选择的领域你会得到作为StaticField

编辑 这是解释我的意思是一个文本框: 让我们有一个静态文本字段成黑色Flash文档中的阶段。

var myFieldLabel:StaticText 
var i:uint; 

//This for check for all staticFields in state and trace its text. It is possible and it is working. I my case I have only one field and I get reference to it in myFieldLabel:StaticText var. Also I change it's alpha to 0.3. 
for (i = 0; i < this.numChildren; i++) 
{ 
var displayitem:DisplayObject = this.getChildAt(i); 
if (displayitem instanceof StaticText) { 
    trace("a static text field is item " + i + " on the display list"); 
    myFieldLabel = StaticText(displayitem); 

    trace("and contains the text: " + myFieldLabel.text); 
    trace(myFieldLabel.mouseX); 
    myFieldLabel.alpha = 0.3; 
} 
} 

//Adds event listener to the stage for mouse move event 
stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseRollOver); 

//This is an event handler. I check if the mouse position is within the static field 
function mouseRollOver(evnt:MouseEvent):void 
{ 
if (0 <= myFieldLabel.mouseX && myFieldLabel.mouseX <= myFieldLabel.width && 0 <= myFieldLabel.mouseY && myFieldLabel.mouseY <= myFieldLabel.height) 
{ 
    mouseOverStaticText(evnt) 
} 
else 
{ 
    mouseNotOverStaticText(evnt) 
} 
} 

// this two methods change the static field alpha. Thay are only to show that is posible to detect and manipulate some properties of the StaticField. 
function mouseOverStaticText(evnt) 
{ 
myFieldLabel.alpha = 1; 
} 
function mouseNotOverStaticText(evnt) 
{ 
myFieldLabel.alpha = 0.3; 
} 

我不确定管理StaticText字段的目的是什么。如果您必须执行某些操作,则StaticText不是设计来管理的,这几乎可以确定该字段不能是静态的 - 它们可以是动态的(不具有可选属性),也可以使用MovieClip封装,或者可以有不同的解决方案案件。

+0

这个答案很混乱。因为StaticText不在InteractiveObject的继承链中,所以没有鼠标事件可以响应。使用'DisplayObject.mouseX'和/或'DisplayObject.mouseY'并没有什么意义,因为'mouseX'和'mouseY'提供的位置信息与相关DisplayObject的原点有关。你可以找到这种方式是:鼠标相对于我的DisplayObject(在这种情况下是静态文本)。 – 2013-03-07 00:00:52

+0

所以我更新我的答案。你说得对,静态文本不是继承InteractiveObject,不能关注事件,但是staticText继承DisplayObject,你可以根据这个字段获得Mouse的相对位置。你需要的是一个能够引发鼠标事件的对象(在这种情况下是舞台)。在事件的处理程序中,您必须检查鼠标X和Y的位置。在我的示例中,mouseRollOver处理程序中的“if”会执行此操作。如果光标在静态字段上或不在静态字段中,您将看到静态字段的字母变化。 – 2013-03-07 08:55:28