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

  • SEARCH
  • Home
  • 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 7595807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:45:47+00:00 2026-05-30T21:45:47+00:00

Let me explain my situation… I’m making a 2D platformer game, where you can

  • 0

Let me explain my situation… I’m making a 2D platformer game, where you can go around and shoot stuff. Going around uses W, A & D keys, and shooting is done with the mouse. When I do all of the actions separately, everything works, but when I click the mouse button, and press a key in the same time, my code starts acting as if the key was still pushed down. This happens only sometimes.

I register all the keyboard events like this:

<body onload="init()" onkeydown="press(event);" onkeyup="release(event);">

Here’s the script that handles that:

var KEY = {W: 87, A: 65, S:83, D: 68, E: 69};

var input = {
    right: false,
    up: false,
    left: false,
    down: false,
    e: false
};

function press(evt) {
    var code = evt.keyCode;

    switch(code) 
    {
        case KEY.W: input.up = true; break;
        case KEY.A: input.left = true; break;
        case KEY.S: input.down = true; break;
        case KEY.D: input.right = true; break;
        case KEY.E: input.e = true; break;
    }
}

function release(evt)
{
    var code = evt.keyCode; 
    input.code = code;

    switch(code) 
    {
        case KEY.W: input.up = false; break;
        case KEY.A: input.left = false; break;
        case KEY.S: input.down = false; break;
        case KEY.D: input.right = false; break;        
        case KEY.E: input.e = false; break;
    }
}

Even when I don’t register mouse events, this happens. Can someone explain why? And how do I fix this?

  • 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-05-30T21:45:48+00:00Added an answer on May 30, 2026 at 9:45 pm

    EDITED

    It seems, that the jerky behavior is not occasionally. If you press and keep left and then press up and release left, the movement continues. But, if you release up after a while, movement will stop. Ie. you can make continuous circular moves, but you can’t get back to the original direction. This perhaps dues to the eventhandling process, using stack-type saving for event calls and their return-points.

    Below is an example code, which does the trick. It uses arrow-keys to move the spot. The code is in global space for clarity, and done for IE only, but you just edit it as you wish.

    <html>
    <head>
    <script>
    function move(elem){
        if(i[37])elem.style.left=elem.style.pixelLeft-2;
        if(i[38])elem.style.top=elem.style.pixelTop-2;
        if(i[39])elem.style.left=elem.style.pixelLeft+2;
        if(i[40])elem.style.top=elem.style.pixelTop+2;
        return;
    }   
    
    function keyDn(){
        var key=window.event.keyCode;
        if(i[key]!==undefined) i[key]=true;
        return;
    }
    
    function keyUp(){
        var key=window.event.keyCode;
        if(i[key]!==undefined) i[key]=false;
        return;
    }
    
    window.onload=function (){
        spot=document.getElementById('movablespot');
        i={'37':false,'38':false,'39':false,'40':false};
        document.body.onkeydown=keyDn;
        document.body.onkeyup=keyUp;    
        a=setInterval(function (){move(spot);return;},5);
    }    
    </script>
    </head>
    <body>
    <span id="movablespot" style="position:absolute;top:100px;left:100px;">O</span>
    </body>
    </html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let me try to explain the situation the best I can. Lets say I
Let me try to explain what the situation is as thoroughly as I can
Let me explain my particular situation: A business can pick a neighbourhood they are
Alright let me explain my situation first: I am part of an organization that
let me explain my current situation i have a SharePoint site lets say it
Let me explain the situation before I ask the question. I have a site,
OK, this is impossible, but I will try to explain the situation here. Let's
Let me explain my situation: I'm using a MVC framework (CodeIgniter), so every request
Let me explain my question by posing a hypothetical situation. Lets start with a
Let me explain my situation. I have made a user control that contains an

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.