0
\$\begingroup\$

I'm brand new to game development (with Javascript)... I'm pretty sure my question is very simple, but I have a hard time to figure out myself... I'm starting at the very beginning, so this is pretty simplistic.

This is my whole code in javascript:

var fire = false;
function keyUp(evt) {
  switch (evt.keyCode) {
     case SPACE:
        fire = false;
     break;
     ...
  }
}
function keyDown(evt) {
  switch (evt.keyCode) {
     case SPACE:
        fire = true;
     break;
     ...
  }
}
function update() {
    ...
    if (fire) {
        playerHasShot = true;
        shot.x = player.x + player.w;
        shot.y = player.y + (player.h / 2);
        shot.vx += 2;
    }
    if (!fire) {
        playerHasShot = false;
    }
   ...
    shot.x += shot.vx;
   ...
    //Bullet
    if (playerHasShot) {
        playerHasShot = false;
    }
    if (!playerHasShot) {
        playerHasShot = false;
    }
    ...
    if (shot.vx > 4) {
        shot.vx = 4;
    }
    ...
    if (shot.x > canvas.width) {
        shot.x = player.x + player.w;
        shot.y = player.y + (player.h / 2);
        playerHasShot = false;
    }
    ...
    render();
    renderShot();
}

I hit the spacebar once, and that guy can't stop shooting... Would any of you mind telling me how to stop him, please ? I'm sure I'm close, but there's something I'm missing somewhere.... Thanks! :)

\$\endgroup\$
9
  • \$\begingroup\$ Where do you use playerHasShot? \$\endgroup\$
    – Bálint
    Feb 16 '18 at 11:03
  • 1
    \$\begingroup\$ Most of that code doesn't seem to be relevant to shooting. I would recommend you to remove all the movement- and drawing-related code from the question. The less code you post, the more people will actually bother to read it all. I, for one, lost interest after about a minute trying to find out where you actually handle shooting. \$\endgroup\$
    – Philipp
    Feb 16 '18 at 12:14
  • \$\begingroup\$ @Bàlint, It's used in the update function. if (playerHasShot) { and if (!playerHasShot) {... \$\endgroup\$
    – EricF
    Feb 16 '18 at 12:15
  • 1
    \$\begingroup\$ when and how is fire set? \$\endgroup\$
    – Brian H.
    Feb 16 '18 at 13:22
  • 1
    \$\begingroup\$ Welcome to GDSE. For future reference, you should read how to make a minimal, complete, and verifiable example (MCVE). Back to this though, can you verify if keyUp(evt) ever gets called? Either fire is being set to true & never changes, or it does temporarily changed to false, but thing gets switched back to true again by mistake. Narrow the problem down to one of those two. \$\endgroup\$
    – Pikalek
    Feb 16 '18 at 15:26
0
\$\begingroup\$

If I understand your code correctly, then fire appears to be the flag which says if the player pressed space this frame. When that's true, and you want to spawn one bullet each time the player presses space, you need to reset that flag after spawning the bullet.

if (fire) {
    /* 
    ...code to spawn the projectile...
    */
    fire = false;
}

You won't need to do anything in case of a keyUp event, because releasing the spacebar isn't doing anything. That event gets interesting when you want autofire (spawining bullets while the player holds down space). In that case you would do fire = false in the keyup event instead of your main loop. But your code currently doesn't seem to be designed to handle more than one bullet at a time, so I don't think that this is what you want to do (at least for the moment).

I am not sure what the playerHasShot variable is supposed to mean in your code, because while you set and unset it various times, it doesn't seem to actually do anything. But I know that this section of code:

if (playerHasShot) {
    playerHasShot = false;
}
if (!playerHasShot) {
    playerHasShot = false;
}

doesn't make any sense. After the first three lines, playerHasShot will be false (or at least "falsey" if that variable can have non-boolean values), no matter what value it had before. The last 3 lines won't do anything at all. It literally means "when playerHasShot is falsey, set playerHasShot to false". Those 6 lines could be replace with playerHasShot = false and it wouldn't change anything.

\$\endgroup\$
1
  • \$\begingroup\$ you got my intent correctly. I thought I had to create a variable playerHasShot to be aware if the player fired or not, but I agree: It doesn't do anything... \$\endgroup\$
    – EricF
    Feb 16 '18 at 23:35
-1
\$\begingroup\$

Did you try combining both your switch cases. And just add the keyUp and keydown together switch case,instead of having 2 different switches.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.