开发者

Key down lags Mouse in Flash with FF

I'm trying to improve the performance of a flash game we did. 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...

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....

I found that a lot of people are having the same problem in their own games but they couldnt solve it!

Some help plz!

Edit:

The full co开发者_JAVA百科de 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. 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.

But I still got the problem that I can't change that in all the Firefoxs from the game users. 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. 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. 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.

Let me know if this helps at all.


I'm running Firefox 3.6.14 and I couldn't replicate your error. 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. You should have an object that you can move with WASD and an aim, that you move with the mouse. 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. 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. 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.

ps: bienvenido a stackoverflow ;)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