我正在一个小项目上使用matter.js,尝试使用js函数添加和删除来自物品世界的对象。
添加函数似乎工作,删除方法只适用于添加功能 -
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
}
控制台显示错误“无法读取属性'类型'未定义”的删除功能。谁能告诉我如何解决这个问题?任何帮助和建议都会非常明显。
要从世界中移除身体,您需要使用Composite.remove(...)
。
所以在你的情况下它将是:
Composite.remove(engine.world, boxes[Cid]);