View Full Version : Key.addListener causes a delay before running code
you_rock
02-15-2004, 09:11 PM
There is a MC object called myPlayer that has various states inside of it as frame labels.
I made the switch from onEnterFrame to a Listener object, however there is a delay between the time the key is held down and when it actually moves the myPlayer object.
It is almost like a Scooby Doo effect where the myPlayer object runs in place and then starts moving a full second later.
Thoughts???
// KEYPRESSES
keyMove = new Object();
keyMove.onKeyDown = function() {
if (Key.isDown(Key.RIGHT)) {
myPlayer._x += 10;
myPlayer._xscale=-100;
myPlayer.gotoAndPlay("walking");
Move();
}
else if (Key.isDown(Key.LEFT)) {
myPlayer._x -= 10;
myPlayer._xscale=100;
myPlayer.gotoAndPlay("walking");
Move();
}
if (Key.isDown(Key.DOWN)) {
myPlayer._y += 10;
myPlayer.gotoAndPlay("walking");
Move();
}
if (Key.isDown(Key.UP)) {
myPlayer._y -= 10;
myPlayer.gotoAndPlay("walking");
Move();
}
}
keyMove.onKeyUp = function () {
myPlayer.gotoAndPlay("standing");
}
Key.addListener(keyMove);
CyanBlue
02-15-2004, 10:18 PM
Howdy... ;)
I don't know what you meant by the Scooby Doo effect, but the code itself does not look like there should be any delay...
If you can upload some sample that shows the delay, I can take a look... :)
agent81
02-15-2004, 10:24 PM
hold down a key in wordpad, or whatever, it'll type once, and then after a pause start repeating itself. (there's a name for it, just can't remember).
This is whats happening, you need to make you're on(keyDown), trigger an onEnterFrame
its a bit of a ****, personally I'd go swapping it back to onEnterFrame, listeners are great for events that happen less frequently, where you can afford slightly lazier evaluation.
sorry:(
you_rock
02-16-2004, 05:37 PM
That is sad news.
I was hoping to find a way to only send messages to the server when a key is pressed, and the keyListener seemed to be a simple solution to accomodate this. The onEnterFrame works, but increases the number of hits dramatically.
The problem is, at 24 FPS, the server is getting creamed with requests from onEnterFrame keyPresses.
Originally, I had an if statement and a boolean variable that at least cut down some of the server traffic to only send messages when the variable was true.
Is there any other solution to accomplish what the Key.addListener does for me? Or is there a way to speed up the process of the repeating for the key listener?
agent81
02-16-2004, 05:53 PM
why are you sending messages to the server, when a key is pressed?
I don't understand, sorry. I thought you were controlling a character, in which case the performance loss in using onEnterFrame, may infact be less than the loss in accuracy with listener events.
Are you using Flash Comm?
you_rock
02-16-2004, 07:09 PM
Hi, thanks for your help.
I am using Remoting.
I have seperate functions for the movement of the myPlayer object.
Each time there is a keypress the client moves the myPlayer._x and sends a result message to the other client who receives the result from the server.
With, onEnterFrame, the program would send messages if you were moving and if someone else was moving. If neither is moving it refreshes and continues to send messages until movement occurs again.
With the listener, it was convenient that messages would only be sent when someone actually pressed something, thereby cutting down the amount of server hits for refreshing and waiting for someone to move.
I am trying to create a solution that minimizes lag and server hits.
agent81
02-16-2004, 07:37 PM
If you use Shared Objects, then this shouldn't be happening. They only update, when the content actually changes.
Otherwise, you can easily write that functionality into your client side logic, just like you said earlier. check the values, and only update them when they are different.
if you find that you don't want to update the server every frame, write a function to check the current variables (and if they are different), and execute it on a setInterval call
I'm just making some updates at the moment to a multiplayer racing game that I wrote a while back, using Flash Comm. Its using a predefined track, and the data flow is still pretty massive. You just need to get your coding logic as tight as possible :D
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.