0
\$\begingroup\$

I am making a javascript game where the player jumps over oncoming cars, but after the first jump every time the player hits the ground they jump again automatically. I believe the problem is because I am resetting the jumping variable to false when it hits the ground yet it is still going through the initial jump() function so it sees it as false and makes it jump again. Is there any way to fix this?

this.isJumping = function(){
  var y=player.y;
  var jumping;

  if (y=area.canvas.height - 200){
    player.image.src="man.png";
  }
  else{
    jumping=true;
    player.image.src="man2.png";
  }
  return jumping;
}

(And later in the code)

function jump() {
  if (!player.isJumping() && area.frameNo>0) {
    player.speedY = -20;
    jumping=true;
  }
}
\$\endgroup\$
5
  • 1
    \$\begingroup\$ where is the code that calls jump()? \$\endgroup\$ Jun 6 '16 at 23:52
  • \$\begingroup\$ this.canvas.addEventListener("click", jump); \$\endgroup\$ Jun 7 '16 at 0:51
  • \$\begingroup\$ You show us some code but it is not the part we are interested in : Where do you call the "jump" function? The problem is obviously in that place, as you state yourself "the player jumps again as soon as it touches the ground" \$\endgroup\$ Sep 23 '16 at 15:51
  • \$\begingroup\$ If jump is just the handler, my solution below won't work either, as your click isn't going to flip the value of jumping. You need to add in mouse down and mouse up as different events. \$\endgroup\$
    – Bob_Gneu
    Nov 28 '16 at 5:53
  • \$\begingroup\$ You've written = instead of ==? \$\endgroup\$
    – user253751
    Nov 19 '20 at 23:05
1
\$\begingroup\$

Update - Nov 28, 2016

One solution to your issue is to switch to mousedown and mouseup with the following event handler, as shared before:

function jumpHandler() {
    if (!player.isJumping() && area.frameNo > 0) {
        player.speedY = -20;
        jumping = true;
    } else {
        jumping = false;
    }
}

Another solution would be to switch over to Phaser input events, which make a bit more contextual sense, but might require some work depending on your implementation.

Phaser also has mouseDown and mouseUp detection which would then be passed to the same handler as before.

All of the above should work, but requires some testing for triage and addressing the issue.

Initial Answer

A quick and easy solution, one that would still depend on your other implementation details, is to have the else block for your jump method set jumping to false.

function jump() {
    if (!player.isJumping() && area.frameNo > 0) {
        player.speedY = -20;
        jumping = true;
    } else {
        jumping = false;
    }
}

Looking at your code makes me think that there may be some scoping issues that would need to be shared to be able to nail down a more accurate and maintainable solution.

\$\endgroup\$
-1
\$\begingroup\$

You are not setting jumping to false anywhere so it will be true forever, change this:

if (y=area.canvas.height - 200)
{    
    player.image.src="man.png";
}

to this

if (y=area.canvas.height - 200)
{    
    player.image.src="man.png";
    jumping = false;
}

Also, it is not a good idea to use y = area.canvas.height - 200, i would do <= instead, what if the area.canvas.height is odd?

\$\endgroup\$
3
  • \$\begingroup\$ Yes, but since it is changed to false when it touches the ground it immediately runs the jump() function again and makes the player jump \$\endgroup\$ Jun 7 '16 at 14:48
  • \$\begingroup\$ Then your issue is aldo related with area.frameNo, what is it and how do you change it's value? You should reset it once the jump has been done \$\endgroup\$
    – PRDeving
    Jun 8 '16 at 6:57
  • \$\begingroup\$ In the code above, the jumping variable is only ever undefined or true, and making the change suggested is not actually going to change the outcome. This is due to the way that the OP is calling !player.isJumping(), !true is false and !undefined is true, after evaluation. \$\endgroup\$
    – Bob_Gneu
    Nov 27 '16 at 21:45

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.