Player is not colliding with ground

Any ideas why my player isn’t colliding with the ground? Everything was working fine until I moved my player into its own class in it’s own file so that I could utilize modules. The player shows up and movement works, but it isn’t colliding with my ground. I’ll just post the sections of code we are dealing with:

    this.platforms = this.physics.add.staticGroup();
   
    this.ground = this.platforms.create(960, 1050, "grassGround").setScale(1.2).refreshBody();
    this.player = new Player(this, 400, 200);

    this.physics.add.collider(this.player, this.platforms);

I was able to get collision to work between my player and my platforms by passing a new argument for collision detection in my Player class. This just allowed me to see the real problem. When I assign this.player to new Player() within my levels I can’t actually manipulate this.player within that scene. Any thoughts?

export default class Player {
constructor(scene, x, y, collision) {
    this.scene = scene;
    
    this.player = scene.physics.add.image(x, y, "player");
    this.player.setCollideWorldBounds(true);
    scene.physics.add.collider(this.player, collision);
}

Hey Ben,
It looks like your player class does not extend Phaser.Physics.Arcade.Sprite or similar. This means your Player.js class is not a phyics object. You can fix this in one of two ways:

Way 1: Extend Phaser.Physics.Arcade.Sprite in your Player.js class and remove your player property. You’ll then be able to reference your player with the this keyword.

Way 2: Change this.physics.add.collider(this.player, this.platforms); in your main function to this.physics.add.collider(this.player.player, this.platforms); This will make your Player.js class into what’s called a “Wrapper”.

1 Like

Worked perfectly with option 2! I’ve even implemented a Special class for my debuffs and power-ups that randomly spawn and it’s also working and colliding as expected. This has been a huge help. Thanks