0

im creating a simple 2d game, and I want save the keydown keys into array, and execute them inisde a loop, so the user can hold a key and make it look like the chartater is moving non stop.

i got a setInterval function that act like a game Timer, and it just loop it self all the time. i added a listener and an array to hold the key.

I checked the keys inside the array and it look fine but, the functions moveRight and moveLeft are not working for some reson.

here is the code:

      this.keysPressed = new Array();
      InitGameLoop: function () {
        var that = this;
        setInterval(function () {
            $(document).keydown(function (e) {
                var key = e.which;
                that.keysPressed.push(key);
                for (var i = 0; i < that.keysPressed.length; i++) {
                    if (that.keysPressed[i] == 38) {
                        that.moveRight(worldWidth, 10);
                    }
                    else if (that.keysPressed[i] == 37) {
                        that.moveLeft(10);
                    }
                    log(that.keysPressed, that.yPos);
                    that.keysPressed.pop();
                }
            });
        }, 60);

my questions are:

  1. what am i doing worng?
  2. is this a good idea? (if not, please feel free to recommend me about another :) )

(sorry for my english)

4
  • I don't get it. You just want to move the character right as long as the right key is pressed, and same for the left key? Commented Jan 27, 2013 at 19:53
  • i have more functions like jump and shoot, but i cant pressed on them in the same time and execute them. so i thoghot to store them and excute them in a loop 1 afther 1. Commented Jan 27, 2013 at 19:57
  • There is no reason to store them in an array. Just process each key as it comes. Commented Jan 27, 2013 at 20:07
  • i tried that, but when i add a jump into this, i cant make it look like im jumping and moving at the same time. Commented Jan 27, 2013 at 20:09

2 Answers 2

1

Registering an eventhandler inside setInterval is always wrong. In your case, every 60 milliseconds you are creating an additional listener, and when you press a key, all of your listeners will fire. Also there is absolutely no need to store them in an array. Just register the listener once, and it will fire each time a key is pressed. If more than one keys are pressed, the listener will fire for each key individually.

$(document).keydown(function (e) {
     var key = e.which;
     console.log(key);
     // call your according functions here
     switch (key){
     case 37: // moving left
        // do stuff
        // set a flag to indicate that you are moving left
        moveleft = true;
        break;
     case 39: // moving right
        // do stuff
        // set a flag to indicate that you are moving right
        moveright = true;
        break;
     }
});

Since you are catching the keydown, you should set flags. This way you can track which keys are pressed currently. On keyup, you are resetting these flags again (need another eventhandler for that).

Sign up to request clarification or add additional context in comments.

3 Comments

is great and it works, but if i want to do for a exemple Jump + Right it will not work. what can i add to make sure i have a jump + right ability?
you need to work with the flags and check in your function run/move if the according flags are set. E.g. If you are moving right the right flag is set, now if you jump, check if a move flag is set and react accordingly (jump to the right).
thank you, it toke my a while to understand you suggestion, but its working better then i hoped.
1

Instead of storing the pressed keys in an array, make each key code activate a related var in a 'movement' array. e.g. when left is pressed, movement['left']=1. Use keyup to set it back to 0.

Have your loop check the array for each possible movement, and trigger the related functions in corelance to the active movements at that given moment.

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.