Jump to content

Best way to give a different platforms different velocity.y values


phuurup
 Share

Recommended Posts

Hey, 

 

I'm trying to get the player to jump different heights based on what he's jumping on. 

 

I tried: Within the players 'move' function saying (in point form) if(touching down)vel.y = -350 else if (this touching special platform) el = -700, and the player only bounces -350. I also tried creating a function so when the player and platform overlap and collide, it'd call this new height, but that didn't work either.

 

Has anyone came across the same problem before, if so how did you solve it. Or would anyone know an example of how to solve this problem.

 

ps: I'm brushing up on my logic/Javascript so hopefully my n00b questions will soon disappear.

 

thanks for any help in advance. 

Link to comment
Share on other sites

I feel like I should incorporate the special jump with the player movement. Does anyone have any comments about that? 

 

 

playerMove: function() {

    if( this.cursor.left.isDown ) {
      this.player.body.velocity.x = -300;
    } else if( this.cursor.right.isDown ) {
      this.player.body.velocity.x = 300;
    } else {
      this.player.body.velocity.x = 0;
    }
 
    // handle player jumping
    if( this.player.body.touching.down ) {
      this.player.body.velocity.y = -350;
    } else if ( this.player.body.touching.specialPlatform) {
      this.hero.body.velocity.y = - 700;
    }
 
 
 
Or should i redesign the code so that playerMove just moves the player left and right, while the actual collision between player and platform determines the value of the y velocity? I'm also looking threw the Phaser.Physics.Arcade.Body API right now.
 
thoughts, ..anyone?
Link to comment
Share on other sites

I tried searching for a reason but the inspector says 'Uncaught TypeError: Cannot read property 'touching' of undefined', how should I work around or correct this?

 

 

 

   jumpFun: function() {

        if( this.physics.arcade.collide( this.hero, this.platforms )) {
        this.normalBounce();
        } else if (this.physics.arcade.collide( this.hero, this.special )) {
            this.specialBounce();
        }
},
    
    normalBounce: function() {
    if( this.hero.body.touching.down ) {
      this.hero.body.velocity.y = -350;
    } 
    
},
    
    
    
    specialBounce: function(){
    if( this.hero.body.touching.down ) {
      this.hero.body.velocity.y = -750;
    } 
}, 
Link to comment
Share on other sites

That error message makes it sound like your "this.hero" doesn't have a physics body -- that's weird, though, 'cuz I'm assuming you'd see a lot of other weird stuff and not be able to set its velocity. Did you call enable on "this.hero"? Are you sure you have something there that's a Sprite?

 

It sounds like colliding with a platform should assign the jump velocity to your player sprite, so I think you're on the right track there. You won't have to have special "touching" properties; "touching.down" will do it because you'll re-use it for both kinds of platforms.

Link to comment
Share on other sites

dthayes! thanks for all your help. I enabled the body and put the function that calls the jumping in the update function and it works! I was about to erase it all and start from scratch, thanks again!!

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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