我的flash项目中有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中将StaticText替换为TextField时,它可以正常工作。我还认为,如果我可以让鼠标检测不是StaticText作为目标,而是某种具有某些文本属性的对象(如“selectable”设置为true),我可以使用静态文本字段,但我无法弄清楚如何做到这一点。无论如何,我需要以某种方式检测静态文本字段作为目标。任何帮助将不胜感激 提前致谢
答案 0 :(得分:2)
您最好的选择是将静态文本框放在动画片段中,然后根据该代码分配您的代码。静态文本框没有实例名称,也无法操作。
答案 1 :(得分:0)
很难做到这一点。请参阅此链接enter link description here 如您所见,您可以检查DisplayObject是否为StaticText,并通过检查mousX和MouseY属性,您可以找到翻转是否与此字段相关。如果您使用动态文本并取消选中可选字段,您将获得一个充当StaticField
的文本字段修改强> 这是我的意思解释: 让我们在Black flash文档中将一个StaticText字段放入舞台。
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字段的目的是什么。静态文本不是设计要管理的,如果你必须做的事情几乎可以肯定该字段不能是静态的 - 它们可以是动态的(没有可选属性)或者可以用MovieClip封装,或者你的可以有不同的解决方案情况下。