本文介紹了在Java中使用LibGdx制作Sprite Sheet動畫的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我在這里有一些代碼,它獲取一個精靈工作表,并通過獲取圖像的長度和寬度并按行和列對其進行細分來將其劃分為單獨的精靈。這個,適用于我的測試精靈工作表,但不是我實際上要用于我的游戲的那個。
我的動畫課在這里:
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, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
man.draw(batch);
batch.end();
}
}
當我使用WalkingMan.png時,代碼可以完美地工作。但當我使用任何其他精靈工作表時,例如WalkingWomen an.jpg(我并不擔心這個工作表不透明),當相應地調整列和行時,對齊方式是關閉的。有什么建議嗎?
WalkingMan.png
WalkingWoman.jpg
推薦答案
動畫類似乎通過假定精靈工作表圖像中的所有幀都均勻分布來將源圖像拆分為多個幀。因此,對于WalkingMan圖像,512x512px的原始圖像被分割成30幀,每幀85.33px寬(512/6列),102.4px高(512/5行)。
如果如您所說,調整WalkingWomen圖像的列數和行數(1300×935),您將得到36幀,每幀寬144.44px(1300/9列),高233.75px(935/4行)。
問題是,WalkingWomen精靈的每個幀的間隔不均勻。如果將第一幀從圖像中拆分出來,則會得到如下結果:
see how her foot is cut off on the right
WalkingMan圖像將每個圖像放置在一個大小相同的邊界框內的精靈工作表中。您只需確保WalkingWomen床單的每一幀都以相同的方式均勻分布。
這篇關于在Java中使用LibGdx制作Sprite Sheet動畫的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,