0

So the operating system has a delay when you hold down a key. It will execute the command once, delay, then repeat. I tried to bypass this with setting a variable to true or false based on when the key has "keydown" and "Keyup." I am not sure why it is not working though. I am not sure if I am forgetting about something, but If someone could explain why my code is still not fixing the issue.

Variables

var keyPressed = {Kleft: 37, Kright: 39, Kup: 38,  Kdown: 40, Kspace: 32};
    key = {K37: false, K39: false, K38: false, K40: false, K32: false};

detect for key being pressed

//to detect multiple keys being pressed 
    $(document).keydown(function(e) {
        if(keyPressed.Kleft)    //left arrow
            {
                console.log('test');
                key.K37 = true;
                airplane.performAction();
            }
        if(keyPressed.Kright) //right arrow
            {
                key.K37 = true;
                // airplane.performAction();
            }
        if(keyPressed.Kup)  //up arrow
            {
                key.K37 = true;
                // airplane.performAction();
            }
        if(keyPressed.Kdown) //down arrow
            {
                key.K37 = true;
                // airplane.performAction();
            }
        if(keyPressed.Kspace)   //space key
            {
                key.K37 = true;
                bullet = new Bullet({x: airplane.position.x+32, y: airplane.position.y-12}, bullets_total++); //create a new bullet
                bullets.push(bullet); //store the bullet in the bullets array
            }
    }).keyup(function(e) {
        key.K37 = false; //testing just one key for now
        console.log('ehhlo');
        // console.log(key['K'+toString(e)]);
        // key['K'+toString(e)] = false;
    });

the executed function on Keypress

function MyAirplane()
{
    this.performAction = function(action)
    {
        if(key.K37 === true){
            console.log('left');
            this.position.x -= 10;
        }
        if(key.K37 == true){
            this.position.x += 10;
        }
        if(key.K37 == true){
            this.position.y -= 10;
        }
        if(key.K37 == true){
            this.position.y += 10;
        }
    }
}
2
  • You dont call performAction()in your exemple. Commented Sep 12, 2015 at 0:18
  • Your not comparing the actual key pressed against keyPressed values, your if statements should follow the logic if(e.keyCode == keyPressed.Kleft){} Commented Sep 12, 2015 at 0:26

1 Answer 1

1

Just an idea, try not automatically calling performAction.

//to detect multiple keys being pressed 
$(document).keydown(function(e) {
    if(keyPressed.Kleft)    //left arrow
        {
            key.K37 = true;
        }
    if(keyPressed.Kright) //right arrow
        {
            key.K37 = true;
        }
    if(keyPressed.Kup)  //up arrow
        {
            key.K37 = true;
        }
    if(keyPressed.Kdown) //down arrow
        {
            key.K37 = true;
        }
    if(keyPressed.Kspace)   //space key
        {
            key.K37 = true;
            bullet = new Bullet({x: airplane.position.x+32, y: airplane.position.y-12}, bullets_total++); //create a new bullet
            bullets.push(bullet); //store the bullet in the bullets array
        }
}).keyup(function(e) {
    key.K37 = false; //testing just one key for now
    console.log('ehhlo');
    // console.log(key['K'+toString(e)]);
    // key['K'+toString(e)] = false;
});

And create an event timer:

function MyAirplane()
{
    this.performAction = function(action)
    {
        if(key.K37 === true){
            console.log('left');
            this.position.x -= 10;
        }
        if(key.K37 == true){
            this.position.x += 10;
        }
        if(key.K37 == true){
            this.position.y -= 10;
        }
        if(key.K37 == true){
            this.position.y += 10;
        }
    }
    // 60 frames per second event checker.
    var eventLoop = setInterval(this.performAction.bind(this), 1000 / 60);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.