I have this class
class GameManager {
constructor() {
this._activeObjects = new THREE.Group();
}
get randomGeometry() {
const geometry = new THREE.BoxGeometry(0.1, 0.1, 0.1);
return geometry;
}
get randomMaterial() {
const material = new THREE.MeshBasicMaterial({
color: "red",
wireframe: true,
});
return material;
}
spawnCube(scene) {
const newCube = new THREE.Mesh(this.randomGeometry, this.randomMaterial);
this._activeObjects.add(newCube);
scene.add(newCube);
}
animateObjects() {
this._activeObjects.children.forEach((obj) => {
obj.position.z += 1
})
}
}
export const gameManger = new GameManager();
Once I actually attempt to render these cubes and animate them.
gameManger.spawnCube(scene);
(function animate() {
gameManger.animateObjects()
renderer.render(scene, camera);
requestAnimationFrame(animate);
})();
The cube renders just fine but the _activeObjects.children
only has the added cube within the scope of the spawnCube
method. If i try to access it from animateObjects
it’s empty.
Shouldn’t this retain reference to the same group?