Thursday, September 13, 2012

Removing Multiple Objects from a Physijs Scene

‹prev | My Chain | next›

I would like to be able to restart my Three.js / Physijs fruit jumping game. This seems as simple as cycling through all the scene's children and removing any fruit:
function reset() {
  scene.children.forEach(function(object) {
    if (object.is_fruit) {
      scene.remove(object);
    }
  });
}
I will leave the player and the ground as-is, though I will reset the player back to its starting position. Only resetting does not seem to work. Fruit seems to hang around and, if I check scene.children, it does not seem to correspond to what is actually in the scene:


There should at least be 5 objects in the scene: the camera, the ground, the player, and two fruit. But the scene is only reporting three. Strange.

In the end, I can come up with no other way to do this than to reach under the Physijs covers:
  for (var i in scene._objects) {
    if (scene._objects[i].is_fruit) {
     scene.remove(scene._objects[i]);
    }
  }
That is not all that satisfying a solution, but it works.

Day #508

No comments:

Post a Comment