the reacting time on (key.isDown) is slow

  • partyjan
  • Student
  • Student
  • partyjan
  • Posts: 69
  • Loc: Norway

Post 3+ Months Ago

This is a part of the code I use...
It woks, but when I press down the "left,right,up and down" keys it takes long time before it reacts...

It is a object that is supposed to move around on the screen...
but when I press the keys it takes up to maybe 1-3 seconds before he turns...

The question is, is there a way that the reacting time will be faster?





Code: [ Select ]

var KeyListener1:Object = new Object();
var KeyListener2:Object = new Object();

Key.addListener(KeyListener1);
Key.addListener(KeyListener2);

KeyListener1.onKeyDown = function () {
    
    
    

    
    if (Key.isDown(Key.LEFT)){
            Xhastighet--;
            break;
            
            if (fisk._xscale > 0)
            fisk._xscale *= -1;
        }
        
            if (Key.isDown(Key.RIGHT)){
            Xhastighet++;
            break;
            
            if (fisk._xscale < 0)
            fisk._xscale *= -1;
        }
}


KeyListener2.onKeyDown = function () {
    if (Key.isDown(Key.UP)){
            Yhastighet--;
            break;
        }
        
            if (Key.isDown(Key.DOWN)){
            Yhastighet++;
            break;
        }
}
  1. var KeyListener1:Object = new Object();
  2. var KeyListener2:Object = new Object();
  3. Key.addListener(KeyListener1);
  4. Key.addListener(KeyListener2);
  5. KeyListener1.onKeyDown = function () {
  6.     
  7.     
  8.     
  9.     
  10.     if (Key.isDown(Key.LEFT)){
  11.             Xhastighet--;
  12.             break;
  13.             
  14.             if (fisk._xscale > 0)
  15.             fisk._xscale *= -1;
  16.         }
  17.         
  18.             if (Key.isDown(Key.RIGHT)){
  19.             Xhastighet++;
  20.             break;
  21.             
  22.             if (fisk._xscale < 0)
  23.             fisk._xscale *= -1;
  24.         }
  25. }
  26. KeyListener2.onKeyDown = function () {
  27.     if (Key.isDown(Key.UP)){
  28.             Yhastighet--;
  29.             break;
  30.         }
  31.         
  32.             if (Key.isDown(Key.DOWN)){
  33.             Yhastighet++;
  34.             break;
  35.         }
  36. }
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • graphixboy
  • Control + Z
  • Mastermind
  • User avatar
  • Posts: 1828
  • Loc: In the Great White North

Post 3+ Months Ago

I don't know if framerate affects listeners but try upping the framerate to around 30. I've never noticed lag with a key press
  • IceCold
  • Guru
  • Guru
  • User avatar
  • Posts: 1254
  • Loc: Ro

Post 3+ Months Ago

Also, why do you use 2 listeners?
use only one listener, and place all the code inside it's onKeyDown method.
Anyway, it shouldn't really matter about the framerate. I made a game with 12 frames/sec and i have instant react on key press, only i didn't use a listener :D
i used:
Code: [ Select ]
this.onEnterFrame = function()
{
    DetectKeys();
}

function DetectKeys()
{
    if (Key.isDown(Key.LEFT))
    {
        // move left
        // flip horizontal if it was right flipped
        if (mcHero._xscale < 0)
            mcHero._xscale *= -1;
    }
    
    if (Key.isDown(Key.RIGHT))
    {
        // move right
        // flip horizontal if it was left flipped    
        if (mcHero._xscale > 0)
            mcHero._xscale *= -1;
    }
    
    if (Key.isDown(Key.UP))
    {
        // move up
    }
    
    if (Key.isDown(Key.DOWN))
    {
        // move down
    }    
}
  1. this.onEnterFrame = function()
  2. {
  3.     DetectKeys();
  4. }
  5. function DetectKeys()
  6. {
  7.     if (Key.isDown(Key.LEFT))
  8.     {
  9.         // move left
  10.         // flip horizontal if it was right flipped
  11.         if (mcHero._xscale < 0)
  12.             mcHero._xscale *= -1;
  13.     }
  14.     
  15.     if (Key.isDown(Key.RIGHT))
  16.     {
  17.         // move right
  18.         // flip horizontal if it was left flipped    
  19.         if (mcHero._xscale > 0)
  20.             mcHero._xscale *= -1;
  21.     }
  22.     
  23.     if (Key.isDown(Key.UP))
  24.     {
  25.         // move up
  26.     }
  27.     
  28.     if (Key.isDown(Key.DOWN))
  29.     {
  30.         // move down
  31.     }    
  32. }

Post Information

  • Total Posts in this topic: 3 posts
  • Users browsing this forum: No registered users and 8 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.