Reputation: 31
I'm working with matter.js on a small project, trying to add and remove objects from a matterjs world with js functions.
the add function seems to work, the remove method only works inside the adding function-
var boxes = [];
function addCircle(Cid, Ccolor, Cradius) {
boxes[Cid] = Bodies.circle((w/2), (h/2), Cradius, {
density: 0.0005,
frictionAir: 0.06,
restitution: 0.3,
friction: 0.01,
render: { fillStyle: Ccolor, strokeStyle: 'rgba(0,0,0,0)',
lineWidth: 0,
}
});
boxes[Cid].angle = Math.random() * 0.5;
boxes[Cid].force.y -= 0.0001;
World.add(engine.world, boxes[Cid]);
//World.remove(engine.world, boxes[Cid]); <-- This one works
}
function removeCircle(Cid) {
//console.log(boxes[Cid]);
World.remove(engine.world, boxes[Cid]); // <-- This one doesn't
}
console shows error "Cannot read property 'type' of undefined" for the remove function. Can someone tell me how to solve this? Any help and suggestions will be really appreciable.
Upvotes: 3
Views: 1904
Reputation: 96909
To remove a body from a world you need to use Composite.remove(...)
.
So in your case it'll be:
Composite.remove(engine.world, boxes[Cid]);
Upvotes: 2