Jump to content

How do you remove colliders/overlap?


Goshawk
 Share

Recommended Posts

If I have:

this.physics.add.collider(this.hero, this.platforms, this.collidesWithPlatform, false, this);

but later in the game I no longer want the collisions (that is, the hero now falls through the platforms), how do I remove the collider?

Link to comment
Share on other sites

Thanks. I found another way which avoids the need of the variable.

this.physics.world.colliders

...returns a process queue (`Phaser.Structs.ProcessQueue.<Phaser.Physics.Arcade.Collider>`). Calling:

this.physics.world.colliders.destroy();

...destroys the colliders in that queue.

Link to comment
Share on other sites

In fact, since the factory returns the object, I can set a name on it when I create it:

this.physics.add.collider(this.hero, this.platforms, this.collidedWithPlatform, false, this).name = 'platform_collider';

Then, if I want to destroy that particular collider only, I can do:

this.physics.world.colliders.getActive().find(function(i){
    return i.name == 'platform_collider'
}).destroy();

 

Link to comment
Share on other sites

Though, I'm not sure it works like I think it does.

If I scale the hero sprite directly afterwards:

this.physics.world.colliders.getActive().find(function(i){
    return i.name == 'platform_collider'
}).destroy();

this.hero.scaleX = 2.0;

...I get an error:

project.bundle.js:104510 Uncaught TypeError: Cannot read property 'destroy' of undefined

I have no idea why at the moment. Any help in understanding this would be welcome.

Edit:

This mouthful works:

this.physics.world.colliders.remove(this.physics.world.colliders.getActive().find(function(i){
            return i.name == 'platform_collider'
        }));

this.hero.scaleX = 2.0;

whilst the functional `.destroy()` version doesn't. Why does `this.hero.scaleX = 2.0;` break it?

Link to comment
Share on other sites

  • 1 year later...
 Share

  • Recently Browsing   0 members

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