使用LibGdx对Java中的Sprite Sheet进行动画处理

我在这里有一些代码,可以获取一个精灵表,并通过考虑图像的长度和宽度并将其按行和列划分,将其划分为各个精灵。这适用于我的测试Sprite工作表,但不适用于我要用于游戏的工作。

我的动画课在这里:

package Simple;


    import com.badlogic.gdx.Gdx;
    import com.badlogic.gdx.graphics.Texture;
    import com.badlogic.gdx.graphics.g2d.Batch;
    import com.badlogic.gdx.graphics.g2d.TextureRegion;
    import com.badlogic.gdx.graphics.g2d.Animation;

    public class RunningMan {
        private Texture texture;
        private TextureRegion[] walkFrames;
        private Animation walkAnimation;
        private float statetime = 0f;
        private TextureRegion currentFrame;
        int rows = 5;
        int cols = 6;
        public RunningMan(Texture texture) {
            this.texture = texture;
            TextureRegion[][] tmp = TextureRegion.split(texture,texture.getWidth() / cols,texture.getHeight() / rows);  // split the sprite sheet up into its 30 different frames
            walkFrames = new TextureRegion[rows * cols];
            int index = 0;
            for(int i = 0; i < rows; i++) {
                for (int j = 0; j < cols; j++) {
                    walkFrames[index++] = tmp[i][j];    // Put the 30 frames into a 1D array from the 2d array
                }
            }
            walkAnimation = new Animation(0.06f,walkFrames);  // initialize the animation class
            statetime = 0f;
        }

        public void draw(Batch batch) {
            statetime += Gdx.graphics.getDeltaTime();   // statetime is used to determine which frame to show
            if(statetime > walkAnimation.getanimationDuration()) statetime -= walkAnimation.getanimationDuration();  // when we reach the end of the animation,reset the statetime
            currentFrame = walkAnimation.getKeyFrame(statetime);    // Get the current Frame

            batch.draw(currentFrame,50,50); // draw the frame
        }
    }

我运行动画的班级在这里:

package Simple;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

public class Animation extends ApplicationAdapter {
    SpriteBatch batch;
    RunningMan man;

    @Override
    public void create () {
        batch = new SpriteBatch();
        man = new RunningMan(new Texture(Gdx.files.internal("WalkingMan.png")));
    }

    @Override
    public void render () {
        Gdx.gl.glClearColor(1,1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        batch.begin();
        man.draw(batch);
        batch.end();
    }
}

当我使用WalkingMan.png时,代码可以完美运行。但是,当我去使用其他任何精灵表时,例如WalkingWoman.jpg(我不担心这一步是不透明的),当相应地调整列和行时,对齐方式将关闭。有什么建议么?

WalkingMan.png

WalkingWoman.jpg

wuzh166 回答:使用LibGdx对Java中的Sprite Sheet进行动画处理

动画类似乎通过假设Spritesheet图像中的所有帧均等间隔将源图像拆分为多个帧。因此,对于WalkmanMan图像,将512x512px的原始图像分成30帧,每帧分别为85.33px宽(512/6列)和102.4px高(512/5行)。

如果您说过,调整了Walkingwoman图像的列和行号(即1300x935),则会得到36个帧,每个帧宽144.44像素(1300/9列),高233.75像素(935/4行) )。

问题在于,WalkingWoman Spritesheet的每一帧都没有均匀分布。如果将第一帧从图像中分离出来,则会得到:

see how her foot is cut off on the right

WalkingMan图像将每个图像放置在Spritesheet中大小相同的边界框中。您只需要确保Walkingwoman工作表以相同的方式均匀间隔每个帧即可。

本文链接:https://www.f2er.com/3064400.html

大家都在问