views:

87

answers:

2

When the user presses the mouse, and releases it over a static textfield with selectable text, no MOUSE_UP event is fired - not on the stage and also nowhere else.

I experienced this when using a scrollbar class on a movieclip with a nested static textfield. When the user drags the scroll handle and releases the mouse over the textfield, the dragging/scrolling is stuck.

To test this, create a new AS3 fla file, place a static textfield somewhere, and put in some text. Make sure the selectable property is checked in the properties panel. Add this script to the timeline:

import flash.events.* 
function down(event:Event):void { trace('down'); }
function up(event:Event):void { trace('up'); }
stage.addEventListener(MouseEvent.MOUSE_DOWN, down)
stage.addEventListener(MouseEvent.MOUSE_UP, up)

Now test the movie and click the mouse. You will notice that trace('up') will not occur when you release the mouse over the textfield.

+1  A: 

The short answer is no, although you could attempt to loop through the stage's display list and add mouse events to it.

Assign mouse events to a dynamic text field with an instance name you can reference to.

import flash.events.MouseEvent;

// tf:TextField instance created with authoring tool
tf.text = "Hello World!";

tf.addEventListener(MouseEvent.MOUSE_UP, handleMouseUp);

stage.addEventListener(MouseEvent.MOUSE_UP, handleMouseUp);
stage.addEventListener(MouseEvent.MOUSE_DOWN, handleMouseDown);

function handleMouseUp(event:Event):void { trace('up'); }
function handleMouseDown(event:Event):void { trace('down'); }

It is a correct behaviour to not have any events dispatched from the stage when releasing the mouse over a text field with no event listeners added to it. After all, your text field is sitting on top of the stage.

Hope this helps.

rey
A: 

There is a way but you'll have to fudge things up a bit.

1.Add a Sprite over the textfield, now the MOUSE_UP event is received but the text is not selectable anymore.

2.Use the TextSnapshot class pointing on "hitTestTextNearPos" and "setSelected" to select the text inside the static textfield when MOUSE_DOWN / MOUSE_MOVE events are received on the overlaying sprite.

You now have a selectable static text field that fires MOUSE_UP on top of it. A custom pointer ( selection ) may be needed to emulate the behavior perfectly.

Oliver
This sounds like a great solution! I will try it out and report back.
Jovica Aleksic
Cool, keep me posted!
Oliver
There is another caveat mentioned in the Adobe docs for hitTestTextNearPos: For this method to work correctly with rich static textfields (with possibly multiple fonts), you must create a dynamic textfield for each of these fonts and embed at least one character within the authoring tool. Fair enough, considering the situation.https://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/flash/text/TextSnapshot.html#hitTestTextNearPos%28%29
Jovica Aleksic