简体   繁体   English

AS3项目-鼠标移动侦听器在应用程序外部进行响应

[英]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. 运行此命令时,如果我在swf区域中移动,则通常会在鼠标移动时进行跟踪。 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. 如果单击并拖动,它的踪迹就像我在浏览器的swf区域中移动一样。

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. 只需将其放入Flex中名为“ Engine”的一个空AS3项目中即可(显然没有引号)。

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. 作为一种解决方法,我添加了一个MOUSE_MOVE一个MOUSE_OVER并在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. 您可以做的是将MouseEvent的坐标与舞台的边界进行比较,而忽略外部的坐标。

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. 好的,这是一个已知的错误,仅适用于Mac。

There is a fix here : 这里有一个解决方法:

http://www.visible-form.com/blog/transformmanager-fix-for-mac-firefox/ 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. 如果单击Flash电影内部并将鼠标拖到Flash电影外部,则MOUSE_MOVE事件将继续触发,直到释放鼠标为止。 MOUSE_LEAVE will trigger only when you release the mouse outside the player. 仅当您在播放器外部释放鼠标时,MOUSE_LEAVE才会触发。 This is how Flash Player works. 这就是Flash Player的工作方式。

Maybe I'm wrong but I don't think you can change this behaviour. 也许我错了,但我不认为您可以更改此行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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