有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

libgdx动画的java问题

当我尝试为动画设置播放模式时,我遇到了特定行的问题

根据this API,我有以下可能性:

  • 循环
  • 乒乓球
  • 循环随机
  • 反循环
  • 正常的
  • 颠倒

但是,在执行以下操作时:

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

我得到LOOP_PINGPONG cannot be resolved or is not a field。这是什么原因?在我看来,它在API中是可行的吗


编辑:

我应该声明,我正在使用libgdx跟踪gamedev的guide。但是导游已经一岁多了,所以我不能在那里寻求太多帮助

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();
    }

}

共 (2) 个答案

  1. # 1 楼答案

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

    下面是动画类的源代码

    正如您所看到的,枚举实际上被称为“播放模式”,所以不要这样做:

    spriteAnimation.setPlayMode(Animation.LOOP_PINGPONG);
    

    你应该这么做

    spriteAnimation.setPlayMode(Animation.PlayMode.LOOP_PINGPONG);
    

    编辑:

    由于您已导入以下内容:

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

    你也可以用这个:

    spriteAnimation.setPlayMode(PlayMode.LOOP_PINGPONG);
    
  2. # 2 楼答案

    我想试试。我查看了libgdx Github目录,发现PlayMode是一个枚举