0

So i am making a html5 game where i was working in movement. Player can move with w, a, s ,d in key. That is working well but i wanted to add a power ..like if player press a + space-bar or d + space-bar an ability will trigger.

i used and operator and it should have worked in my theory but it's not working. I am new in html5 and any help will be appreciated. thanks in advance.

Here is my code... Just copy paste on html note.

<canvas id="canvas" width="800" height="500" style="border:1px solid #000000;"></canvas>

<script>
    var c = document.getElementById("canvas").getContext("2d");


    var WIDTH = 800;
    var HEIGHT = 500;


    var player = {
        x: Math.random() * WIDTH,
        y: Math.random() * HEIGHT,
        width: 30,
        height: 30,
        color: 'black',

        pressingDown: false,
        pressingUp: false,
        pressingLeft: false,
        pressingRight: false,


        pressinpowerRight: false,
        pressinpowerLeft: false,
    };



    drawEntity = function(e) {
        c.fillStyle = e.color;
        c.fillRect(e.x - e.width / 2, e.y - e.height / 2, e.width, e.height);
    }


    document.onkeydown = function(event) {
        if (event.keyCode === 68) //d
            player.pressingRight = true;
        else if (event.keyCode === 83) //s
            player.pressingDown = true;
        else if (event.keyCode === 65) //a
            player.pressingLeft = true;
        else if (event.keyCode === 87) // w
            player.pressingUp = true;


        else if (event.keyCode === 68 && event.keyCode === 32) // this statement is not working
            player.pressinpowerRight = true;
        else if (event.keyCode === 65 && event.keyCode === 32) // this statement is not working

            player.pressinpowerLeft = true;

    }

    document.onkeyup = function(event) {
        if (event.keyCode === 68) //d
            player.pressingRight = false;
        else if (event.keyCode === 83) //s
            player.pressingDown = false;
        else if (event.keyCode === 65) //a
            player.pressingLeft = false;
        else if (event.keyCode === 87) // w
            player.pressingUp = false;

        else if (event.keyCode === 68 && event.keyCode === 32) // not working
            player.pressinpowerRight = false;
        else if (event.keyCode === 65 && event.keyCode === 32) // not working
            player.pressinpowerLeft = false;
    }

    updatePlayerPosition = function() {
        if (player.pressingRight)
            player.x += 5;
        if (player.pressingLeft)
            player.x -= 5;
        if (player.pressingDown)
            player.y += 5;
        if (player.pressingUp)
            player.y -= 5;

        if (player.pressingpowerRight)
            player.x += 50;
        if (player.pressingpowerLeft)
            player.x -= 50;
    }

    update = function() {
        c.clearRect(0, 0, WIDTH, HEIGHT);

        updatePlayerPosition();
        drawEntity(player);

    }

    setInterval(update, 40);
</script>

1 Answers1

0

Try creating a previousButtonKey global variable and always record the last key, then check the previousButtonKey with another key to combine two keys on your condition.

Ex. First key is to be pressed is 'Shift', store it to a variable, then once another key event happens, check the previous and the new key. If it is valid, then do the action that you want.

Luke Villanueva
  • 2,030
  • 8
  • 44
  • 94
  • OK thanks a lot for the logic but i am new in js i am not finding the right code to do that.. so can you please provide the code for this logic. thanks again. – Asaduzzaman Arman Sep 11 '17 at 01:46