Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9192635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:58:23+00:00 2026-06-17T20:58:23+00:00

im creating a simple 2d game, and I want save the keydown keys into

  • 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)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T20:58:25+00:00Added an answer on June 17, 2026 at 8:58 pm

    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).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So i am creating a simple game in unity where i want to use
I'm developing a simple android game divided into levels. I want a check icon
Greetings! I'm creating a simple snake game. I want to expand my classes in
Im creating a simple bowling game using OOP, and i want to have a
I am new to XNA, and I am creating a simple game. Sorry that
Creating a simple RPG game, first time using XNA. Trying to get my character
I'm creating a simple labyrinth game with Java + Swing. The game draws a
I am creating a simple tennis game in Objective-C. Currently, when the player or
I'm pretty new to programming, and I'm creating a simple text-based game.> I'm wondering
im creating simple breakout game, in which i have continuing falling bricks which are

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.