Jump to content

Jump on top of sprite


Göran
 Share

Recommended Posts

Hey,

 

I am adding boxes, from the object layer of my tilemap, as sprites, to the game. I then add them to a group and loop through each of them.

    this.boxes.forEach(function(sprite){      if(sprite.body.touching.left) {        console.log("touching left");        sprite.body.velocity.x = 0;        this.player.scale.x = -1;      }      if(sprite.body.touching.right) {        console.log("touching right");        sprite.body.velocity.x = 0;        this.player.scale.x = 1;      }      if(sprite.body.touching.up) {        console.log("touching up");        this.onTopOfBox = true;      }    }, this);

My problem is, that I can't seem to be able to jump, while I am on top of a box. My movement code looks like this:

movePlayer: function() {    this.player.body.gravity.y = 450;    this.player.body.velocity.x = 0;    if (this.cursors.left.isDown)    {        this.player.body.velocity.x = -150;        /*if (this.facing != 'left')        {            this.player.animations.play('left');            this.facing = 'left';        }*/    }    else if (this.cursors.right.isDown)    {        this.player.body.velocity.x = 150;        /*if (this.facing != 'right')        {            this.player.animations.play('right');            this.facing = 'right';        }        */    }    else    {        /*if (this.facing != 'idle')        {            this.player.animations.stop();            if (this.facing == 'left')            {                this.player.frame = 0;            }            else            {                this.player.frame = 5;            }            this.facing = 'idle';        }        */    }    if (this.jumpButton.isDown && this.game.time.now > this.jumpTimer && this.tile !== 45)    {      if(this.player.body.onFloor() || this.onTopOfBox) {        console.log(this.onTopOfBox);        this.playerJump.play();        this.player.body.velocity.y = -250;        this.jumpTimer = this.game.time.now + 750;      }    }        this.onTopOfBox = false // FIXED IT  }

As you can see, I am checking if either the player is on the floor, or on top of the box. For some reason, this.onTopOfBox always returns true, if I assign the value in the previously mentioned loop. Anybody got ideas? Do I need to show more code? Basically what happens is, that I can jump all the time, after I jump from a box.

 

Any help is greatly appreciated (also critique on my code :P)

 

Edit: Setting this.onTopOfBox to false after the check made it work.

Edited by Göran
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

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