Skip to main content
Known Participant
April 30, 2009
Question

Key.isDown / getAscii() problems

  • April 30, 2009
  • 1 reply
  • 1180 views

I'm having some trouble detecting keypresses.

Woking with the arrow keys works fine, but using getAscii() is having problems.

I tried using:

if (getAscii(100)) {//do stuff;};

but the issue is that once you press a key, getAscii() keeps returning the value of the last key pessed even after you stop pessing it.

I tied using a keyDown listener, instead of onEnteFame and couldn't get it to work at all.

I tried searching these foums, but some of the solutions had all the code deleted... odd.

How do I add the ability to add key detection for the other keyboard keys to this code?

xspeed = 0;
yspeed = 0;

this.onEnterFrame = function() {
     if (Key.isDown(Key.LEFT)) {
         xspeed += -3;

    };
     if (Key.isDown(Key.RIGHT)) {
         xspeed += 3;

    };
     if (Key.isDown(Key.UP)) {
         yspeed += -3;

    };
     if (Key.isDown(Key.DOWN)) {
         yspeed += 3;

    };
     xspeed *= .8;
     yspeed *= .8;
     if (Math.abs(xspeed) < 1) {xspeed = 0;};
     if (Math.abs(yspeed) < 1) {yspeed = 0;};
     if (xspeed <> 0 or yspeed <> 0) {
         _root.Map._x += xspeed;
         _root.Map._y += yspeed;
     };

}

(The code has a few exta lines to make the accelleration nice and smooth.)

This topic has been closed for replies.

1 reply

April 30, 2009

try this:

//////////********KEY LISTENER*************************
delete keyListener;
Key.removeListener(keyListener);

keyListener = new Object();
keyListener.onKeyDown = function() {

code.Key.getAscii();

trace(Key.getAscii());

if (Key.getAscii() == 97) {
  trace("A hit");
}
///*************spacebar *********************
if (Key.isDown(32)) {
  trace("space bar pressed");
}
////*********CONTROL J***********************         
if (Key.isDown(Key.CONTROL) && Key.getCode() == 74) {

  trace("CONTROL J");
}
////********* p ***********************         
if (Key.isDown(112)) {

  trace("p");
}
////********* q ***********************         
if (Key.isDown(113)) {

  trace("g");
}
////*********F1***********************  
if (Key.getCode() == 112) {
  trace("F1");

}

};
Key.addListener(keyListener);

Important:

Always remember to remove your listener before calling on it again.Most of the time I place it on a frame the user can NOT return to, because it will keep adding listeners and you can easly build up hundreds of them. So remeber to remove it before calling it again...just like the above code does already.

Desprez10Author
Known Participant
May 1, 2009

Well, I got that solution to work, but it destroyed my frame rate - to the point where I don't know if I can use it.

Presumably because of the constant adding and deleting of the listener - it's the only real significant difference - unless getAscii() is relatively cpu intensive.

Is there a better way to do this?

EDIT: Hmmm. It's also bonkered the smooth accelleration. Now, when I stop pressing a movement key, it no longer smoothly accelerates to 0, but now simply just halts, untill I press another key, and then it finishes it's stop, while applying the new direction.

May 1, 2009

Maybe you misunderstood me... the above code will not slow down any frame rate in the flash player and will run pretty lean.

It only creates 1 object...the same as creating 1 button basicaly....so you must have an issue somewhere else.