Jump to content
This forum will be closing down. Please move to the respective dedicated project forums.

Question about touching.down


Recommended Posts

Hello guys,


I recently started playing around with Phaser and I'm loving it so much. I have a question about touching.down and how it actually works. I assume one object has to 'touch' another when you define a collision. But I'm having a trouble getting this to work.


I have this code below, and it doesn't seem to work at all. Console logging 'p.body.touching.down' returns false. I have my player sprite loaded on top of a platform. I have their collision set in the update function like most of the tutorials and examples suggest (  game.physics.collide(p, platforms); ). Practically, I've been following examples and figuring it out by myself, but this stucked me a bit.

  if (cursors.up.isDown && p.body.touching.down) {    console.log("JUMP");    p.body.velocity.y = -100;  }

I can make the 'jump' if I remove && p.body.touching.down, but I can't limit it.


P.S. Is there a different approach to getting the player to jump?


Thank you!

Link to comment
Share on other sites

Here's the full code:

var game = new Phaser.Game(800, 600, Phaser.CANVAS, '', {preload: preload, create: create, update: update});var p; var cursors;var platforms; function preload() {  game.load.image('player','images/player.png');  game.load.image('tile', 'images/tile.png');}function create () {    p = game.add.sprite(0, 500, 'player');    p.body.collideWorldBounds = true;  platforms = game.add.group();  var ground = platforms.create(0, game.world.height - 50, 'tile');  ground.body.immovable = true;  cursors = game.input.keyboard.createCursorKeys();}function update ()   game.physics.collide(p, platforms);  p.body.velocity.x = 0;  if (cursors.left.isDown) {    console.log("GO LEFT");    p.body.velocity.x = -100;  }  else if (cursors.right.isDown) {    console.log("GO RIGHT");    p.body.velocity.x = 100;  }  // jump  if (cursors.up.isDown && p.body.touching.down) {    console.log("JUMP");    p.body.velocity.y = -100;  }}
Link to comment
Share on other sites

Hi Zrt,


here are the modifications I had to make to get the jumping to work:


a ) The script wouldn't run at all, because the update() functions is missing it's opening "{"


b ) you have not given the player any gravity, so he is not falling down, so he never touches the ground


Fix:   p.body.gravity.y = 7; ( in the create functions, after p has been initialized)


Now the player falls to the ground, the touch variable is true, and presto, you can jump :)




Link to comment
Share on other sites

Thank you, jpdev, will try this! Btw, the bracket in the update() function is fine, it seemed that I accidentally removed it in the post above while I was deleting the comments I have in my actual code. :)


So, as I understand the player needs gravity so he can touch something. Seems logical, thank you. :)


EDIT: It works! Seems I was putting the gravity in the wrong function. :unsure: Follow up question, does the player object always needs ground that binds to the gravity? Because I just noticed that I can only jump only if I stand on something. Is there a way to make a jump without standing on ground, or platform or whatever? Thank you.

Link to comment
Share on other sites

As you wrote in your first post, if you remove the touching-check from the jump code, you can jump when ever.


So touching down is not required for jumping.


It's up to you, how you want to limit the ability to jump.


Take for example the mario games. Normally mario is allwoed to jump when he touches the ground or a platform.

Only if you have some special like the cape or a propeller-head or something like that, than you are allowed to jump again while in the air. (often limited to one time while in the air.).


To do something like that, you would check touching to reset a jump counter, and when jumping check against that counter. (just an example.)


If you tell us what jumping-style you are trying to implement we can probably figure something out here  :)

Link to comment
Share on other sites


  • Recently Browsing   0 members

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