Sorry, but this makes absolutely no sense to me. It definitely shows 2 spatials but in the end the loop only does 1 loop? It seems it skips always the last entry of the ArrayList :?
The problem is that scene.attachChild indirectly manipulates you "allspats" array. (I have no code in front of me, its like sth with child.removeFromParent() or so).
So your arrayList where you are iterating in your for loop is modified (the first spatial is removed). I am wondering why it wont throw a ConcurrentModificationException or so…
The problem can be solved (i think):
ArrayList<Spatial> allspats = new ArrayList<Spatial>();
allspats.addAll(loaded.getChildren());
The problem is that scene.attachChild indirectly manipulates you "allspats" array. (I have no code in front of me, its like sth with child.removeFromParent() or so).
And yeah, that's exactly what's going on.
Here's owaye's code:
In order to see what's wrong with this, you must rememver that every part of the scenegraph can always have only one parent.
So scene.attachChild(x) removes the child from allspats in order to attach it to an other Node.
That's why allspats.size() gets smaller on each iteration of the for loop.
If that's still hard to grasp, consider that the foreach list iteration shown above essentially boils down to something like this:
int i = 0;
while(i < allspats.size())
{
Spatial x = allspats.get(i);
i++;
}
Now it should become obvious that modifying allspats.size() in the loop will change the number of iterations the loop will do.