I’m having trouble getting custom controls to work. Any help would be appreciated. I’m pretty sure my mistake is something elementary, but I couldn’t find an answer in the documentation.
Simply put I can’t get code I put in the controlUpdate method to run. My understanding is this code will run automatically once the custom control is attached to a spatial (please correct me if I’m wrong here). I’ve also tried calling the controlUpdate method in various ways from simpleUpdate, to no avail.
The setSpatial code is working when I initially attach the control to a spatial.
If I attach the control to rootNode the controlUpdate code runs. But it won’t run on any other spatial.
Code below for reference
Main class
[java]package mygame;
import com.jme3.app.SimpleApplication;
import com.jme3.scene.Node;
public class Main extends SimpleApplication {
public static void main(String[] args) {
Main app = new Main();
app.start();
}
@Override
public void simpleInitApp() {
bugControl bugControl = new bugControl (assetManager, rootNode);
Node Bug1 = new Node();
Bug1.addControl(bugControl);
}
@Override
public void simpleUpdate(float tpf) {
}
}[/java]
bugControl class
[java]package mygame;
import com.jme3.scene.control.AbstractControl;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;
import com.jme3.asset.AssetManager;
import com.jme3.scene.Node;
import com.jme3.export.JmeExporter;
import com.jme3.export.JmeImporter;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Spatial;
import com.jme3.scene.control.Control;
import java.io.IOException;
public class bugControl extends AbstractControl implements Cloneable {
protected Node rootNode;
protected AssetManager assetManager;
public bugControl(){}
public bugControl(AssetManager assetmanager, Node rootnode){
rootNode = rootnode;
assetManager = assetmanager;
}
@Override
public void setSpatial(Spatial spatial) {
super.setSpatial(spatial);
Box box = new Box(1, 0.3f, 0.3f);
spatial = new Geometry(“Box”, box);
Material mat = new Material(assetManager, “Common/MatDefs/Misc/Unshaded.j3md”);
mat.setColor(“Color”, ColorRGBA.randomColor());
spatial.setMaterial(mat);
rootNode.attachChild(spatial);
}
@Override
protected void controlUpdate(float tpf){
if(spatial != null) {
spatial.move(tpf,0,0);
}
}
@Override
public Control cloneForSpatial(Spatial spatial){
final bugControl control = new bugControl();
control.setSpatial(spatial);
return control;
}
@Override
public void read(JmeImporter im) throws IOException {
super.read(im);
}
@Override
public void write(JmeExporter ex) throws IOException {
super.write(ex);
}
@Override
protected void controlRender(RenderManager rm, ViewPort vp) {
}
}[/java]