[SOLVED] Animation Listener

I’ve been developing a zombie FPS, so I’m imported a model with the all animation that I needed. So by default, the zombie walk and when the player hits the zombie, the animation change to “ReactionHit”. My problem is that I want to change the animation back to “walking” when the “ReactionHit” ends. I followed the JME3 tutorial and add the listener to the control and implement the two methods (onAnimCycleDone and onAnimChange), but when I shoot the zombie the game crashes.

ERROR: IllegalArgumentException: The given listener is already registered at this AnimControl.

CODE:

    //Here I set the default walking animation of the zombie
    private void zombieAnimation(String animation){
        control = zombie.getControl(AnimControl.class);
        channel = control.createChannel();
        channel.setAnim(animation);
        channel.setLoopMode(LoopMode.Loop);
        channel.setSpeed(1f); 
    } 
    
    //Here is where I change the zombie animation to "ReactionHit"
    private void hitAnimation() {
        channel.reset(true);     
        control = zombie.getControl(AnimControl.class);
        control.addListener(this);
        channel.setAnim("ReactionHit");
        channel.setLoopMode(LoopMode.DontLoop);
        channel.setSpeed(1f);
    }

    @Override
    public void onAnimCycleDone(AnimControl control, AnimChannel channel, String animName) {
        if (animName.equals("ReactionHit")) {
            //Here I change the animation to "Running"
            zombieAnimation("Running");
        }
    }

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

this should really say it all.
You have to either unregister the listener after Running again or only ever set it up once, regardless of ReactionHit

2 Likes

You always (on each hitAnimation call) add the listener to the control, one time is enough. Like the error says…

It’s work. Thanks for your time!

1 Like