I have the following code:
public class Simulator extends SimpleApplication implements AnimEventListener {
private AnimChannel animationChannel;
private AnimControl animationController;
private Node avatar;
// Constructor, getters/setters, etc.
@Override
public void simpleInitApp() {
viewPort.setBackgroundColor(ColorRGBA.LightGray);
DirectionalLight directionalLight = new DirectionalLight();
rootNode.addLight(directionalLight);
avatar = (Node)assetManager.loadModel("models/Ninja.mesh.xml");
avatar.localScale = 0.025f;
rootNode.attachChild(avatar);
animationController = avatar.getControl(AnimControl);
animationController.addListener(this);
animationChannel = animationController.createChannel();
flyCam.setDragToRotate(true);
setDisplayFps(false);
setDisplayStatView(false);
animationChannel.setAnim("Walk", 0.5f);
}
@Override
public void yeehaw() {
animationChannel.setAnim("Backflip", 0.5f);
}
@Override
public void onAnimCycleDone(AnimControl control, AnimChannel channel, String animName) {
channel.setAnim('Idle1');
channel.setLoopMode(LoopMode.DontLoop);
channel.setSpeed(1f);
}
@Override
public void onAnimChange(AnimControl control, AnimChannel channel, String animName) {
// No-op for now
}
}
When I run:
public class Driver {
public static void main(String[] args) {
Simulator simulator = new SImulator();
simulator.start();
}
}
Everything runs perfectly. But the minute I try to invoke animationController
from outside the Simulator
like so:
public class Driver {
public static void main(String[] args) {
Simulator simulator = new SImulator();
simulator.start();
simulator.yeehaw();
}
}
I start getting null NPEs:
java.lang.NullPointerException: Cannot invoke method setAnim() on null object
Any idea why this is?