[SOLVED] Animation doesn't work

Hello! I have NullPointerException in string "“control.addListener(this);”

package mygame.utils;

import com.jme3.animation.AnimChannel;
import com.jme3.animation.AnimControl;
import com.jme3.animation.AnimEventListener;
import com.jme3.animation.Animation;
import com.jme3.animation.LoopMode;
import com.jme3.animation.SpatialTrack;
import com.jme3.app.SimpleApplication;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.collision.shapes.CapsuleCollisionShape;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import controls.avatarCameraInputControl;
import java.util.HashMap;

/**
 *
 * @author vik24rus
 */
public class Avatar implements AnimEventListener {
    Spatial objectAvatar;
    SimpleApplication app;
    RigidBodyControl phyAvatar;
    BulletAppState bulletApp;
    avatarCameraInputControl avatarCameraInputControl;
    AnimControl avatarAnimaControl;
    AnimChannel avatarAnimaChannel;
    Node player;
    private AnimChannel channel;
    private AnimControl control;
    public Avatar(SimpleApplication app , BulletAppState bulletApp ) {
        this.app = app;
        this.bulletApp = bulletApp;
        objectAvatar = app.getAssetManager().loadModel("Models/Player/Player.j3o"); //LoadPlayerModel
        player = (Node) app.getAssetManager().loadModel("Models/Player/Player.j3o");
        objectAvatar.setLocalTranslation(new Vector3f(0.5f,0.5f,0.5f));  //StartPlayerPosition
        avatarCameraInputControl = new avatarCameraInputControl(app , objectAvatar);
        objectAvatar.addControl(avatarCameraInputControl);
        addPhysics();
        addAnimation();
    }
    
    private void addPhysics(){
        phyAvatar = new RigidBodyControl( new CapsuleCollisionShape(1 , 2) , 2.0f );
        objectAvatar.addControl(phyAvatar);
        phyAvatar.setPhysicsSpace(bulletApp.getPhysicsSpace());
        phyAvatar.setKinematic(true) ;
    }
    
    private void addAnimation(){
        
        control = player.getControl(AnimControl.class);
        control.addListener(this);
        channel = control.createChannel();
        channel.setAnim("ArmatureAction");

    }
    
    
    
    
    public Spatial getObjectAvatar(){
        return objectAvatar;
    }

    @Override
    public void onAnimCycleDone(AnimControl control, AnimChannel channel, String animName)
    {
        channel.setAnim("ArmatureAction", 0.50f);
        channel.setLoopMode(LoopMode.DontLoop);
        channel.setSpeed(1f);
    }

    @Override
    public void onAnimChange(AnimControl control, AnimChannel channel, String animName)
    {
        
    }
    
    public void onAction(String binding, boolean value, float tpf) {
       
    }
        
}

I think that because AnimControl is not in the main node in my model? or no?
All examples what I found locate animation in class which extends SimpleApplication, but I use animation in my class. Tell me please,what i make not properly

the control is not in the root node of the model. getControl does not traverse the scenegraph, so you must obtain the correct child node “Cube”.

something like player.getchild(0).getChild(0).getControl(AnimControl.class) should suffice. use the string names if you prefer. Case sensitive.

sorry for the terrible grammar, i do not get on well with tablets.

or player.getChild(“Cube”)

https://javadoc.jmonkeyengine.org/com/jme3/scene/Node.html#getChild-java.lang.String-

Edit: and note: I know jayfella mentioned that in his but I missed it the first time I read it and others might also. It does the traversal through the children to find the node named that.

…it’s a bit dangerous if all of your nodes are generically named, though.

you know i never knew it traversed. i presumed it wouldnt due to so many potential conflicts. quite convenient for models. ty. :slight_smile:

ALL WORKS!!! Best game engine! :slight_smile: so simple! And I wait new Monkanim :smiley:

2 Likes