【问题标题】:Libgdx animation doesn't play or loopLibgdx 动画不播放或循环播放
【发布时间】:2021-07-19 03:09:52
【问题描述】:

我制作了一个游戏,其中一只乌龟必须收集海星,但是当我添加行走动画时它不起作用。发生的情况是它只显示第一帧,整个动画没有播放,我没有收到任何错误。

我正在使用不同的图像来加载动画

这是我的动画方法-

// used to load animation from multiple files
public Animation loadAnimationFromFiles(String[] fileNames, float frameDuration, boolean loop)
{
    int fileCount = fileNames.length;
    Array<TextureRegion> textureArray = new Array<TextureRegion>();
    for (int n = 0; n < fileCount; n++)
    { 
        String fileName = fileNames[n];
        Texture texture = new Texture( Gdx.files.internal(fileName) );
        texture.setFilter( TextureFilter.Linear, TextureFilter.Linear );
        textureArray.add( new TextureRegion( texture ) );
        
    }
    
    Animation anim = new Animation(frameDuration, textureArray);
    if (loop) {
    //setAnimation(anim);
        System.out.println("animation run");
        anim.setPlayMode(Animation.PlayMode.LOOP);
    }
    else {
        anim.setPlayMode(Animation.PlayMode.NORMAL);
    }
    if (animation == null) {
        System.out.println("animation null");
        setAnimation(anim);
    }
    return anim;
}

这是我使用动画的乌龟类-

public class Turtle extends BaseActor
{
public Turtle(float x, float y, Stage s)
{
    super(x, y, s);
    
    String[] fileNames = {
        "turtle2.png",
        "turtle.png",
    };

    loadAnimationFromFiles(fileNames, 0.1f, true);
    
}

public void act(float dt)
{   
    super.act(dt);
    if (Gdx.input.isKeyPressed(Input.Keys.LEFT))
        this.moveBy(-5,0);
    if (Gdx.input.isKeyPressed(Input.Keys.RIGHT))
        this.moveBy(5,0);
    if (Gdx.input.isKeyPressed(Input.Keys.UP))
        this.moveBy(0,5);
    if (Gdx.input.isKeyPressed(Input.Keys.DOWN))
        this.moveBy(0,-5);
    
}

}

【问题讨论】:

    标签: java animation libgdx


    【解决方案1】:

    您刚刚创建了动画,但从未使用过它。要使用动画,您需要通过测量经过的增量时间从中获取纹理。

    您的海龟类应该看起来更像这样:

    public class Turtle extends BaseActor
    {
    
       private Animation<TextureRegion> animation;//store the animation in a field to use it
       private float time;//use a timer for the animation to be played
    
       public Turtle(float x, float y, Stage s)
       {
           super(x, y, s);
        
           String[] fileNames = {
               "turtle2.png",
               "turtle.png",
           };
    
           //store the animation to the field instead of just creating it
           animation = loadAnimationFromFiles(fileNames, 0.1f, true);
           //set the animations timer
           time = 0;
        }
    
        public void act(float dt)
        {   
            //increase the time since the animation was started by the delta time
            time += dt;
            //tell the animation how much time passed, so it can give you the current animation texture
            TextureRegion texture = animation.getKeyFrame(time);
            
            //TODO draw texture
    
            //I assume the super.act call also draws a texture; that should be deleted to not draw over the already created texture
            super.act(dt);
            if (Gdx.input.isKeyPressed(Input.Keys.LEFT))
                this.moveBy(-5,0);
            if (Gdx.input.isKeyPressed(Input.Keys.RIGHT))
                this.moveBy(5,0);
            if (Gdx.input.isKeyPressed(Input.Keys.UP))
                this.moveBy(0,5);
            if (Gdx.input.isKeyPressed(Input.Keys.DOWN))
                this.moveBy(0,-5);
        
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-16
      • 1970-01-01
      • 1970-01-01
      • 2014-03-21
      • 2013-12-10
      • 2023-03-31
      相关资源
      最近更新 更多