[英]Animate multiple sprites in libgdx screen
I am making a small game in libgdx, and I would like to animate more characters on the screen.我正在 libgdx 中制作一个小游戏,我想在屏幕上制作更多角色的动画。 I know how to animate a single character, but considering for example 5 characters, I wouldn't know how to animate all of them.
我知道如何为单个角色制作动画,但考虑到例如 5 个角色,我不知道如何为所有角色制作动画。 Can anyone give me some advice?
谁能给我一些建议? For now the characters are inside an array.
现在字符在一个数组中。 My array is:
我的数组是:
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));
if your characters have a draw()
function如果你的角色有一个
draw()
函数
for(Characters c: characters){ c.draw() }
Otherwise I do it like this否则我会这样做
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);
}
}
}
it is lot of code but hope its helping.这是很多代码,但希望它有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.