Doug

Destroying a matter.js rectangle body

Recommended Posts

Hi all

I'm creating a matter.js rectangle body with the following code:

let wireRect = this.matter.add.rectangle(w.x, w.y, this.wireLength, 10, { 
            restitution: 0.9,
            isSensor: true,
            angle: angle
        });

I then later need to remove it from the game.

wireRect.destroy(); returns an error "wireRect.destroy is not a function".  I have another game in which I am using this.matter.add.image(XXX) and the .destroy() method works fine on those.

So I tried:

this.matter.world.remove(this.matter.world, wireRect);

That doesn't seem to work either.

I can see matter.body documentation https://photonstorm.github.io/phaser3-docs/MatterJS.Body.html, but can't see how I would find the correct destroy method for it.

Any ideas welcome please! 

Thanks :)

Share this post


Link to post
Share on other sites

That is correct, it should remove the physics body. It seems you also have to remove the image though with wireRect.visible = false; for example.

Share this post


Link to post
Share on other sites

Thanks very much.  However, the objects still accept collisions etc and also still show up in debug mode.  Have you had this working successfully?

Just out of interest, how did you get the "Phazer patron" badge to display (I am a patron, but it's not showing for me)?

Share this post


Link to post
Share on other sites

You are a legend, thanks.  I can replicate this working in the example page you sent too :)  Will have a go at implementing in my code as soon as I have a chance.

Thanks very much!  Greatly appreciated.

Share this post


Link to post
Share on other sites

Just to follow up on this, I can confirm that @rgk was right.  I changed the code to the following and it all works.  

this.matter.world.remove(this.wireRect);

Thanks again :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.