Click here to Skip to main content
15,889,867 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
In Html and Javascript I am trying to make the character (a letter O) move with the wasd keys. I have tried everything I can, and I can't get it to work. Can you tell me how to do it. Also, can you be pretty specific... I kind of a beginner. Here is the code.

JavaScript
<!DOCTYPE html>
<canvas id="ctx" width="500" height="500" style="border: 1px solid black;">

    <script>

    
    var ctx = document.getElementById("ctx").getContext("2d");

    var player = {
        pressRight: false,
        pressLeft: false,
        pressDown: false,
        pressUp: false,
        x: 250,
        y: 250,
    };

       ctx.font = "100px,arial";
       ctx.fillText("O",player.x,player.y);
    
   document.onkeydown = function (event){

        if(event.keyCode == "D")        //d
                player.pressRight = true;
        else if(event.keyCode == "S")   //s
                player.pressDown = true;
        else if(event.keyCode == "A") //a
                player.pressLeft = true;
        else if(event.keyCode == "W") // w
                player.pressUp = true;
    }
    
    document.onkeyup = function (event){
            if(event.keyCode == "D")        //d
                    player.pressRight = false;
            else if(event.keyCode == "S")   //s
                    player.pressDown = false;
            else if(event.keyCode == "A") //a
                    player.pressLeft = false;
            else if(event.keyCode == "W") // w
                    player.pressUp = false;
    }
    
    function updatePlayerPosition(){
            if(player.pressRight == true)
                    player.x += 10;
            if(player.pressLeft == true)
                    player.x -= 10;
            if(player.pressDown == true)
                    player.y += 10;
            if(player.pressUp == true)
                    player.y -= 10;
    }

    function drawPlayer(){
        ctx.clearRect(0,0,500,500);
        ctx.font = "100px,arial";
        ctx.fillText("O",player.x,player.y);
    }


    function Update(){
        ctx.clearRect(0,0,500,500);
        updatePlayerPosition();
        drawPlayer();
    }

    setInterval(Update,100);
    
</script>


What I have tried:

I have looked up videos and searched as much as I could on the internet and have not figured it out... I sure its user error but I don't know where I'm messing up.
Posted
Updated 12-Aug-16 13:13pm

Small Correction

KeyCode for <br />
d-> 68<br />
s-> 83<br />
a-> 65<br />
w-> 87 


C#
if (event.keyCode == 68)        //d
                  player.pressRight = true;
              else if (event.keyCode == 83)   //s
                  player.pressDown = true;
              else if (event.keyCode == 65) //a
                  player.pressLeft = true;
              else if (event.keyCode ==87) // w
                  player.pressUp = true;


refer :JavaScript Event KeyCodes[^]
 
Share this answer
 
Comments
Ghostdragon777 12-Aug-16 8:08am    
Thank you for the help
Karthik_Mahalingam 12-Aug-16 8:09am    
welcome :)
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900