【问题标题】:How can I detect StaticText in AS3?如何在 AS3 中检测 StaticText?
【发布时间】:2013-03-05 14:33:05
【问题描述】:

我的 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),那么我可以让这个东西与静态文本字段一起工作,但我不能不知道如何做到这一点。无论如何,我需要以某种方式将静态文本字段检测为目标。任何帮助将不胜感激。
提前致谢

【问题讨论】:

  • 最简单的方法:将所有文本文件封装在movieclip中(全部一个或一个)并使用它。您可以通过 DisplayObject 的 hitTest 函数检测碰撞
  • 您可以使用TextSnapshot 类来处理静态文本字段,但它们是只读的,您不能设置文本。你仍然可以使用hitTestTextNearPos

标签: actionscript-3


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    编辑 这是我的意思的解释: 让我们在 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 字段的目的是什么。如果您必须做某事,StaticText 不是为了管理而设计的,这几乎可以肯定该字段不能是静态的 - 它们可以是动态的(没有可选属性)或者可以用 MovieClip 封装,或者您的解决方案中可以有不同的解决方案案例。

    【讨论】:

    • 这个答案令人困惑。因为StaticText不在InteractiveObject的继承链中,所以没有鼠标事件可以响应。使用DisplayObject.mouseX 和/或DisplayObject.mouseY 并没有什么意义,因为mouseXmouseY 提供的位置信息是相对于相关DisplayObject 的原点而言的。您可以通过这种方式发现:鼠标相对于我的 DisplayObject 的位置(在这种情况下为静态文本)。
    • 所以我更新了我的答案。您是对的,静态文本不继承 InteractiveObject 并且不能重新设置事件,但 staticText 继承 DisplayObject 并且您可以根据字段获取鼠标的相对位置。你需要的是一个可以引发鼠标事件的对象(在这种情况下是舞台)。在事件处理程序中,您必须检查 mouseX 和 Y 位置。在我的示例中,mouseRollOver 处理程序中的“if”执行此操作。如果光标在静态字段上或不在静态字段上,您将看到静态字段的 alpha 发生变化。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 2013-02-27
    • 2016-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多