Except in the second one with TowerControl I’m trying to get the control of the selected tower. In the first line with selected after getControl(TowerControl.class) there is a get index.
Also I put your loop in here:
[java]
if(selected != -1 && state.getBudget() > 0 && !keyDown){
for(Spatial s = results.getClosestCollision().getGeometry(); s != null ; s = s.getParent()){
//TowerControl tower = rootNode.getChild(“tower”+selected).getControl(TowerControl.class);
TowerControl tower = s.getControl(TowerControl.class);
System.out.println(s); //print the geometry
if(s != null){
if(tower.getChargeNum() < 5){
//tower not full. load a charge
if(mapping.equals(“LoadCharge”)){
tower.addCharge(state.getLoadedCharge());
System.out.println(“Check 4”);
}
}
break;
}//if loop
}//for loop
}[/java]
Apparently the geometry is called mesh1.
However it gives me an Nullpointerexception on the line if(tower.getChargeNum() < 5){
I am unsure what to do at this point. Is there anything you would suggest me to do?
Change selected from int to Spatial. Set selected to s when you find one without a null TowerControl.
…then don’t even bother looking it up. It’s like you found the book you were looking for on the shelf but then somehow want to go back to the card catalog to look up the book. But you already had the book right in your hand… so just hold onto it.
@pspeed
It works now! Thanks. the field variable selected isn’t really necessary then if I’m using the loop. I managed to do it without changing it from int to spatial.
If I wanted the index I can just add this: s.getControl(TowerControl.class).getIndex();
EDIT: s.getControl(TowerControl.class).getIndex() doesn’t work. However the game still runs so nevermind. I forgot that s is a spatial not a geometry.
My last question is: Given a spatial say for example:
Is there a way to get the Geometry of that spatial. Say I figured out the geometry is called “geom” what do I have to do to access geom. I know for sure casting doesn’t work because you can’t cast a node to a geometry.