# Keyboard input causes mouse to lag - Flash



## Kajenx (Aug 14, 2008)

So I've been working on a space shooter type game in Flash using AS3, and I have it so the user holds down the arrow keys or the WASD keys to move, and uses the mouse to shoot (left button). I keep running into this strange lag where the mouse just jumps around the screen like it's lagging really badly, but the graphics still run fine at a smooth FPS.

I've narrowed down the problem. It only happens when you hold a key down and move the mouse. This can be any key, though, not just the keys used to move, so at first I thought it was the listener. I tried deleting all the listeners for the keyboard, though, and the mouse would still lag if I held a key down.

Interestingly, I tried it in both FF3 and IE7, and the mouse was dead on in FF, even though the graphics seemed to run a bit slower. In IE the graphics ran at 30 FPS (where I set it) but the mouse would still lag when holding a key down.

Does anyone have an idea why this would happen? I can't seem to isolate the problem to any bit of code. I think it might be the IE flash plugin.


----------



## jamiemac2005 (Jul 5, 2007)

Hey, it could be in the way you've written the event listener? we'd need to see some code though (i should be able to help but i haven't yet had the cash to move to flash with AS3)...

The main thing to do is look for memory-extensive code within the mouse handler and ensure it's only running onmousemove etc rather than all the time... The way to find out if it's the mouse's event listener is to make the event listener run more regularly and see if the app slows down.

Still, post your code (in "

```
" tags) and we'll take a look.(in particular both event handlers/listeners).

Cheers,
Jamey

p.s. what's your system spec? (it could make a drastic difference on someone elses pc but if you don't experience these problems with flash normally then i doubt it)...
p.p.s. sorry you mentionned 30FPS, is that the global Frames per second or do you use a FPS counter/handler(because if you're running it at higher FPS the frames not being used will still make a difference)...
```


----------



## Kajenx (Aug 14, 2008)

Hi jamie!

I'm using the mouseX and mouseY to work the mouse and it's doing it every frame.

The weirdest part about the bug is that everything works perfectly until you press a key down. I tried putting the ship on autopilot (thinking it might be the panning screen that caused the lag) but no, everything was fine. I tried deleting the keyboard listeners and same thing, the mouse would lag when a key was down, but not when no keys were pressed.

I had two mouse listeners for the left button, so I tried getting rid of those and the mouse lag was still there.

It's the most bizarre bug I've ever run into. I don't know what code to post, because I have no idea where it's coming from. :4-dontkno

The 30 FPS was my own frame counter. I've tried it on some really good computers and the lag was still there though.

- Luke


----------



## jamiemac2005 (Jul 5, 2007)

hmm, how's the code organised (is the keyboard handling above the mouse handling?) If you were to post the code showing the general flow of your program(e.g. some form of handling loop), the code which handles your frame counting(and the actual frame rate), and possibly the keyboard listner's code(because i think the problem may be there). Then i recon we'd get a good idea of how to fix it.

Cheers,
Jamey


----------



## Kajenx (Aug 14, 2008)

I've been doing some tests and I discovered it definitely was not a problem with my code.


```
import flash.events.*;

addEventListener(Event.ENTER_FRAME, Main);

function Main(event:Event) {
	cursor.x = mouseX;
	cursor.y = mouseY;
	for (var a = 0; a < 200000; a++) {
		Math.random();
	}
}
```
Try running that code at 30 FPS in IE7 (you'll need a movieclip named "cursor" on the stage). Swirl the mouse around in circles. The mouse may look a little laggy without a key pressed down, but when you hold a key down you'll see a very obvious difference.

Now try the same thing in FF. There will be no lag with a key held down.

Now, and this is the oddest thing, try the test in IE7, but this time have another flash open in anoter tab. There won't be any lag when you hold a key down, but when you close the other tab, the lag will come back.


----------



## jamiemac2005 (Jul 5, 2007)

Hmm, you could try change the event it's listening to to: "Event.MOUSE_MOVE", so that it's not running when it doesn't need to be... But i dont see an actual problem there...

Hmm, its starting to sound like the problem is computer-specific (the IE7 Lag is a weird thing to hear of)... If you could upload a swf version of the file we could tell you if we experience the lag aswell...

Cheers,
Jamey


----------



## Kajenx (Aug 14, 2008)

Here's a link to the test.

I think it's a problem with the plugin for IE. Try running the three tests I listed in my last post:

1. IE with no other tabs open (Lag)
2. FF (No lag)
3. IE with a flash open in another tab (No lag)

NOTE: I'm using IE7. Someone tested it on IE6 and they said it didn't lag there. (They also tested IE7 and got the lag.)

Basically, now all I'm looking for is some hack that'll force the input events to work properly.


----------



## jamiemac2005 (Jul 5, 2007)

Hey, i did the tests you stated, and you're absolutely right, though another thing to note is that in IE7 the framerate(average) is arount 25fps as apposed to when i use FF where it's 18fps on average?

Both are using the latest flash plugin, so i don't understand the problem. Maybe you could try asking about it on one of the Adobe forums: http://www.adobe.com/cfusion/webforums/forum/categories.cfm?forumid=15&catid=194&entercat=y

I even tried using the current beta release of flash player 10 and the same occours on IE. it's definately an IE problem but i can't find anything on it.

Cheers,
Jamey


----------



## Kajenx (Aug 14, 2008)

Thanks, I made a post on that forum. Hopefully they'll have some ideas... :sigh:


----------



## jamiemac2005 (Jul 5, 2007)

Yeah, hope you get it sorted, because i havent a clue.

Cheers,
Jamey


----------

