4

我的 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 中的 TextField 替换 StaticText 时,它工作正常。我还认为,如果我可以让鼠标检测到的不是 StaticText 作为目标,而是某种具有某些文本属性的对象(例如“可选择”设置为 true),那么我可以让这个东西与静态文本字段一起工作,但我不能不知道如何做到这一点。无论如何,我需要以某种方式将静态文本字段检测为目标。任何帮助,将不胜感激。
提前致谢

4

2 回答 2

2

您最好的选择是将静态文本框放在影片剪辑中,然后根据它分配您的代码。静态文本框没有实例名称,也无法操作。

于 2013-03-05T14:37:45.257 回答
0

很难做到这一点。查看此链接在此处输入链接描述 如您所见,您可以检查 DisplayObject 是否为 StaticText,并通过检查 mousX 和 MouseY 属性,您可以找到翻转是否与此字段相关。如果您使用动态文本并取消选中可选字段,您将获得一个充当静态字段的文本字段

编辑 这是我的意思的解释:让我们有一个静态文本字段进入黑色 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 封装,或者您的解决方案中可以有不同的解决方案案子。

于 2013-03-05T15:05:19.987 回答