简体   繁体   中英

Issue with libgdx animation

I have an issue with a specific line when I try to set playmode for the animation.

According to this API I have following possibilities:

  • LOOP
  • LOOP_PINGPONG
  • LOOP_RANDOM
  • LOOP_REVERSED
  • NORMAL
  • REVERSED

However, when doing:

import com.badlogic.gdx.graphics.g2d.Animation;
...
spriteAnimation.setPlayMode(Animation.LOOP_PINGPONG);

I get LOOP_PINGPONG cannot be resolved or is not a field . What is the cause of this? As I see it is stated as doable in the API?


Edit:

I should state that I am following a guide for gamedev with libgdx. But the guide is over a year old so I can't seek much help there.

package com.guldbechnielsensolutions.gamehelpers;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Preferences;
import com.badlogic.gdx.graphics.g2d.Animation.PlayMode;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.TextureRegion;

public class AssetLoader {

    public static Preferences prefs;

    public static Texture texture;
    public static TextureRegion bg, grass;

    public static Animation spriteAnimation;
    public static TextureRegion sprite, spriteDown, spriteUp;

    public static TextureRegion skullUp, skullDown, bar;

    public static Sound dead, flap, coin;

    public static BitmapFont font, shadow;

    public static void load() {

        texture = new Texture(Gdx.files.internal("data/texture.png"));
        texture.setFilter(TextureFilter.Nearest, TextureFilter.Nearest);

        bg = new TextureRegion(texture, 0, 0, 136, 43);
        bg.flip(false, true);

        grass = new TextureRegion(texture, 0, 43, 143, 11);
        grass.flip(false, true);

        spriteDown = new TextureRegion(texture, 136, 0, 17, 12);
        spriteDown.flip(false, true);

        sprite = new TextureRegion(texture, 153, 0, 17, 12);
        sprite.flip(false, true);

        spriteUp = new TextureRegion(texture, 170, 0, 17, 12);
        spriteUp.flip(false, true);

        TextureRegion[] sprites = { spriteDown, sprite, spriteUp };
        spriteAnimation = new Animation(0.06f, sprites);
        spriteAnimation.setPlayMode(Animation.LOOP_PINGPONG);

        skullUp = new TextureRegion(texture, 192, 0, 24, 14);
        // Create by flipping existing skullUp
        skullDown = new TextureRegion(skullUp);
        skullDown.flip(false, true);

        bar = new TextureRegion(texture, 136, 16, 22, 3);
        bar.flip(false, true);

        dead = Gdx.audio.newSound(Gdx.files.internal("data/dead.wav"));
        flap = Gdx.audio.newSound(Gdx.files.internal("data/flap.wav"));
        coin = Gdx.audio.newSound(Gdx.files.internal("data/coin.wav"));

        font = new BitmapFont(Gdx.files.internal("data/text.fnt"));
        font.getData().setScale(.25f, -.25f);
        shadow = new BitmapFont(Gdx.files.internal("data/shadow.fnt"));
        shadow.getData().setScale(.25f, -.25f);

        prefs = Gdx.app.getPreferences("Get Me Out");

        if (!prefs.contains("highScore")) {
            prefs.putInteger("highScore", 0);
        }       
    }

    public static void setHighScore(int val) {
        prefs.putInteger("highScore", val);
        prefs.flush();
    }

    public static int getHighScore() {
        return prefs.getInteger("highScore");
    }

    public static void dispose() {
        // We must dispose of the texture when we are finished.
        texture.dispose();

        // Dispose sounds
        dead.dispose();
        flap.dispose();
        coin.dispose();

        font.dispose();
        shadow.dispose();
    }

}

https://github.com/libgdx/libgdx/blob/master/gdx/src/com/badlogic/gdx/graphics/g2d/Animation.java

Here is the source code for the Animation Class.

As you can see the enum is actually called "PlayMode" so instead of doing this:

spriteAnimation.setPlayMode(Animation.LOOP_PINGPONG);

you should be doing this

spriteAnimation.setPlayMode(Animation.PlayMode.LOOP_PINGPONG);

EDIT:

Since you have imported the following:

import com.badlogic.gdx.graphics.g2d.Animation.PlayMode;

You could also use this:

spriteAnimation.setPlayMode(PlayMode.LOOP_PINGPONG);

I would try PlayMode.LOOP_PINGPONG . I looked into the libgdx Github directory and it appears that PlayMode is an enum.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM