views:

242

answers:

4

MOUSE_OUT events are, evidently, handled differently for X and Y mouse moves when leaving a Sprite.

How do I fix this or work around it? Where is this documented?

MOUSE_OUT occurs when x==0, but not y==0 (you need to go to y==-1):

private var _sp:Sprite;

public function test( ):void
{
    stage.align = StageAlign.TOP_LEFT;
    stage.scaleMode = StageScaleMode.NO_SCALE;

    _sp = new Sprite( );
    _sp.graphics.beginFill( 0xFF0000, 1 );
    _sp.graphics.drawRect( 0, 0, 15, 15 );
    _sp.graphics.endFill( );
    _sp.x = 10;
    _sp.y = 10;
    _sp.alpha = 1;

    addChild( _sp );

    _sp.addEventListener( MouseEvent.MOUSE_MOVE, msMvCb, false, 0, true );
    _sp.addEventListener( MouseEvent.MOUSE_OUT, msOutCb, false, 0, true );
}

private function msMvCb( evt:MouseEvent ):void
{
    traceMousePos( "mv", evt );
    _sp.alpha = 1;
}

private function msOutCb( evt:MouseEvent ):void
{
    traceMousePos( "out", evt );
    _sp.alpha = .5;
}

private function traceMousePos( note:String, evt:MouseEvent ):void
{
    trace( note + " -- " + evt.localX + ", " + evt.localY + ", " + evt.stageX + ", " + evt.stageY );
}

Here is a trace from moving straight up, with MOUSE_OUT on -1 ...

mv -- 7, 3, 17, 13
mv -- 7, 2, 17, 12
mv -- 7, 1, 17, 11
mv -- 7, 0, 17, 10
out -- 7, -1, 17, 9

And here is a trace from moving straight to the left, with MOUSE_OUT on 0 ...

mv -- 3, 7, 13, 17
mv -- 2, 7, 12, 17
mv -- 1, 7, 11, 17
out -- 0, 7, 10, 17

edit

The same errant behavior occurs with MOUSE_ENTER.

A: 

I am no expert, but looks like some bug to me, i tryed with ROLL_OVER too, does the same thing, also as a noticable thing, if you put the box at 0,0 there is a small 1px border on the top of the stage, which also shows as -1 when over it.

Andy
Yes, this errant behavior occurs with ROLL_OVER also, but I cannot reproduce the visual effect you describe.
jedierikb
+1  A: 

One way to "fix" this behavior is to make a subclass of Sprite which listens to all of its MOUSE_MOVE, MOUSE_OUT, and MOUSE_ENTER events. On examining each event, it should be relatively straightforward to decide whether to let it propagate as normal, stop it entirely, or dispatch a new type of event that matches your desired behavior.

For example, one could listen to MOUSE_MOVE, and if y==0, dispatch a new MOUSE_OUT event (and block any subsequent redundant events if y==-1).

ZackBeNimble
This is a good idea, however... when mouseY == 0, the mouse is still visually in the sprite. Capturing and calling my own mouseOut then might technically get around this, but the visual behavior would be off.
jedierikb
+1  A: 

Well spotted! My guess would be that it's something to do with the rounding of twips – the base unit of measurement in Flash (1/20 of a pixel). If the sprite is positioned on a half pixel (_sp.y = 10.5) you should find, when moving the mouse straight up, that localY is now 0 on MOUSE_OUT.

It's also worth noting that frame-rate can affect the firing of mouse events, and the speed at which the mouse is moving can affect the values of localX and localY. If the mouse moves very quickly over the sprite, and/or if the frame-rate is low, the values can be wildly inaccurate.

Richard M
Good thinking re: twips. However, mouseEvents' position values are ints. And, in the posted example, sprites are also positioned at int values.
jedierikb
As for the speed of the mouse movement, that does not seem to be an issue. I can move the mouse /really/ slow, and get this errant behavior.
jedierikb
Yes, the mouse co-ordinates will be integers. My suggestion is that even when your sprite co-ordinates are integers this may not be true within Flash's internal rendering and 'mousing' systems and your problem is a consequence of this.Regarding the speed of mouse movement: in my experience (using the OS X Flash player at least) mouse co-ordinates gathered in mouse event handlers become increasingly inaccurate as mouse velocity increases.
Richard M
A: 

the only fix i had come across to this in the past was is what Richard said.

AdityaGameProgrammer