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 6561159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:33:10+00:00 2026-05-25T13:33:10+00:00

Diagonal move not working and issue on left-longPress/right simultaneous But on double keypress the

  • 0

Diagonal move not working and issue on left-longPress/right simultaneous

But on double keypress the ship goes crazy!

$(document).bind('keydown', function(e) {
    var box = $("#plane"),
        left = 37,
        up = 38,
        right = 39,
        down = 40

    if (e.keyCode == left) {
        box.animate({left: "-=5000"},3000);
    }
    if (e.keyCode == up) {
        box.animate({top: "-=5000"},3000);
    }
    if (e.keyCode == right) {
        box.animate({left:"+=5000"},3000);
    }
    if (e.keyCode == down) {
        box.animate({top: "+=5000"},3000);
    }
});
$(document).bind('keyup', function() {
    $('#plane').stop();
});
  • 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-25T13:33:11+00:00Added an answer on May 25, 2026 at 1:33 pm

    Relying on keyboard events to move an element will make it dependent on the OS key-interval delay. Instead, use the game interval and check the pressed keys stored inside an Object

    On keydown keyup events, if the keyCode returned by event.which is >=37 && <=40 means arrow keys were used. Store inside a K (keys) Object a boolean value for key-number property.

    Interval:

    Inside a window.requestAnimationFrame increase or decrease the x, y position of your element if a key-number property (inside our K object) is true (if(K[37])).

    Diagonal movement:

    Moving an element diagonally using simultaneously i.e. the ↑ and → needs a compensation for the diagonal distance: 1 / Math.sqrt(2) (0.7071..)

    const Player = {
      el: document.getElementById('player'),
      x: 200,
      y: 100,
      speed: 2,
      move() {
        this.el.style.transform = `translate(${this.x}px, ${this.y}px)`;
      }
    };
    
    const K = {
      fn(ev) {
        const k = ev.which;
        if (k >= 37 && k <= 40) {
          ev.preventDefault();
          K[k] = ev.type === "keydown"; // If is arrow
        }
      }
    };
    
    const update = () => {
      let dist = K[38] && (K[37] || K[39]) || K[40] && (K[37] || K[39]) ? 0.707 : 1;
      dist *= Player.speed;
      if (K[37]) Player.x -= dist;
      if (K[38]) Player.y -= dist;
      if (K[39]) Player.x += dist;
      if (K[40]) Player.y += dist;
      Player.move();
    }
    
    document.addEventListener('keydown', K.fn);
    document.addEventListener('keyup', K.fn);
    
    (function engine() {
      update();
      window.requestAnimationFrame(engine);
    }());
    #player{
      position: absolute;
      left: 0;  top: 0;
      width: 20px;  height: 20px;
      background: #000;  border-radius: 50%;
    }
    Click here to focus, and use arrows
    <div id="player"></div>

    or here’s a slight remake using new Set() to keep record of the currently pressed keyboard keys, and new Map() to register functions for every pressed key:

    const Player = {
      el: document.querySelector("#player"),
      x: 200,
      y: 100,
      speed: 2,
      move() {
        this.el.style.translate = `${this.x}px ${this.y}px`;
      }
    };
    
    const getDistance = () => Player.speed * ((kbd.has("ArrowUp") || kbd.has("ArrowDown")) && (kbd.has("ArrowLeft") || kbd.has("ArrowRight")) ? 0.707 : 1);
    
    // Watch pressed keyboard keys:
    const kbd = new Set();
    const handleArrowKeys = (evt) => {
      if (!keyActions.has(evt.key)) return; // Do nothing. Not a registered key
      evt.preventDefault(); // Prevent browser default action
      kbd[evt.type === "keydown" ? "add" : "delete"](evt.key); // Add or delete a key
    };
    addEventListener("keydown", handleArrowKeys);
    addEventListener("keyup", handleArrowKeys);
    
    // Register keyboard keys actions
    // Set a desired function for every desired key:
    const keyActions = new Map();
    keyActions.set("ArrowRight", () => {
      Player.x += getDistance();
    });
    keyActions.set("ArrowLeft", () => {
      Player.x -= getDistance();
    });
    keyActions.set("ArrowUp", () => {
      Player.y -= getDistance();
    });
    keyActions.set("ArrowDown", () => {
      Player.y += getDistance();
    });
    
    const engine = () => {
      // Trigger a keyAction for pressed keys
      kbd.forEach((key) => keyActions.get(key)());
      Player.move();
      requestAnimationFrame(engine);
    };
    
    engine();
    #player { position: absolute; left: 0; top: 0; width: 20px; height: 20px; background: #000; border-radius: 50%; }
    Click here to focus, and use arrow keys
    <div id="player"></div>
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the sum for a diagonal top-left to bottom-right: public int sumarDiagonal() {
And by adjacent, I only mean one unit left, right, up, or down. Diagonals
How can I create a diagonal line from the lower left corner to the
I'm trying to basically code: double squared(diagonal) = a(squared) + b(squared); Can anyone help
A=[a_11, a_12; a_21, a_22] The skew diagonal is [a_12, a_21] . Right now, I
I need to test if one variance matrix is diagonal. If not, I'll do
I have a diagonal line starting from the left bottom part of the screen
i'm trying to create a diagonal line with jQuery that animates from top left
When using wireframe fill mode in Direct3D, all rectangular faces display a diagonal running
How do you draw text on a diagonal? In other words, a horizontal UILabel

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.