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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:24:31+00:00 2026-06-12T07:24:31+00:00

I don’t know how to solve the problem: When I keep left+space pressed down

  • 0

I don’t know how to solve the problem:
When I keep left+space pressed down together, the animation works,but when I keep left pressed (without leave it) and then I press space (one time, kept pressed left), the animation jumps but it doesn’t go left… The animation stops even if I press some other key (to reset it I must leave all). Can someone explain me why is it so and give me some practical solution? Thank you very much! This is the code:

<style>
#map{
    position:relative;
    height:220px;
    width:800px;
}
#character{
    height:100px;
    width:100px;
    position:absolute;
    bottom: 10px;
    left: 0px;
    z-index:1;
}
</style>
<div id="map"><div id="character"><img src="images/bart.gif"></div></div>
<script>
enableJump = true;
jump = function(){
    if(enableJump){ enableJump = false;
        salta = function(){
            if(flagY){
                posY++;
                if(posY >= 90) flagY = false;
                jj = document.getElementById('character').style.bottom=posY;
            }
            else{
                posY--;
                jj = document.getElementById('character').style.bottom=posY;
                if(posY < 10){
                    flagY = true;
                    clearInterval(j);
                    enableJump = true;
                    up = false;
                }
            }
        }
        j = setInterval(salta,8);
    }
}
up = false; dx = false;
document.onkeydown = function(evt){
    code = evt.keyCode;
    if(code == 32) up=true;
    if(code == 39) dx=true;
    moveCharacter();
}
document.onkeyup = function(evt){
    code = evt.keyCode;
    if(code == 32) up=false;
    if(code == 39) dx=false;
}
posX = 10; posY = 10; flagY = true; var j;
moveCharacter = function(){
    if(dx == true && up == true){
        jump();
        posX+=10;
        x1 = document.getElementById('character').style.backgroundImage='url(images/character_left.gif)';
        temp = document.getElementById('character').style.left=posX;
    }
    else if(dx){
        posX+=10;
        x1 = document.getElementById('character').style.backgroundImage='url(images/character_left.gif)';
        temp = document.getElementById('character').style.left=posX;
    }
    else if(up){ jump(); }
}
</script>

Code cleaned (without jump but with the same problem, keeping pressed left and pressing another key it stops and I must leave it to reset it)

<script>
var dx = false;
var posX = 10;
var posY = 10;
document.onkeydown = function(evt){
    if(evt.keyCode == 39) dx=true;
    moveCharacter();
}
document.onkeyup = function(evt){
    if(evt.keyCode == 39) dx=false;
}
moveCharacter = function(){
    if(dx){
        posX+=10;
        document.getElementById('character').style.left=posX;
    }
}
</script>
  • 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-12T07:24:32+00:00Added an answer on June 12, 2026 at 7:24 am

    Finally i solved the problem:
    Functions keydown, keyup, movecharacter were deleted.
    I added:

    var keysDown = {};
    try {
        if (window.addEventListener) {
            window.addEventListener("keydown", function (v) {keysDown[v.keyCode] = true;}, false);
            window.addEventListener("keyup", function (v) {delete keysDown[v.keyCode];}, false);
        } else if (document.attachEvent) {
            document.attachEvent("onkeydown", function (v) {keysDown[v.keyCode] = true;});
            document.attachEvent("onkeyup", function (v) {delete keysDown[v.keyCode];});
        } else if (window.attachEvent) {
            window.attachEvent("onkeydown", function (v) {keysDown[v.keyCode] = true;});
            window.attachEvent("onkeyup", function (v) {delete keysDown[v.keyCode];});
        } else {
            document.addEventListener("keydown", function (v) {keysDown[v.keyCode] = true;}, false);
            document.addEventListener("keyup", function (v) {delete keysDown[v.keyCode];}, false);
        }
    } catch (e) {
        alert("Keys don't work!\nError: "+e);
    }
    ...
    var update = function () {
        if (38 in keysDown) { // Player holding up
            ..
        }
        if (40 in keysDown) { // Player holding down
            ..
        }
        if (37 in keysDown) { // Player holding left
            ..
        }
        if (39 in keysDown) { // Player holding right
            ..
        }
    };
    setInterval(update,10);
    

    Reference: http://www.lostdecadegames.com/how-to-make-a-simple-html5-canvas-game/
    http://stackoverflow.com/questions/9507200/why-keydown-listener-doesnt-work-in-ie

    It works with Chrome, Firefox, IE7/8/Quirks mode (not in document mode IE9 standards)
    Thank for your interest!

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

Sidebar

Related Questions

Don't know if this is an eclipse specific problem but whenever I declare a
Don't know how to frase this but I found this code wich works as
don't know if the title describes anything about what I'm trying to say but
don't know what happened in my jcrop selection. I think I pressed some keys
Don't really know how to formulate the title, but it should be pretty obvious
Don't know how to google for such, but is there a way to query
Don't know why but font is not displaying.Please help. CSS(in css folder): style.css: @font-face
I don't know why, but this code worked for me a month ago... maybe
don't know better title for this, but here's my code. I have class user
Don't know whats exactly going on, but it's definitely killing my time for nothing.

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.