Jump to content

Strange Collision Behaviour


m4d3
 Share

Recommended Posts

Hi,

 

i was playing around with Phaser, making a little Platformer style game, but i cant get the ground collision right.

Either its working like intended but the animation doesnt trigger or my sprite falls through the ground and other objects. Here is what my class looks like:

 

 

class Player extends Phaser.Sprite {    game: Phaser.Game;    isJumping: bool;    constructor(game: Phaser.Game) {        super(game)        this.game = game;        //this = Game.createSprite(Game.stage.width * .5 - 50, 200, "player");        this.x = game.stage.width * .5 - 50;        this.y = game.stage.height - 150;        this.loadGraphic("player");        this.acceleration.y = 980;        this.drag.x = 900;        //player.drag.y = 500;        this.maxVelocity.x = 250;        this.maxVelocity.y = 250;        this.animations.add('run');        this.animations.play('run', 10, true);        this.isJumping = true;    }    public update() {        var keyboard: Phaser.Keyboard = this.game.input.keyboard;        // Jumping YAY!        if (keyboard.isDown(Phaser.Keyboard.UP) && !this.isJumping && this.velocity.y === 0) {            this.velocity.y = -300;            this.acceleration.y -= 300;            this.isJumping = true;        }        // Check for Ground Contact        if (this.isTouching(Phaser.Collision.DOWN)) {            // Stop Forces in Y            this.acceleration.y = 0;            this.velocity.y = 0;            this.isJumping = false;            if (Math.abs(this.velocity.x) != 0) {                this.animations.play('run');            } else {                this.animations.stop("run");            }        } else {            // if Jumping, apply Gravity            this.acceleration.y += 50;            this.animations.stop("run");        }    } }

 

Could someone tell me whats wrong?

In my main update() routine i check for collision:


function update() {Game.collide(playerGroup, collisionGroup, collides);}

I tried a lot of different aproaches but none did work like intended. Help would be really apprecheated!

Link to comment
Share on other sites

Hello, which version of Phaser are you using?

 

I've found that in some versions, calling Game.collide with groups leads to unexpected results. In version 0.95 I do something like this in the main update function to get collisions to work:

 

//Typescriptpublic update() {            this.map.collide();            for (var i = 0; i < this.level.objectList.length; i++) {                if (this.level.objectList[i]) {                    this.game.collide(this.player, this.level.objectList[i], this.resolveCollision.bind(this));                    for (var j = 0; j < this.level.objectList.length; j++) {                        if (i != j) {                            if (this.level.objectList[j]) {                                this.game.collide(this.level.objectList[i], this.level.objectList[j]);                            }                        }                    }                }            }        }

Chances are you probably don't need to do this in the latest version of Phaser though, I haven't tried it yet.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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