Reputation: 1515
I have a table that contains rows with image and text. Now, I decided to exchange the image with a rotation animation. Is there a way to do this in a table or do I have to use SpriteBatch
to accomplish this? If yes, is there a way to keep my table and somehow find the correct position of that row/column that the image was in?
I'm reluctant to get rid of the table because there's a large portion of text. And it makes it much easier with the setWrap
flag.
Upvotes: 0
Views: 1534
Reputation: 586
Feel free to use it: (Code as is, no warranty)
Implement your own Actor that can display Animations:
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;
}
and pass your animation into it.
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);
just add your animationActor to your stage, a window or a table for example.
Upvotes: 0
Reputation: 7057
Yes, it is possible.
You can wrap all animation stuff in a Group. Now insert that Group in the table which will manage location of the group.
Hope this helps.
Upvotes: 1
Reputation: 7540
You have to load everything at the beginning in the constructor
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"));
}
This is the method would look like that draws sprite:
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);
}
Just don't forget to begin sprite batch before calling it.
public void render() {
spriteBatch.begin();
drawBlocks();
drawBob();
spriteBatch.end();
if (debug)
drawDebug();
}
The full code can be found here in my re-blog: http://www.sadafnoor.com/blog/adding-animation-on-character-movement-using-libgdx/
Upvotes: 0