[SOLVED] Animation Listener

Hi, in the last three weeks I’ve been creating my own FPS shooter and the enemies are the zombies. So my problem is when I hit the zombie it would change the animation from “walking” to “reactionHit”, but it doesn’t change.

CODE:

private AnalogListener analogListener = new AnalogListener() {
@Override
public void onAnalog(String name, float intensity, float tpf) {
if (name.equals(MAPPING_SHOOT)){
CollisionResults results = new CollisionResults();

            Ray ray = new Ray(cam.getLocation(), cam.getDirection());
            
            rootNode.collideWith(ray, results);
            
            String zombieName = results.getClosestCollision().getGeometry().getName();
            if (!zombieName.equals("Box") && !zombieName.equals("erba-geom-0") && !zombieName.equals("ID56") 
                && !zombieName.equals("ID111_0") && !zombieName.equals("M4A1-geom-0")){
                if (results.size() > 0){  
                    CollisionResult target = results.getClosestCollision();
                    zombieAnimation("ReactionHit");
                } 
            }
        }     
    }  
};
//This set the walking animation

private void zombieAnimation(){
control = zombie.getControl(AnimControl.class);
channel = control.createChannel();
channel.setAnim(“Walking”);
channel.setLoopMode(LoopMode.Loop);
channel.setSpeed(1f);
}

private void hitAnimation() {
    channel.reset(true);
    control = zombie.getControl(AnimControl.class);
    channelHit = control.createChannel();
    channelHit.setAnim("ReactionHit", 0.50f);
    channelHit.setLoopMode(LoopMode.DontLoop);
    channelHit.setSpeed(1f);
}

Well, it simply looks like you don’t set value to variable “zombie”. It looks like magic to me.

And now that I look it even longer. You never call zombieAnimation(), you call zombieAnimation(string derp).

A little advice. To reduce complexity and increase manageability, why don’t you give the zombie as a parameter to the zombieAnimation function? Now it relies on some ambient context you need to always manage somewhere and this… will almost never work. Super tricky.

The “zombie” is a Spatial that contain a model with its animation.

That my fault when I was copying the code. So the correct code is:

private AnalogListener analogListener = new AnalogListener() {
@Override
public void onAnalog(String name, float intensity, float tpf) {
if (name.equals(MAPPING_SHOOT)){
CollisionResults results = new CollisionResults();

            Ray ray = new Ray(cam.getLocation(), cam.getDirection());
            
            rootNode.collideWith(ray, results);
            
            String zombieName = results.getClosestCollision().getGeometry().getName();
            if (!zombieName.equals("Box") && !zombieName.equals("erba-geom-0") && !zombieName.equals("ID56") 
                && !zombieName.equals("ID111_0") && !zombieName.equals("M4A1-geom-0")){
                if (results.size() > 0){  
                    CollisionResult target = results.getClosestCollision();
                    hitAnimation();
                } 
            }
        }     
    }  
};
//Set the default "walking" animation
private void zombieAnimation(){
   control = zombie.getControl(AnimControl.class);
   channel = control.createChannel();
   channel.setAnim(“Walking”);
   channel.setLoopMode(LoopMode.Loop);
    channel.setSpeed(1f);

}

private void hitAnimation() {
    channel.reset(true);
    control = zombie.getControl(AnimControl.class);
    channelHit = control.createChannel();
    channelHit.setAnim("ReactionHit", 0.50f);
    channelHit.setLoopMode(LoopMode.DontLoop);
    channelHit.setSpeed(1f);

}

I’ve resolved it, I was calling the zombieAnimation() in the simpleUpdate, so the hitAnimation() was never play.

1 Like

For the future:

Though we’ve told you that before already.

If you forget the link it’s stickied to the top of the main forum page.

Debugging can save a ton of your time :slight_smile: