Bhaskar
Bhaskar

Reputation: 700

LibGDX - Creating an infinitely looping animation

I am trying to create an infinitely looping animation in libgdx. The problem is when I run it, the animation will run the first cycle, but then crash because of a NullPointerException.

I have set the animation playMode to loop, by setting the boolean looping to true when calling getKeyFrame():

currentFrame = globeAnimation.getKeyFrame(time, true);

When I run this, it will work for keyframes 1-40 (the number of frames in my animation, but then will crash with this error the second it reaches keyframe 41 with this error message:

java.lang.NullPointerException

I also tried setting the playMode to loop by setting it in the constructor:

globeAnimation = new Animation(GLOBE_ANIMATION_FRAME_RATE, globeAnimationTextureRegions, PlayMode.LOOP);

For some reason when I do this, it gives me an error stating that it

cannot resovle constructor for 
Animation(float,TextureRegion[],PlayMode) constructor Animation.Animation(float,Array<? extends TextureRegion>,PlayMode)

This doesn't make sense to me, because the framework has a constructor that takes those exact parameters.

public Animation (float frameDuration, Array<? extends TextureRegion> keyFrames, PlayMode playMode) {

    this.frameDuration = frameDuration;
    this.animationDuration = keyFrames.size * frameDuration;
    this.keyFrames = new TextureRegion[keyFrames.size];
    for (int i = 0, n = keyFrames.size; i < n; i++) {
        this.keyFrames[i] = keyFrames.get(i);
    }

    this.playMode = playMode;
}

Can some one please explain either why that constructor isn't working or an alternative route to making this work.

Edit:

Phil Anderson found out why the constructor wasn't resolving: I had to replace globeAnimationTextureRegions with

new Array<TextureRegion>(globeAnimationTextureRegions).

for the constructor to resolve.

Upvotes: 0

Views: 707

Answers (1)

Phil Anderson
Phil Anderson

Reputation: 3146

You're getting this error...

cannot resovle constructor for 
Animation(float,TextureRegion[],PlayMode) constructor Animation.Animation(float,Array<? extends TextureRegion>,PlayMode)

Becuase you're passing in a Java array of TextureRegions, and the parameter type is a libGdx Array object of generic type TextureRegion.

Try something like...

globeAnimation = new Animation(GLOBE_ANIMATION_FRAME_RATE, new Array<TextureRegion>(globeAnimationTextureRegions), PlayMode.LOOP);

Upvotes: 2

Related Questions