Jump to content

player movement issue - player doesnt jump as expected


kriket
 Share

Recommended Posts


 

Controls = click on right side of screen to move right and vice-versa. To jump, click in the middle. But the player doesn't jump properly. 

 

 

Here's the tiny code snippet that deals with player movement.

 

In update() function,



    if (this.input.activePointer.isDown)
    {
        if(this.input.activePointer.x < this.game.width/3)
        {
                this.player.body.moveLeft(200);
                if (this.facing != 'left')
                {
                    this.player.animations.play('left');
                    this.facing = 'left';
                }
        }

        else if ((this.input.activePointer.x >= this.game.width/3) && (this.input.activePointer.x < this.game.width * 2/3) && (this.game.time.now > this.jumpTimer) && this.checkIfCanJump())
        {
            this.player.body.moveUp(300);

            if (this.facing == 'right') {
                this.player.animations.play('jumpR');
            }
            else if (this.facing == 'left') {
                this.player.animations.play('jumpL');
            }
            else {
              this.player.frameName = 'front.png';
            }
            this.jumpTimer = this.game.time.now + 750;
        }
        else
        {
                this.player.body.moveRight(200);
                if (this.facing != 'right')
                {
                    this.player.animations.play('right');
                    this.facing = 'right';
                }
        }
    }
      
    else {
        this.player.body.velocity.x = 0;
        this.player.animations.stop();
        if (this.facing != 'idle') 
        {
          if (this.facing == 'left')
          {
              this.player.frameName = 'standingL.png';
          }
          else if (this.facing == 'right')
          {
              this.player.frameName = 'standingR.png';
          }
          else
          {
              this.player.frameName = 'front.png';
          }


        this.facing = 'idle';
        }
    }


 

 

Please advise. Thanks.

 

 

 

Link to comment
Share on other sites

The problem is that this happens in the update loop. So when you press to jump, it first jumps but your mouse is still pressed down the next update calls, so it enters on the else branch where it moves to the right. You might want to change it so it only moves right if it isn't jumping, or to only move right if the pointer is actually in the right side.

 

The easiest way to fix this would be to re-order your conditions, so that after you check if it is left tap check if it is a right tap and only after that check if the tap is in the middle and can jump.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...