PDA

View Full Version : Keylistener issues in Flash 8 ?



Ophyr
April 10th, 2006, 09:05 AM
Hi Guys,

I was wondering if anyone had a similiar problem I have ?

I don't have the code at hand, but it's not too hard to explain.
The setup is as follows :

I have a listener for keys, and a switch statement that catches the key events. If I press 'left' for example, a variable moveLeft becomes true. If I release the left button, moveLeft will become false.

This seems to work, until I do the following: Press and hold left, press and hold any other button aswell. If I release the buttons, it will not register the keyUp event for some reason. I know the code worked in Flash MX 2004 because I literally copy-pasted it :cross-eye

For now I work with Key.isDown(Key.LEFT), but that looks just plain ugly I.m.h.o...

Joppe
April 10th, 2006, 10:43 AM
Soo.. Mind posting the code?

Ophyr
April 10th, 2006, 10:46 AM
I was about to, but I have it on an external HDD that I don't have here :D It's basically 1 on 1 with the description above though, I thought that was enough info, but I'll post code tonight :)

Joppe
April 10th, 2006, 10:54 AM
Ok, its probably enought with the info but I like to see coding and see if I can help :P

Ophyr
April 10th, 2006, 02:13 PM
Well, here's the code


keyListener = new Object();

Key.addListener(keyListener);

keyListener.onKeyDown = function() {
KeyDown(Key.getCode());
};
keyListener.onKeyUp = function() {
KeyUp(Key.getCode());
};

KeyDown = function (keyCode) {
switch (keyCode) {
case 37 :
_root.moveLeft = true;
break;
case 39 :
_root.moveRight = true;
break;
case 38 :
_root.jump = true;
break;
case 80 :
togglePause();
break;
}
};
KeyUp = function (keyCode) {
switch (keyCode) {
case 37 :
_root.moveLeft = false;
break;
case 39 :
_root.moveRight = false;
break;
case 38 :
_root.jump = false;
break;
}
};


You can use it like this


if(_root.moveLeft) doStuff();

Psykloak1
April 10th, 2006, 04:46 PM
Thats a big reason that I stopped using keyListeners and stuff.

The way I do it now (and this mightturn out to be worse but it works great for me) is put an "if (Key.isDown(87))" In an "onClipEvent(enterFrame) " in a movie clip. Like I said, maybe this is not the best way or a coiincedance, but it solved a bunch of problems I had using keyListener

so:


onClipEvent(enterFrame) {
if (Key.isDown(87)) //w
_y=-5;
if (Key.isDown(65)) //a
_x=-5;
if (Key.isDown(83)) //s
_y=5;
if (Key.isDown(68)) //d
_x=5;
}

when put in a movie clip would make the charecter move around using WASD