Trouble with animation

i’m getting errors but i dont know what i’m doing wrong.
NullPointerException on line 38:

> "control.addListener(this);"

This is the full script:

package JmonkeyTutorials;

import com.jme3.animation.AnimChannel;
import com.jme3.animation.AnimControl;
import com.jme3.animation.AnimEventListener;
import com.jme3.animation.LoopMode;
import com.jme3.app.SimpleApplication;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.light.DirectionalLight;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.scene.Node;
 
/** Sample 7 - how to load an OgreXML model and play an animation,
 * using channels, a controller, and an AnimEventListener. */
public class CopyOfHelloAnimation extends SimpleApplication implements AnimEventListener {
  private AnimChannel channel;
  private AnimControl control;
  Node player;
  
  public static void main(String[] args) {
    CopyOfHelloAnimation app = new CopyOfHelloAnimation();
    app.start();
  }
 
  @Override
  public void simpleInitApp() {
    viewPort.setBackgroundColor(ColorRGBA.LightGray);
    initKeys();
    DirectionalLight dl = new DirectionalLight();
    dl.setDirection(new Vector3f(-0.1f, -1f, -1).normalizeLocal());
    rootNode.addLight(dl);
    player = (Node) assetManager.loadModel("character3.j3o");
    player.setLocalScale(0.5f);
    control = player.getControl(AnimControl.class);
    control.addListener(this);
    channel = control.createChannel();
    channel.setAnim("chase");
    rootNode.attachChild(player);
    
  }
 
  public void onAnimCycleDone(AnimControl control, AnimChannel channel, String animName) {
    if (animName.equals("chase")) {
      channel.setAnim("wander", 0.50f);
      channel.setLoopMode(LoopMode.DontLoop);
      channel.setSpeed(1f);
    }
  }
 
  public void onAnimChange(AnimControl control, AnimChannel channel, String animName) {
    // unused
  }
 
  /** Custom Keybinding: Map named actions to inputs. */
  private void initKeys() {
    inputManager.addMapping("Walk", new KeyTrigger(KeyInput.KEY_SPACE));
    inputManager.addListener(actionListener, "Walk");
  }
  private ActionListener actionListener = new ActionListener() {
    public void onAction(String name, boolean keyPressed, float tpf) {
      if (name.equals("Walk") && !keyPressed) {
        if (!channel.getAnimationName().equals("chase")) {
          channel.setAnim("wander", 0.50f);
          channel.setLoopMode(LoopMode.Loop);
        }
      }
    }
  };
}

Thank you for your time.
Greets Wesly.

Obviously you don’t get an AnimControl in the line before so control stays null. Look at the j3o in the SceneExplorer to see where the AnimControl is.

1 Like

How do you mean? I got this code from the tutorial and just replaced the file name.
And why didn’t i got a AnimControl in the line before? It says getControl(AnimControl.class)

Because there is no AnimControl on the root node of the model. Thats why I suggest opening the model in the SceneComposer and checking where the AnimControl actually is.

1 Like

I did as you said but where can i find the AnimControl of my model?

In the scene explorer, click around the tree of your model’s scene graph to find the one with a control child.

1 Like

And to answer the inevitable question of “how do I get that spatial then?”, theres a getChild(String name) method in the Node class.

1 Like

Thank you. I found it!
but what now?

control = player.getControl().getChild(player); ?
i’m sorry, i’m so confused XD.

That doesn’t even make sense. Controls don’t have children. Nodes have children.

You seem very new to Java. You might consider doing some more Java tutorials and then going through all of the JME tutorials. The experience will be really helpful in getting past these progammer-beginner issues.

1 Like

True, first day :smile:
I will. But to get this fixed how do i do it? Player.getChild()?

player.getChild(“name of node”)

…do you know where the javadoc is?

Name of what node?
http://docs.oracle.com/javase/7/docs/api/overview-summary.html

The child you found in the scene explorer.

…this shouldn’t be so hard.

And not those javadocs… the JME javadocs, obviously.

player.getChild(“Armature”)

Haha got it.

public void simpleInitApp() {
    viewPort.setBackgroundColor(ColorRGBA.LightGray);
    initKeys();
    DirectionalLight dl = new DirectionalLight();
    dl.setDirection(new Vector3f(-0.1f, -1f, -1).normalizeLocal());
    rootNode.addLight(dl);
    player = (Node) assetManager.loadModel("character3.j3o");
    player.setLocalScale(0.5f);
    player.getChild("Armature");
    control = player.getControl(AnimControl.class);
    control.addListener(this);
    channel = control.createChannel();
    channel.setAnim("chase");
    rootNode.attachChild(player); 
  }

right?

Well does your animation play?

No it doesn’t :frowning:

Check if theres another AnimControl then.