简体   繁体   English

按键向下滞后Flash中的鼠标使用FF

[英]Key down lags Mouse in Flash with FF

I'm trying to improve the performance of a flash game we did. 我正在努力提高我们所做的Flash游戏的性能。 It's a game very similar to GTA but for flash... This is why its important that Mouse and keyboard can be used at the same time... 这是一个非常类似于GTA的游戏,但对于闪存...这就是为什么重要的是鼠标和键盘可以同时使用...

But in Firefox (with the last flash version, and the last Firefox version) in some cases if you press a key (WASD) and you move the mouse, the mouse events are dispached after the keyboard events, when you release the keyboard.... 但在某些情况下,如果您按一个键(WASD)并移动鼠标,则在Firefox(使用最后一个Flash版本和最后一个Firefox版本)中,在键盘事件之后,当您释放键盘时,将分离鼠标事件。 ..

I found that a lot of people are having the same problem in their own games but they couldnt solve it! 我发现很多人在他们自己的游戏中遇到同样的问题,但他们无法解决它!

Some help plz! 一些帮助PLZ!

Edit: 编辑:

The full code is very big and i colund't find what part is the problem. 完整的代码是非常大的,我不知道哪个部分是问题。 But I did this class to do a test (this class is runing with the game : 但是我做了这个课程来做一个测试(这个课程在游戏中运行:

    public function MouseAndKeyboardTest()
    {
        var stage : Stage = FW.Stage_;

        stage.addEventListener(MouseEvent.MOUSE_MOVE, MouseMove);
        stage.addEventListener(KeyboardEvent.KEY_DOWN, KeyDown);
        stage.addEventListener(KeyboardEvent.KEY_UP, KeyUp);
    }

    private function KeyUp(e : KeyboardEvent)
    {
        trace("Key Up : " + e.keyCode);
    }

    private function KeyDown(e : KeyboardEvent)
    {
        trace("Key Down : " + e.keyCode);
    }

    private function MouseMove(e : MouseEvent)
    {
        trace("Mouse Move : [" + e.stageX + ", " + e.stageY + "]"   );
    }

and this was the trace result when you had the key down and the mouse moving at the same time, some mouse move in between and a lot after all key are up: 当你按下键并且鼠标同时移动时,这是跟踪结果,一些鼠标在两者之间移动并且在所有键都关闭之后移动很多:

        Key Down : 87
        Key Down : 87
        Key Down : 87
        Key Down : 87
        Key Down : 87
        Key Down : 87
        Key Down : 87
        Key Down : 87
        Key Down : 87
        Key Down : 87
        Key Down : 87
        Key Down : 87
        Key Down : 87
        Key Down : 65
        Key Down : 65
        Key Down : 68
        Key Up : 65
        Key Down : 68
        Key Down : 68
        Key Down : 68
        Key Down : 68
        Key Down : 68
        Key Down : 68
        Key Down : 68
        Key Down : 68
        Key Down : 68
        Key Down : 68
        Key Down : 68
        Key Down : 68
        Key Down : 68
        Key Down : 65
        Key Up : 68
        Key Down : 65
        Key Down : 65
        Key Down : 65
        Key Down : 65
        Key Down : 65
        Key Down : 65
        Key Down : 65
        Key Down : 65
        Key Down : 65
        Key Down : 65
        Key Down : 65
        Key Down : 65
        Key Down : 65
        Key Down : 68
        Key Up : 65
        Mouse Move : [353.65, 137.55]
        Mouse Move : [354.8, 138.4]
        Mouse Move : [354.8, 137.4]
        Mouse Move : [362.8, 135.4]
        Mouse Move : [372.9, 135.25]
        Key Down : 68
        Mouse Move : [449.9, 139.25]
        Mouse Move : [462.85, 139.35]
        Key Down : 68
        Key Down : 68
        Mouse Move : [479.85, 139.35]
        Mouse Move : [477.85, 139.35]
        Mouse Move : [469.85, 141.35]
        Mouse Move : [458.95, 145.2]
        Key Down : 68
        Mouse Move : [445.95, 148.2]
        Key Down : 68
        Key Down : 68
        Key Down : 68
        Key Down : 68
        Key Down : 68
        Key Down : 68
        Key Down : 68
        Key Down : 68
        Key Down : 68
        Key Down : 68
        Key Down : 68
        Key Down : 68
        Key Down : 68
        Key Down : 68
        Key Down : 68
        Key Down : 68
        Key Down : 68
        Key Down : 68
        Key Down : 68
        Key Down : 68
        Mouse Move : [330.65, 171.55]
        Key Down : 68
        Key Up : 87
        Key Up : 68
        Mouse Move : [210, 196.2]
        Mouse Move : [207, 198.2]
        Mouse Move : [246.2, 190]
        Mouse Move : [354.2, 180]
        Mouse Move : [431.15, 173]
        Mouse Move : [323.15, 197]
        Mouse Move : [181.15, 220]
        Mouse Move : [80.15, 231]
        Mouse Move : [267.15, 203]
        Mouse Move : [186.15, 225]
        Mouse Move : [20.15, 217]
        Mouse Move : [161.15, 217]
        Mouse Move : [132.15, 223]

