Goshawk Posted July 22, 2018 Share Posted July 22, 2018 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 More sharing options...
kurhlaa Posted July 22, 2018 Share Posted July 22, 2018 Hi, your function returns a variable, a collider's object. So later if you want to remove it: colliderObject.destroy(); theNeedle 1 Link to comment Share on other sites More sharing options...
Goshawk Posted July 22, 2018 Author Share Posted July 22, 2018 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. theNeedle 1 Link to comment Share on other sites More sharing options...
Goshawk Posted July 22, 2018 Author Share Posted July 22, 2018 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(); theNeedle 1 Link to comment Share on other sites More sharing options...
Goshawk Posted July 22, 2018 Author Share Posted July 22, 2018 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 More sharing options...
samme Posted July 23, 2018 Share Posted July 23, 2018 Probably easier to just do var platformCollider = this.physics.add.collider(/*…*/); // … this.physics.world.removeCollider(platformCollider); theNeedle 1 Link to comment Share on other sites More sharing options...
Goshawk Posted July 23, 2018 Author Share Posted July 23, 2018 Oh, yes. I'm probably overthinking this! Although I'm a little worried about the proliferation of variables in my game. I'm new to both JS and Phaser 3 so I've been prodding a lot as I learn. Link to comment Share on other sites More sharing options...
robatt Posted March 29, 2020 Share Posted March 29, 2020 what if I had a collider created on a sprite group, and wanted to disable collision only for the sprite that collided? Link to comment Share on other sites More sharing options...
tkohey_sad Posted April 5, 2020 Share Posted April 5, 2020 var platformCollider = this.physics.add.collider(object,object); this.physics.world.removeCollider(platformCollider); Link to comment Share on other sites More sharing options...
Recommended Posts