[英]Animate multiple sprites in libgdx screen
我正在 libgdx 中制作一個小游戲,我想在屏幕上制作更多角色的動畫。 我知道如何為單個角色制作動畫,但考慮到例如 5 個角色,我不知道如何為所有角色制作動畫。 誰能給我一些建議? 現在字符在一個數組中。 我的數組是:
public void makeCharacters() {
characters = new Array<Characters>();
characters.add(new Characters(new Sprite(new Texture("img1.png")), spawn));
characters.add(new Characters(new Sprite(new Texture("img2.png")), spawn));
characters.add(new Characters(new Sprite(new Texture("img3.png")), spawn));
characters.add(new Characters(new Sprite(new Texture("img4.png")), spawn));
characters.add(new Characters(new Sprite(new Texture("img5png")), spawn));
如果你的角色有一個draw()
函數
for(Characters c: characters){ c.draw() }
否則我會這樣做
class Characters{
public SpriteBatch batch;
public TextureRegion[] regions; //create array of texture
public Animation<TextureRegion> animation;
public Characters(String relative_path, int tileWidth, int tileHeight){
batch = new SpriteBatch();
// I take into consideration that you use an image so probably a sprite sheet.
regions = TextureRegion.split(new Texture(relative_path), tileWidth, tileHeight)[0];
animation = new Animation<>(frameDuration, regions);
}
public void draw(float elapse){
batch.begin();
batch.draw(animation.getKeyFrame(elapse, `loop` : true),x,y,width,height);
batch.end();
}
}
public class MyGdxGame extends ApplicationAdapter {
Characters characters = new Array<Characters>();
@Override
public void create () {
//width/height of a frame not the entire image
characters.add("img1.png", width, height);
characters.add("img2.png", width, height);
characters.add("img3.png", width, height);
characters.add("img4.png", width, height);
characters.add("img5.png", width, height);
}
@Override
public void render () {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
elapse += Gdx.graphics.getDeltaTime();
for(Characters c: characters ){
c.draw(elapse);
}
}
}
這是很多代碼,但希望它有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.