我有一个包含图像和文本行的表。现在,我决定用旋转动画交换图像。有没有办法在表中执行此操作,还是必须使用SpriteBatch
来完成此操作?如果是的话,有没有办法保持我的表格,并以某种方式找到图像所在的行/列的正确位置?
我不愿意摆脱桌子,因为有很大一部分文字。使用setWrap
标志可以更轻松。
答案 0 :(得分:0)
您必须在构造函数
的开头加载所有内容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);
}
在打电话之前不要忘记开始精灵批处理。
public void render() {
spriteBatch.begin();
drawBlocks();
drawBob();
spriteBatch.end();
if (debug)
drawDebug();
}
完整的代码可以在我的重新博客中找到: http://www.sadafnoor.com/blog/adding-animation-on-character-movement-using-libgdx/
答案 1 :(得分:0)
是的,这是可能的。
您可以将所有动画内容包装在一个组中。现在将该组插入表中,该表将管理该组的位置。
希望这有帮助。
答案 2 :(得分:0)
随时使用: (按原样提供代码,不提供保修)
实现自己的可以显示动画的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添加到舞台,例如窗口或表格中即可。