如何在LibGDX中的表格布局中添加动画?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在LibGDX中的表格布局中添加动画?相关的知识,希望对你有一定的参考价值。
我有一个包含图像和文本行的表。现在,我决定用旋转动画交换图像。有没有办法在表中执行此操作,还是必须使用SpriteBatch
来完成此操作?如果是的话,有没有办法保持我的表格,并以某种方式找到图像所在的行/列的正确位置?我不愿意摆脱桌子,因为有很大一部分文字。而使用setWrap
旗帜更容易。
答案
对的,这是可能的。
您可以将所有动画内容包装在一个组中。现在在表中插入该组,该表将管理组的位置。
希望这可以帮助。
另一答案
您必须在构造函数的开头加载所有内容
private void loadTextures() {
walkSheet = new Texture(Gdx.files.internal("animation_sheet.png"));
TextureRegion[][] tmp = TextureRegion.split(walkSheet, walkSheet.getWidth() /
FRAME_COLS, walkSheet.getHeight() / FRAME_ROWS);
walkFrames = new TextureRegion[FRAME_COLS * FRAME_ROWS];
int index = 0;
for (int i = 0; i < FRAME_ROWS; i++) {
for (int j = 0; j < FRAME_COLS; j++) {
walkFrames[index++] = tmp[i][j];
}
}
walkAnimation = new Animation(0.025f, walkFrames);
stateTime = 0f;
bobTexture = new Texture(Gdx.files.internal("images/bob.png"));
blockTexture = new Texture(Gdx.files.internal("images/block.png"));
}
这是绘制精灵的方法:
private void drawBob() {
Bob bob = world.getBob();
int facex=1;
if(bob.facingLeft){
facex=-1;
}
if (bob.state==bob.state.WALKING){
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
stateTime += Gdx.graphics.getDeltaTime();
currentFrame = walkAnimation.getKeyFrame(stateTime, true);
spriteBatch.draw(currentFrame, bob.position.x * ppuX, bob.position.y * ppuY, facex*Bob.SIZE * ppuX, Bob.SIZE * ppuY);
}
else if(bob.state==bob.state.IDLE){
spriteBatch.draw(bobTexture, bob.position.x * ppuX, bob.position.y * ppuY, facex* Bob.SIZE * ppuX, Bob.SIZE * ppuY);
}
//spriteBatch.draw(bobTexture, bob.position.x * ppuX, bob.position.y * ppuY, Bob.SIZE * ppuX, Bob.SIZE * ppuY);
}
只是不要忘记在调用之前开始sprite批处理。
public void render() {
spriteBatch.begin();
drawBlocks();
drawBob();
spriteBatch.end();
if (debug)
drawDebug();
}
完整的代码可以在我的重新博客中找到:http://www.sadafnoor.com/blog/adding-animation-on-character-movement-using-libgdx/
另一答案
随意使用它:(代码原样,不保修)
实现自己的可以显示动画的Actor:
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Actor;
/**
* nils honermann (zeg)
* - no warranty, free to use
*/
public class GdxAnimationActor extends Actor {
private Animation animation;
public void dispose(){
for(TextureRegion tr : animation.getKeyFrames()){
tr.getTexture().dispose();
}
animation = null;
}
public GdxAnimationActor(Animation animation){
this.animation=animation;
TextureRegion first = animation.getKeyFrame(0f);
setBounds(first.getRegionX(),first.getRegionY(),first.getRegionWidth(),first.getRegionHeight());
}
@Override
public void draw(Batch batch, float parentAlpha) {
super.draw(batch, parentAlpha);
// TODO: Should be multiplied with the actor's alpha, allowing a parent's alpha to affect all children.
TextureRegion current = animation.getKeyFrame(elapsed/1000.0f/*millis into seconds float*/);
batch.draw(current,getX(), getY(), getOriginX(), getOriginY(), getWidth(), getHeight(), getScaleX(), getScaleY(), getRotation());
if(renderedOnce){
elapsed += System.currentTimeMillis() - millisLastRender;
}
millisLastRender = System.currentTimeMillis();
renderedOnce = true;
}
private float elapsed = 0;
private boolean renderedOnce;
private long millisLastRender = 0;
}
并将动画传递给它。
float fps = 0.1f; //1 texture per 100 millis
Array<TextureRegion> textureArray = new Array<>(3);
textureArray.size = 3;
textureArray.set( (0) , new TextureRegion(new Texture(/*assets*/"example1.png");) );
textureArray.set( (1) , new TextureRegion(new Texture(/*assets*/"example2.png");) );
textureArray.set( (2) , new TextureRegion(new Texture(/*assets*/"example3.png");) );
Animation animation = new Animation(fps,textureArray,Animation.PlayMode.LOOP);
GdxAnimationActor animationActor = new GdxAnimationActor(animation);
只需将animationActor添加到舞台,窗口或表格中即可。
以上是关于如何在LibGDX中的表格布局中添加动画?的主要内容,如果未能解决你的问题,请参考以下文章