Jumping game only jumps once

  • 1
  • Problem
  • Updated 4 years ago
I am making a simple game in javascript where a player jumps over an oncoming car. Right now the character will jump over the first car, but then it will not jump again. Here is my jumping code:

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

if (y<=area.canvas.height - 200){
jumping=false}
return jumping;
}

How can I make it so that the player (a component) will jump again after reaching the ground, but not jump again in air?
Photo of Daniel Galada

Daniel Galada

  • 2 Posts
  • 0 Reply Likes

Posted 4 years ago

  • 1
Photo of Petra V.

Petra V., Champion

  • 7794 Posts
  • 1391 Reply Likes
this.isJumping = function(){
var y=player.y;
var jumping=false;

So, the variable 'jumping' is set to false, now.

if (y<=area.canvas.height - 200){
jumping=false}

And if this condition is true, it is set to twice as false.

return jumping;
}

This will definitely return false.
Photo of Daniel Galada

Daniel Galada

  • 2 Posts
  • 0 Reply Likes
If it is always false why isn't it jumping if the code calling for it is:

function jump() {
if (!player.isJumping()) {
player.speedY = -50;
}}

The code should be jumping infinitely then shouldn't it be?
Photo of Petra V.

Petra V., Champion

  • 7794 Posts
  • 1391 Reply Likes
Perhaps your function jump() isn't called multiple times. Or perhaps 'player' doesn't have a global scope. Who knows?

So far, you have just shown a couple of code snippets, not the whole application.
Anyway, this doesn't seem to be even slightly related to Phonegap Build...