Jump to content

Sprite jump if colliding with map


Roki
 Share

Recommended Posts

I'm building my first game and would love my sprite to jump. Right now he only flies. I know I need to set it so that the up arrow only activates when I'm touching a collision point, but it doesn't seem to be working. Every time I try a variation of the code, it just disables all jumping. Here's my controls: (The first "if (cursors.up.isDown)" is commented out to disable the "flying" I currently have.)

sprite.body.velocity.x = 0;// if (cursors.up.isDown)// {//     sprite.body.velocity.y = -150;// }if (cursors.down.isDown){    sprite.body.velocity.y = 150;}else if (cursors.left.isDown){    sprite.body.velocity.x = -150;    sprite.animations.play('left');}else if (cursors.right.isDown){    sprite.body.velocity.x = 150;    sprite.animations.play('right');}else{    // Stand still    sprite.animations.stop();    sprite.frame = 17;}if (cursors.up.isDown && sprite.body.touching.down) {    sprite.body.velocity.y = -320; }

With this code, I can't seem to get any upwards movement. It's not an error, just, nothing happens. Any idea how to fix it?

P.S. if you also know how to enable double jumping, that would be my next step and I would be super grateful for that code as well.

Link to comment
Share on other sites

I suspect you have no map nor anything else in your game yet, because touching works only when the body touches another body or if it touches a map's tile. If there is nothing is your scene, you need to use blocked, which will check if your objects is colliding with the world's bounds.

 

Doc for blocked : http://phaser.io/docs/2.4.4/Phaser.Physics.Arcade.Body.html#blocked

Doc for touching : http://phaser.io/docs/2.4.4/Phaser.Physics.Arcade.Body.html#touching

 

Even better could be to use the onFloor() method, which seems to check maps and world bounds.

Doc : http://phaser.io/docs/2.4.4/Phaser.Physics.Arcade.Body.html#onFloor

Link to comment
Share on other sites

I dont exactly get what you mean since we don't have your whole code, but that shouldn't be too difficult, i dont know which physics you are using, but in the case of Arcade physics, enabling body on your elements and setting gravity should be enough to stop it from flying away. Or is it that your sprite is supposed to fly and jump?

Link to comment
Share on other sites

One subtlety about onFloor: if you have other sprites that your sprite can stand on, onFloor will return false when standing on those sprites.

 

I do this in sprites where I care about standing:

  Object.defineProperty(this, 'standing', {    get: function() {      return this.body.blocked.down || this.body.touching.down;    }  });
Link to comment
Share on other sites

Yeah okay I couldn't edit my code because it was being reviewed by a moderator, but here's what's going on:

 

My entire game is 100% functional except getting the up arrow to only activate when I'm colliding with a layer. I suspect it has to do with the way I'm rendering my map because I had to use a CSV file instead of JSON. I have world bounds on and am using this code to make sure my sprite doesn't pass through layers:

this.game.physics.arcade.collide(sprite, layer);

I am also using this line to make sure my map is collidable:

map.setCollisionByExclusion([], true);

Is there any other code you would need to see to help out? To my knowledge, my op and these snippets should be the only relevant code.

 

I can move my sprite upwards, but I can keep moving it up until I hit the top of my map.

To see it in action (now that I finally have it online): http://supplyrun.heroku.com/

Link to comment
Share on other sites

Oh, hey, actually, you've got this code in moveSprite:

if (cursors.up.isDown)     {     sprite.body.velocity.y = -150;     }

Right at the top. Intentional?

Yeah, but because there's no limit on that code, it gives the effect of flying through the air. I have gravity, so the sprite falls when you let go of the key, but if you hold it down, the sprite never falls.That's what I'm hoping to counter. But there's something about "sprite.body.touching.down" that my game really doesn't like.

Link to comment
Share on other sites

¯\_(ツ)_/¯

 

That code will make your sprite fly when you press up if you don't check that the sprite is also touching/blocked down. When you say "something about sprite.body.touching.down that my game doesn't like" what do you mean? Errors, or no effect?

 

Cuz if the sprite doesn't fall through the floor then they are either touching or blocked, so maybe there's something else we should be looking at?

Link to comment
Share on other sites

¯\_(ツ)_/¯

 

That code will make your sprite fly when you press up if you don't check that the sprite is also touching/blocked down. When you say "something about sprite.body.touching.down that my game doesn't like" what do you mean? Errors, or no effect?

 

Cuz if the sprite doesn't fall through the floor then they are either touching or blocked, so maybe there's something else we should be looking at?

There's absolutely no effect. :/ I don't get an error, but with 

if (cursors.up.isDown && sprite.body.touching.down) {    sprite.body.velocity.y = -320; }

my sprite won't go up at all, no matter where it is on the game.

Link to comment
Share on other sites

I have had similar issues in a couple of my games have you tried your code like this?

 

 

In the update method, add:

 
 
 
 
if(this.cursors.up.isDown) {
 
    this.playerJump();
 
}
 
Then in the game state add:
 

 
playerJump: function() {
 
    if(this.player.body.blocked.down) {
 
      this.player.body.velocity.y -= 700;
 
    }   
 
  },

 

This is checking if your players body blocked down, and if so allows you to use the playerJump function. If you are in the air and therefore the body is down blocked down then the function will not work. I hope this helps, let me know!

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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