How can I detect StaticText in AS3?

守給你的承諾、 提交于 2019-12-23 12:28:02

问题


I have StaticText fields in my flash project and I need to run some code when the mouse is hovering over them. So I tried this code

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

but it doesn't work. When I use dynamic text fields and replace StaticText with TextField in the var tf, it works fine. I also thought that I could get this thing working with static text fields if I could make the mouse detect not StaticText as a target but some kind of object that has certain text properties (like "selectable" set to true), but I couldn't figure out how to do this. Anyway, I need to detect a static text field as a target somehow. Any help would be appreciated.
Thanks in advance


回答1:


Your best option would be to put the static text box in a movieclip, and then assign your code based around that. Static text boxes don't have instance names, and can't be manipulated.




回答2:


It is hard to do this. See this link enter link description here As you can see you can check if the DisplayObject is StaticText and by checking the mousX and MouseY properties you can find if the rollover is related to this field. By if you use Dynamic text and uncheck selectable field you will get a textfield that acts as StaticField

EDIT this is an explanation what I mean: Let we have a StaticText field into stage in Black flash document.

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

I'm not sure what is the purpose of the managing of the StaticText field. StaticText is not design to be managed if you have to do something this is almost sure that the field must not be a static - they can be dynamic ( without selectible property ) or can be capsulated with MovieClip or there can be a different solution in your case.



来源:https://stackoverflow.com/questions/15226432/how-can-i-detect-statictext-in-as3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!