Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript Javascript multiple keydown at once

  • This topic is empty.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #264379
    bistercoco
    Participant

    Hello, so I’m learning html5 game programming and I’m now programming a game and I have an issue with simultaneous keydown
    for example I want here to move the plane with the arrows while firing with space
    I need help plz
    Thanks !

    function fire(event){
    if (event.keyCode == 32){
    bullets.push({
    bulletx:planex+90,
    bullety:canvas.height-120
    });
    }
    }

    function moveplane(event) {
    if (event.keyCode == 39) {
    if (planex > canvas.width-200){planex=planex;
    }
    else
    {
    planex=planex+planespeed;
    }
    }
    if (event.keyCode == 37) {
    if (planex canvas.width-200){planex=planex;
    // }
    // else
    // {
    // planex=planex+planespeed;
    // }
    // }
    }

    #264792
    mpboom
    Participant

    It would be helpful if could post a bit more code, and in a better way.

    However, something like this would work, I believe:

    document.addEventListener("keypress", planeAction)
    
    function planeAction() {
      if (event.key = ‘KEY TO FIRE‘) {
        //fire
      } else if (event.key = ‘KEY TO MOVE LEFT’) {
        //move left
      ... and so on
    

    You seem to have two functions – do you also have two event listners?

Viewing 2 posts - 1 through 2 (of 2 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.