Problems with removing controls and lots of things being removed at once when they shouldn’t be

I am having problems with some of my code and removing controls AND nodes; here is the code (pastebinned for convenience):



The Control class: http://pastebin.com/4jnzP8cq



The class where I make all of the ‘traps’, and attach them to the node (the rootNode in this case): http://pastebin.com/iWiSmMFt



and the code which I call in my main class to create the traps (which is called from simpleInitApp() ): [java]public void initTraps() {

Material trapmat = new Material(assetManager, “Common/MatDefs/Misc/Unshaded.j3md”);

trapmat.setTexture(“ColorMap”, assetManager.loadTexture(“Textures/Terrain/BrickWall/BrickWall.jpg”));

trapfactory = new TrapFactory(traplist, trapmat, bulletAppState, TrapNodeName, TrapName, PlayerNodeName, rootNode);

trapfactory.parseFile(“map.txt”);

trapfactory.attachMapToNode(rootNode);

traplist = trapfactory.gettraplist();

}[/java]



I call trapfactory.checkForUsedTraps(); in my main class’ update loop.





My issue is in TrapControl’s function(); calling that method, every ‘trap’ and its hitbox gets removed. If I comment out the third line in function, all of the Geometries are still floating in the air, and when I comment out the first line (uncommenting the third), all of the Geometries are removed but the hitbox is still there, as well as the PhysicsCollisionListener still being active.



The desired behavior is the individual trap detaching its hitbox and geometry from the scene, as well as the PhysicsCollisionListener later being removed from the Physics Space.



How I am interpreting what my function() call should be doing as I coded it:



gets the spatial’s hitbox Physics Space which it is currently attached to, and removing the Spatial’s RigidBodyControl from that space (but not touching that of any other spatial).



removes the spatial from the parent (the rootNode) (again, not affecting any other spatial).



I would be inclined to think that it is a problem with how I set up the first statement, but the way that spatial.removeFromParent() behaves makes me think that it is my approach.



I am a novice at JME, and any advice would be appreciated. I can post all of my code, but as of posting I feel like that would be overkill as a lot of it seems irrelevant.



Thanks!

I haven’t looked at your code, but here is some info:

When a node is attached to the scene, it will have its controls activated: their update() and render() methods will be called.

If you remove a node from the scene with controls attached to it, the controls no longer get updated.

Try and keep as little code out of your main class’s update loop method; put the code into the controls or into AppStates. This can help track down issues and make it cleaner.



It sounds like you might be sharing some references if all traps are getting removed. I suggest you set a breakpoint in your “function()” method and see what the object id of the ‘spatial’, that it is attached to, is. Also step through with the debugger and inspect what all the values are.

is the id the value of the spatial, which is #2193 in my case (as the debugger is telling me for the values)?



the debugger variables line looks like this:

Name Type Value

… … …

spatial Node #2193

So, i have been playing around with it for a bit, and I feel like I have been using controls incorrectly… should I be generating a new TrapControl for each new Trap I want to make? e.g., if I want to have 9 traps on the map, will I have 9 TrapControl instances? or should I have one TrapControl managing all of them? I have been doing the former.

thats why it doesn’t work. you have to use 1 trap control for each trap.

jme doesn’t allow reusing the same control (hence why your code bugs).

tralala is right, it is one control per spatial (or trap in your case).



For “global” operations, you want an AppState and put the logic in its update() loop method.