Animate spatial with nifty onClick problem

Hi,

i created a simple screen + layer + panel with interact, to animate and move my character.

Here my state class :

[java]
import com.jme3.animation.AnimChannel;
import com.jme3.animation.AnimControl;
import com.jme3.animation.AnimEventListener;
import com.jme3.animation.LoopMode;
import com.jme3.app.Application;
import com.jme3.app.SimpleApplication;
import com.jme3.app.state.AbstractAppState;
import com.jme3.app.state.AppStateManager;
import com.jme3.light.AmbientLight;
import com.jme3.light.DirectionalLight;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;
import com.jme3.scene.Spatial;
import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.screen.Screen;
import de.lessvoid.nifty.screen.ScreenController;

public class IntroState extends AbstractAppState implements AnimEventListener,
ScreenController {

private Nifty nifty;
private SimpleApplication app;
private AmbientLight sun;
private DirectionalLight sun2;
private Spatial city;
private Spatial playerModel;
private AnimControl playerControl;
private AnimChannel playerChannel;

@Override
public void initialize(AppStateManager stateManager, Application app) {
super.initialize(stateManager, app);
this.app = (SimpleApplication) app;

// setup camera
this.app.getCamera().setLocation(new Vector3f(20.4f, 15.8f, 13.7f));
this.app.getCamera().setRotation(new Quaternion(-0.1f, 0.9f, -0.3f, -0.2f));

// create basic light
sun = new AmbientLight();
sun.setColor(ColorRGBA.White.multLocal(2f));
this.app.getRootNode().addLight(sun);
sun2 = new DirectionalLight();
sun2.setDirection(new Vector3f(-0.1f, -0.7f, -1.0f).normalizeLocal());
this.app.getRootNode().addLight(sun2);

// create city
city = app.getAssetManager().loadModel("Models/level0/level0.j3o");
this.app.getRootNode().attachChild(city);

// create player
playerModel = app.getAssetManager().loadModel("Models/player/player.mesh.j3o");
playerModel.scale(0.5f);
playerModel.move(5f, 0.1f, 0f);
playerModel.rotate(0f, -1.5f, 0f);
this.app.getRootNode().attachChild(playerModel);
playerControl = playerModel.getControl(AnimControl.class);
playerControl.addListener(this);
playerChannel = playerControl.createChannel();
playerChannel.setAnim("idle");

}

@Override
public void cleanup() {
super.cleanup();
}

@Override
public void update(float tpf) {
//
}

/*

  • ####################
  • player control

  • ####################
    */
    public void onAnimCycleDone(AnimControl control, AnimChannel channel, String animName) {
    //
    }

public void onAnimChange(AnimControl control, AnimChannel channel, String animName) {
//
}

/*

  • ####################
  • gui control

  • ####################
    */
    public void bind(Nifty nifty, Screen screen) {
    this.nifty = nifty;
    }

public void onStartScreen() {
//
}

public void onEndScreen() {
//
}

public void playerMove() {
System.out.println(playerChannel.getAnimationName());
}

}
[/java]

if i’m writing

[java]
public void playerMove() {
System.out.println("#####");
}
[/java]

it works, the method got called. But i has no access to my objects like playerModel or playerChannel.

I remember nifty eats exceptions without printing the stack trace. (Maybe a NullPointerException caused by playerChannel)
Try using a try-catch in playerMove too see if an error occured that has stopped the call of your method.

@destroflyer said: I remember nifty eats exceptions without printing the stack trace. (Maybe a NullPointerException caused by playerChannel) Try using a try-catch in playerMove too see if an error occured that has stopped the call of your method.

yep, there is a nullpointer exception, but how i can access to my object from that method ?

You should be able to access it like anywhere else…
If there’s a NullPointerException, this means, your object is null - The mistake is somewhere else, maybe the initialization isn’t called properly.

@destroflyer said: You should be able to access it like anywhere else... If there's a NullPointerException, this means, your object is null - The mistake is somewhere else, maybe the initialization isn't called properly.

I know what null pointer exception means, but i created my state first and then the gui and didn’t noticed that the state initialization needs longer than the gui initialization.
However, that solved the problem, thanks for the support.