Jump to content

[Tutorial] (solved) Problem with jump following tutorial


kapu
 Share

Recommended Posts

Hi there, my first post!

I'm learning how to use Phaser by following a cool tutorial by Wild . At a moment he explains us how to move our player by using keyboard input. And everything works fine but the jump mouvement =/ And I don't understand why! (my browser console says nothing). Nothing happens when I press the up button.

Here is my code if someone is willing to explain me a bit what's wrong?

 

in the create

player.animations.add('idle',[0,1],1,true);
    player.animations.add('jump',[2],1,true);
    player.animations.add('run',[3,4,5,6,7,8],7,true);
    this.physics.arcade.enable(player);
    this.camera.follow(player);
    player.body.collideWorldBounds = true;
  controls = {
      right: this.input.keyboard.addKey(Phaser.Keyboard.RIGHT),
      left: this.input.keyboard.addKey(Phaser.Keyboard.LEFT),
      up: this.input.keyboard.addKey(Phaser.Keyboard.UP),
    };

 

And in the update function:

this.physics.arcade.collide(player,layer);

player.body.velocity.x=0;


if (controls.right.isDown){
  player.animations.play('run');
  player.scale.setTo(1,1);
  player.body.velocity.x += playerSpeed;
}

if (controls.left.isDown){
  player.animations.play('run');
  player.scale.setTo(-1,1);
  player.body.velocity.x -= playerSpeed;
}

if (controls.up.isDown && (player.body.onFloor() || player.body.touching.down) && this.time.now > jumpTimer){

player.body.velocity.y = -600;
jumpTimer = this.time.now + 750;
player.animations.play('jump');


}

if (player.body.velocity.x==0 && player.body.velocity.y==0){
  player.animations.play('idle');
}

  }

I don't know if any other information would be useful?

 

Link to comment
Share on other sites

The code looks fine @kapu, what I would suggest to do is this to convert what you have in something like this:

let upPressed = controls.up.isDown;
let onFloor = player.body.onFloor();
let touchingBottom = player.body.touching.down;
let timerReady = this.time.now > jumpTimer;

if (upPressed && (onFloor || touchingBottom) && timerReady){
  player.body.velocity.y = -600;
  jumpTimer = this.time.now + 750;
  player.animations.play('jump');
}

After that, bring up the Chrome Developer, add a breakpoint on the first line (`let upPressed...`), press 'UP' and then step through your function to see the value of each variable to see what's not working properly.

Report back after you've done that :) 

Link to comment
Share on other sites

Hi @Nesh108 and thank you!

I commented my ancien code and put what you suggest instead. But it doesn' work either =/ I'm a newbie and I don't know how to manage the debug console and the game: if I set a breakpoin I can't play, the sprite doesn't move at all.

Here is a screenchot of what I get: 

CXNADbAHPVZC.png

 

If i go step by step through the code:

upPressed becomes false and the same goes for onFloor, touchingBottom. And timerReady becomes true.

If I switch off the breakpoint and keep the 'spy variable' (I don't know how to say it in English^^) at upPressed, I can play the game again but nothing happens if I press the up button (and the same goes if I set an other button to jump) =(.

Link to comment
Share on other sites

Then the problem is upPressed.

Try to map something else to that key:

 controls = {
            right : this.input.keyboard.addKey(Phaser.Keyboard.RIGHT),
            left : this.input.keyboard.addKey(Phaser.Keyboard.LEFT),
            up : this.input.keyboard.addKey(Phaser.Keyboard.W),       
        };

In this case 'W' and check if that changes anything.

Another problem is the player apparently not touching the floor/bottom. But first let's fix the issue with the key.

Btw, @kapu once you set the breakpoints, you have to press the play button (>) to step through each line of code, otherwise it will just get stuck there :D 

Also, could you share the code you have? Maybe it's a silly problem somewhere unexpected :) 

Link to comment
Share on other sites

The W letter doesn't change a thing =/ And yes I clicked on the play button several times but it stays in the update loop =/ And nothing happens.

 

And thank you so much to take that time for me! I uploaded the files on a gitlab instance: https://framagit.org/kapu/jeu_JS  (I don't know how you usualy do it on this forum?). The code I was showing you is the Level1.js in the js folder.

 

Thank you again @Nesh108 !

Link to comment
Share on other sites

  • kapu changed the title to [Tutorial] (solved) Problem with jump following tutorial
 Share

  • Recently Browsing   0 members

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