Jump to content

Jumping problem


Jeffrey
 Share

Recommended Posts

Hi!
 
I have been doing a Phaser tutorial and I couldn't get my player to jump. 

I tried using:

if (this.cursor.up.isDown && this.player.body.touching.down) {this.player.body.velocity.y = -320; }

When I press the up arrow key nothing happens. When I get rid of the "&& this.player.body.touching.down" part I can jump, but only when I'm not standing on anything. Same result with  "&& !this.player.body.touching.down" 

 

I have spent a lot of time trying to figure out what is wrong but can't find it, tried to redo the tutorial from the start and got the same result =/

 

What am I doing wrong?

 

(Full code below)

var mainState = {preload: function () {game.load.image("player", "assets/player.png");game.load.image("wallV", "assets/wallVertical.png");game.load.image("wallH", "assets/wallHorizontal.png");},create: function () {game.stage.backgroundColor = "#3498db";game.physics.startSystem(Phaser.Physics.ARCADE);this.player = game.add.sprite(game.world.centerX, game.world.centerY, "player");this.player.anchor.setTo(0.5, 0.5);game.physics.enable(this.player);this.player.body.gravity.y = 500;this.cursor = game.input.keyboard.createCursorKeys();this.createWorld();},update: function () {this.movePlayer();game.physics.arcade.collide(this.player, this.walls);},movePlayer: function () {if (this.cursor.left.isDown) {this.player.body.velocity.x = -200;} else if (this.cursor.right.isDown) {this.player.body.velocity.x = 200;} else {this.player.body.velocity.x = 0;}// THIS PART DOES NOT WORK ----------->if (this.cursor.up.isDown && this.player.body.touching.down) {this.player.body.velocity.y = -320; }},createWorld: function() {// Create our wall group with Arcade physicsthis.walls = game.add.group();this.walls.enableBody = true;// Create the 10 wallsgame.add.sprite(0, 0, 'wallV', 0, this.walls); // Leftgame.add.sprite(480, 0, 'wallV', 0, this.walls); // Rightgame.add.sprite(0, 0, 'wallH', 0, this.walls); // Top leftgame.add.sprite(300, 0, 'wallH', 0, this.walls); // Top rightgame.add.sprite(0, 320, 'wallH', 0, this.walls); // Bottom leftgame.add.sprite(300, 320, 'wallH', 0, this.walls); // Bottom rightgame.add.sprite(-100, 160, 'wallH', 0, this.walls); // Middle leftgame.add.sprite(400, 160, 'wallH', 0, this.walls); // Middle rightvar middleTop = game.add.sprite(100, 80, 'wallH', 0, this.walls);middleTop.scale.setTo(1.5, 1);var middleBottom = game.add.sprite(100, 240, 'wallH', 0, this.walls);middleBottom.scale.setTo(1.5, 1);// Set all the walls to be immovablethis.walls.setAll('body.immovable', true);},// REST OF THE FUNCTIONS};var game = new Phaser.Game(500, 340, Phaser.AUTO, "gameDiv");game.state.add("main", mainState);game.state.start("main");

 

Link to comment
Share on other sites

In your update function, you have to do the collide call first otherwise touching.down will always be false. The body.touching directions are set to false at the start of the frame, and calling collide will set the relevant directions to true for the rest of that frame, so the order of these is important.

game.physics.arcade.collide(this.player, this.walls);this.movePlayer();
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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