0
votes

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;
        }
    }
}
1
You dont call performAction()in your exemple. - Lauromine
Your not comparing the actual key pressed against keyPressed values, your if statements should follow the logic if(e.keyCode == keyPressed.Kleft){} - PalinDrome555

1 Answers

1
votes

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);
}