There is a problem with the Plugin-container in Firefox 3.6.4 and above. Firefox 3.6.4及更高版本中的插件容器存在问题。 By changing: 通过更改:

'dom.ipc.plugins.enabled.npswf32.dll' to false

in the about:config from FF it solved the problem witht the mouse and the keyboard in Firefox. 在FF中的about:config中,它解决了Firefox中鼠标和键盘的问题。

But I still got the problem that I can't change that in all the Firefoxs from the game users. 但是我仍然遇到了这个问题,我无法在游戏用户的所有Firefox中改变它。 The important question is that the user have no lag. 重要的问题是用户没有滞后。

Here's something to take a look at: 这是一个值得一看的东西:

var keys:Object = {};
var keyTimer:Timer = new Timer(1000);

keyTimer.addEventListener(TimerEvent.TIMER, keyTimed, false, 0, true);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownManager, false, 0, true);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpManager, false, 0, true);
stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoved, false, 0, true);

function keyTimed(evt:TimerEvent):void
{
    keyTimer.stop();
    keyTimer.reset();
    if(stage.hasEventListener(KeyboardEvent.KEY_DOWN))
    {
        stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyDownManager);
    }
}

function keyDownManager(evt:KeyboardEvent):void
{
    keyTimer.start();
    switch(evt.keyCode)
    {
        case 37:
        keys['left'] = true;
        break;
        case 38:
        keys['up'] = true;
        break;
        case 39:
        keys['right'] = true;
        break;
        case 40:
        keys['down'] = true;
        break;
    }
    trace("key is down");
}

function keyUpManager(evt:KeyboardEvent):void
{
    keyTimer.stop();
    keyTimer.reset();
    switch(evt.keyCode)
    {
        case 37:
        keys['left'] = false;
        break;
        case 38:
        keys['up'] = false;
        break;
        case 39:
        keys['right'] = false;
        break;
        case 40:
        keys['down'] = false;
        break;
    }
    trace("key is up");
    if(!stage.hasEventListener(KeyboardEvent.KEY_DOWN))
    {
        stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownManager, false, 0, true);
    }
}

function mouseMoved(evt:MouseEvent):void
{
    trace("mouse moved");
}

I've specified a bounds of 1000 milliseconds for a timer that will run whenever a key is down. 我为一个定时器指定了一个1000毫秒的界限,只要一个按键关闭就会运行。 This will enable you to work with multiple keys, but will remove the listener once any combination of keys have been held down for 1000 milliseconds. 这将使您能够使用多个键,但是一旦按下任何键组合1000毫秒,它将删除侦听器。 Once any key is released, the listener is re-added and the timer reset. 释放任何键后,将重新添加侦听器并重置计时器。 What this enables you to do is control the removal of the listener if someone is holding a combination of keys for a set (long in this case) amount of time. 这使你能够做的是控制如果有人持有一组密钥组合(在这种情况下为长)的时间量,则删除监听器。

