Having this particular class declaration
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();
Upon attempting to render and animate these cubes.
gameManger.spawnCube(scene);
(function animate() {
gameManger.animateObjects()
renderer.render(scene, camera);
requestAnimationFrame(animate);
})();
While the cube renders correctly, the issue arises with the _activeObjects.children
array only containing the added cube within the spawnCube
method. Accessing it from the animateObjects
method shows it as empty.
Isn't this supposed to maintain reference to the same group?