Jump to content

Can't jump and walk at the same time


Santiago
 Share

Recommended Posts

Hi, I'm programming a simple platformer game, and I'm programming and setting the movement of the player. Here's my update function:

update: function () {
        
        game.physics.arcade.collide(this.player, this.platform);
        game.camera.follow(this.player);
       
            if(this.cursor.right.isDown){
                this.player.body.velocity.x= 200;
                this.player.animations.play('correr', 5, true);
                this.player.scale.x=1;
            }
            
            else if(this.cursor.left.isDown){
                this.player.body.velocity.x= -200;
                this.player.animations.play('correr', 5, true);
                this.player.scale.x=-1;
            
            }
        
            else if(this.jump.isDown && this.player.body.wasTouching.down) {
                this.player.body.velocity.y= -400
            }   
                
            else if((this.cursor.right.isDown || this.cursor.left.isDown) && this.jump.isDown){
                this.player.body.velocity.x= 200;
                this.player.body.velocity.y=-200;
                
            }
        
            else{
                this.player.body.velocity.x = 0;
                this.player.animations.stop();
                this.player.frame = 4;
            }
        
           
        }

Everything works fine, but in my last else if is suppose that the player should jump and walk, but it doesn't work! My intention is the player could jump while walking pressing the jump key + left or right key, for now I just can jump first and then walk.

I don't know why this las if else it isn't being executed, because I tried to move it in the first if clause and it worked perfectly, but I can't realise about the error.

Thanks for helping.

Link to comment
Share on other sites

Just glancing at that code, your chain of if/elses is written in such a way that you can never actually get at this portion:

else if((this.cursor.right.isDown || this.cursor.left.isDown) && this.jump.isDown){
                this.player.body.velocity.x= 200;
                this.player.body.velocity.y=-200;
                
            }

The first two conditions checking for left & right will always be accessed before this above one, and that's because you've chained all those ifs to be dependent on each other; if you can't do if 1., try if() 2. If you can't do if() 2, try if () 3, and so on. Instead, something like the below might work - though I haven't checked it.

Ideally you want to check the jumping code independent on whether you're pressing left or right.

 

if(this.cursor.right.isDown){
    this.player.body.velocity.x= 200;
    this.player.animations.play('correr', 5, true);
    this.player.scale.x=1;
} else if(this.cursor.left.isDown){
    this.player.body.velocity.x= -200;
    this.player.animations.play('correr', 5, true);
    this.player.scale.x=-1;
}
        
if(this.jump.isDown && this.player.body.wasTouching.down) {
    this.player.body.velocity.y= -400
} else {
    // Whatever you want to do if you're NOT jumping
}

 

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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