在Bullet对象被创建时,为其添加一个构造函数,在其中让Bullet对象在销毁时自动调用一个函数进行删除。示例代码如下:
class Bullet {
constructor(position) {
this.position = position;
this.element = document.createElement('div');
this.element.classList.add('bullet');
document.body.appendChild(this.element);
}
destroy() {
this.element.remove();
}
}
// 创建Bullet对象
const bullet = new Bullet({ x: 100, y: 50 });
// 执行子弹的销毁,可以在适当的时候调用
bullet.destroy();
上一篇:Bulletsharp输出?