Here is my code, and I'm curious if the delete function effectively destroys an object without causing any memory leaks:
class Projectile extends MovableObject{
constructor(){
super();
this.speed = 2;
this.geometry = new THREE.CircleGeometry(0.5);
this.material = new THREE.MeshBasicMaterial({ color: 0xffffff });
this.mesh = new THREE.Mesh(this.geometry, this.material);
}
setRotation(rotation){
this.mesh.rotation.set(rotation.x, rotation.y, rotation.z);
}
setPosition(position){
this.mesh.position.set(position.x, position.y, position.z);
}
}
Later on, I have a function called handleProjectiles where I manage instances of Projectile:
var position;
listProjectiles.forEach(function(projectile, i, list){
position = projectile.getMesh().getWorldPosition();
if(/*need to destroy the projectile*/){
list.splice(i, 1);
scene.remove(projectile.getMesh()); // This is ThreeJS
delete projectile;
console.log("Projectile has been destroyed");
}
});