Jump to content

Make sprite in platformer fall off screen


druphoria
 Share

Recommended Posts

I'm creating a platformer right now and I want to trigger a death function each time the player falls into a hole. Ideally the player will also fall off screen. It seems that if I have collideWorldBounds equal to true, then it automatically makes the player collide with all four sides of the world without a way to specify that I only want this to occur for the left and right sides. I came up with a hacky solution which is to overwrite the function checkWorldBounds in Phaser's Body class and remove the final clause the if-clause that checks for collision with the bottom of the world. I'm wondering if there is a more straightforward way to do this within the framework that I am missing.

Link to comment
Share on other sites

Actually this solution is working out quite nicely. Here, I overwrite the method to not check for lower bound:

// Allow the sprite to fall off screenfunction disableLowerWorldBoundsCheck(sprite) {	    sprite.body.checkWorldBounds = function () {            if (this.position.x < this.game.physics.arcade.bounds.x && this.game.physics.arcade.checkCollision.left)            {                this.position.x = this.game.physics.arcade.bounds.x;                this.velocity.x *= -this.bounce.x;                this.blocked.left = true;            }            else if (this.right > this.game.physics.arcade.bounds.right && this.game.physics.arcade.checkCollision.right)            {                this.position.x = this.game.physics.arcade.bounds.right - this.width;                this.velocity.x *= -this.bounce.x;                this.blocked.right = true;            }            if (this.position.y < this.game.physics.arcade.bounds.y && this.game.physics.arcade.checkCollision.up)            {                this.position.y = this.game.physics.arcade.bounds.y;                this.velocity.y *= -this.bounce.y;                this.blocked.up = true;            }        };}

Here I attach the kill function:

this.sprite.body.checkWorldBounds = true;this.sprite.events.onOutOfBounds.add(this.killPlayer, this);
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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