Furthermore, I've implemented an object to store the key booleans so those properties can be utilized inside an ENTER_FRAME or something similar for movement. 此外,我已经实现了一个对象来存储关键的布尔值,因此这些属性可以在一个ENTER_FRAME或类似的移动中使用。

Let me know if this helps at all. 如果这有帮助,请告诉我。

I'm running Firefox 3.6.14 and I couldn't replicate your error. 我正在运行Firefox 3.6.14,我无法复制您的错误。 But I created a small class, so you can test it for yourself. 但我创建了一个小班,所以你可以自己测试一下。 Does it work as you want? 它能按你的意愿工作吗? To use it, create a new project, using this class as Main. 要使用它,请使用此类作为Main创建一个新项目。 You should have an object that you can move with WASD and an aim, that you move with the mouse. 您应该有一个可以随WASD移动的对象和一个用鼠标移动的目标。 You can move on any direction, and the mouse at the same time. 您可以在任何方向上移动,同时移动鼠标。 Please post your results. 请发布您的结果。

package {
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.KeyboardEvent;
    import flash.events.MouseEvent;
    import flash.ui.Keyboard;
    public class Main extends Sprite {
        private var keys:Array = new Array();
        private var hero:Sprite = new Sprite();
        private var aim:Sprite = new Sprite();
        public function Main():void {
            stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
            stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
            stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
            stage.addEventListener(Event.ENTER_FRAME, update);

            // create the objects for testing
            hero.graphics.beginFill(0xff0000);
            hero.graphics.drawRect(0, 0, 50, 50);
            hero.graphics.endFill();
            hero.x = stage.stageWidth / 2;
            hero.y = stage.stageHeight / 2;
            stage.addChild(hero);

            aim.graphics.lineStyle(1, 0);
            aim.graphics.drawCircle(0, 0, 10);
            aim.graphics.moveTo(0, -10);
            aim.graphics.lineTo(0, 10);
            aim.graphics.moveTo(-10, 0);
            aim.graphics.lineTo(10, 0);
            aim.x = hero.x + hero.width / 2;
            aim.y = hero.y + hero.height / 2;
            stage.addChild(aim);
        }

        private function update(e:Event):void {
            if (keys[65]) hero.x -= 5;
            if (keys[68]) hero.x += 5;
            if (keys[87]) hero.y -= 5;
            if (keys[83]) hero.y += 5;
        }

        private function onMouseMove(e:MouseEvent):void {
            aim.x = e.stageX;
            aim.y = e.stageY;
        }
        private function onKeyDown(e:KeyboardEvent):void {
            keys[e.keyCode] = true;
        }
        private function onKeyUp(e:KeyboardEvent):void {
            keys[e.keyCode] = false;
        }
    }

}

When you get the key down event for say 'W' you could save that fact in a variable, say boolean wKeyIsDown. 当你得到'W'的按键事件时,你可以将这个事实保存在一个变量中,比如boolean wKeyIsDown。 Then cancel the event to stop recieving the event. 然后取消活动以停止接收活动。 Then when w is up change your wKeyIsDown variable to false and cancel the event again. 然后当w up时,将wKeyIsDown变量更改为false并再次取消该事件。 You don't constantly need to know of the occurrence. 您不需要经常知道发生的事情。

It might be helpful to see how you are using the events. 查看您如何使用这些事件可能会有所帮助。 Your movement logic may be flawed. 你的运动逻辑可能有缺陷。

Try using weak references. 尝试使用弱引用。

addEventListener(KeyboardEvent.MOUSE_MOVE, MouseMove, false, 0, true):
addEventListener(KeyboardEvent.KEY_UP, KeyUp, false, 0, true):
addEventListener(KeyboardEvent.KEY_DOWN, KeyDown, false, 0, true):

Notice the last parameter, regarding weak references, must be set to true. 请注意,关于弱引用的最后一个参数必须设置为true。

ps: bienvenido a stackoverflow ;) ps:bienvenido stackoverflow;)

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

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