Forums

Give help. Get help.

  • # December 23, 2017 at 4:00 am

    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;
    // }
    // }
    }

    # January 2, 2018 at 2:53 pm

    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)

You must be logged in to reply to this topic.

icon-anchoricon-closeicon-emailicon-linkicon-logo-staricon-menuicon-nav-guideicon-searchicon-staricon-tag