How can I detect StaticText in AS3?

I have StaticText fields in my Flash project, and I need to run some code when the mouse hangs 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 that will not work. When I use dynamic text fields and replace StaticText with TextField in var tf, it works fine. I also thought that I could make this work with static text fields if I could make the mouse detect not StaticText as a target, but some object that has certain text properties (for example, "selectable" is set to true), but I couldn't figure out how to do this. Anyway, I need to somehow detect a static text field. Any help would be appreciated. Thanks in advance

+4
source share
2 answers

Your best option is to place a static text box in the movie clip and then assign your code based on this. Static text fields do not have instance names and cannot be processed.

+2
source

This is hard to do. See Link, enter the link here. As you can see, you can check if DisplayObject is StaticText, and by checking the properties of mousX and MouseY, you can find out if this is related to the rollover of this field. If you use dynamic text and clear the selected field, you will get a text field that acts like a StaticField

EDIT this explanation of what I mean: Let us have a static Text field in Black Flash text.

 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 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 the purpose of managing a StaticText field is. StaticText is not design driven, if you need to do something, it is almost certain that the field should not be static - they can be dynamic (without a selective property) or can be copied using MovieClip or there may be another solution in your case.

0
source

All Articles