1

 <script>

    var world = [
        [2,2,2,2,2,2,2,2,2,2],
        [2,1,1,2,1,1,1,1,1,2],
        [2,1,1,2,1,2,2,2,1,2],
        [2,1,1,2,1,2,1,2,1,2],
        [2,1,1,2,1,2,1,2,1,2],
        [2,1,1,2,2,2,1,2,1,2],
        [2,1,1,1,1,1,1,2,1,2],
        [2,1,1,1,1,1,1,1,1,2],
        [2,2,2,2,2,2,2,2,2,2],
    ];

    var pacman = {
        x: 29,
        y: 29,
    };

    function displayWorld(){
        var output = '';

        for(var i = 0; i < world.length; i++){
            output += "\n<div class='row'>\n";
            for(var j = 0; j<world[i].length; j++){
                if(world[i][j] == 2)
                    output +="\n<div class='brick'></div>";
                else if (world[i][j] == 1)
                    output += "\n<div class='coin'></div>";
                if (world[i][j] == 0)
                output += "\n<div class='empty'></div>";
            }
            output += "\n</div>";
        }
        //console.log(output);
        document.getElementById('world').innerHTML = output;
    }
    function displayPacman(){
        document.getElementById('pacman').style.left= pacman.x+"px";
        document.getElementById('pacman').style.top= pacman.y+"px";
    }

    displayWorld();
    displayPacman();

    document.onkeydown = function(e){
        if(e.keyCode== 37){
            pacman.x -= 30;
        }
        else if(e.keyCode== 39){
            pacman.x += 30;
        }
        else if(e.keycode== 38){
            pacman.y -= 30;
        }
        else if(e.keycode== 40){
            pacman.y += 30;
        }    
        console.log(e.keyCode);
        displayPacman();
    }
    </script>
</body>
</html>

I am working on a simple Pacman game and I'm having problems getting my Pacman to move up and down. Pacman will move left and right with no problem, which lets me know that my pacman id is working, but my VAR y isn't. It's probably something simple I've missed, but I don't see it. Please help

nessiah
  • 21
  • 2

1 Answers1

3
if (e.keyCode == 37) {
    pacman.x -= 30;
} else if (e.keyCode == 39) {
    pacman.x += 30;
} else if (e.keyCode == 38) {
    pacman.y -= 30;
} else if (e.keyCode == 40){
    pacman.y += 30;
}    

keyCode is case sensitive

You have used

else if(e.keycode== 40){

instead use keyCode

Emre Koc
  • 1,481
  • 10
  • 18
Muthukumaran
  • 147
  • 2
  • 7