简体   繁体   中英

AS3 Project - Mouse Move Listener Reacting Outside of Application

I'm getting an unusual behavior that I can't seem to get to the bottom of. When I run this, if I move in the swf area it traces normally on mouse move. To be expected.

But it's tracing for the move event when I click anywhere on screen. If I click and drag, it traces as if I were moving in the swf area of the browser.

Here's the code. I've simplified to it's barebones. Just put this in in an empty AS3 project in Flex called "Engine" - sans quotes obviously.

package {
import flash.display.Sprite;
import flash.events.MouseEvent;

[SWF(width='640', height='360', backgroundColor='#888888', frameRate='31')]
public class Engine extends Sprite
{       
    public function Engine()
    {
        // Add the mouse handlers
        stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
    }

    public function mouseMoveHandler(evt:MouseEvent):void
    {
        trace("move");
    }
}
}

As a workaround I've add the MOUSE_MOVE one MOUSE_OVER and remove it on MOUSE_OUT. But the behavior still seems quite unusual and I'd be interest in understanding why it's happening.

Can anyone tell me how I can keep the events constrained to the actual stage of the application?

As already mentioned, you can't stop these events from firing. They are triggered until you release the mouse.

What you can do is compare the coordinates of the MouseEvent with the bounds of the stage and ignore those outside.

public function mouseMoveHandler(evt:MouseEvent):void
{
    if (evt.stageX >= 0 && evt.stageX <= stage.stageWidth &&
        evt.stageY >= 0 && evt.stageY <= stage.stageHeight)
    {
        trace("move");
    }
}

Ok, This is a known bug that only happens with Mac.

There is a fix here :

http://www.visible-form.com/blog/transformmanager-fix-for-mac-firefox/

If you click inside your flash movie and drag the mouse outside of it, MOUSE_MOVE event will continue to trigger until you release your mouse. MOUSE_LEAVE will trigger only when you release the mouse outside the player. This is how Flash Player works.

Maybe I'm wrong but I don't think you can change this behaviour.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM