detachAllChildren not working

Hey guys, so i have an issue. I have a method that drops spatials (in this case gold coins) when its called:

[java]
public void dropRandomItem(AssetManager assetManager, Vector3f location, int amount) {
/**
* Gold coins that drop on the ground.
*/
//if (random.nextInt(3) == 1) {
gold = assetManager.loadModel(“Models/Items/Gold/Cylinder.mesh.j3o”);
gold.setMaterial(assetManager.loadMaterial(“Models/Items/Gold/gold.j3m”));

    goldCoins = new Node();


    for (int i = 0; i <= amount; i++) {
        amountOfGold[i] = gold.clone();
        amountOfGold[i].rotate(300f, 20f, 300f);
        amountOfGold[i].setMaterial(assetManager.loadMaterial("Models/Items/Gold/gold.j3m"));
        amountOfGold[i].setLocalScale(0.25f, 0.25f, 0.25f);
        amountOfGold[i].setName("Gold");
        goldCoins.attachChild(amountOfGold[i]);
        for (float r = 0; r < 0.25; r += 0.01) {
            amountOfGold[i].setLocalTranslation(location.add(new Vector3f(random.nextInt(4), r, random.nextInt(4))));
        }
    }
    game.getApp().getRootNode().attachChild(goldCoins);

    GeometryBatchFactory.optimize(goldCoins, true);
    goldAmount = random.nextInt(4) + 1;

[/java]
So this will scatter gold pieces in a small location.

Now i have a removeItem method:
[java]
public void removeItem() {
goldCoins.detachAllChildren();
}//end of removeItem method
[/java]

So in my onAnimCycleDone method i call dropRandomItem then after a certain time has passed i call remove item. But it doesn’t work, the gold pieces still stay there. Am i missing something?

detachAllChildren() is certainly working. Your analysis of what is happening must be incorrect. If it seems not to be working then it’s either not called at all or called on the wrong node.

Put in some simple debugging and/or step through the debugger to see what is really happening.

I fixed it, should really learn how to use the debugger for netbeans :stuck_out_tongue:

Actually, it does and doesn’t work. See, what happens is… you detachAllChildren… and then one of them losses their job and moves back in. Then the others start thinking you love that child more than them, so they ask to move back in. Next thing ya know, detachAllChildren completely failed.

@t0neg0d said: Actually, it does and doesn't work. See, what happens is... you detachAllChildren... and then one of them losses their job and moves back in. Then the others start thinking you love that child more than them, so they ask to move back in. Next thing ya know, detachAllChildren completely failed.

Heheh. Except in JME’s case, it also gives the children amnesia. They don’t even remember who their parents were to begin with. Poor little orphans.

1 Like