Reputation: 21
I have been reading about libgdx and I have been stuck on this animation problem. I have reading about animation in libgdx from their github and when I run my app on my phone, the app crashes after the loading screen. Here is my player Actor that extends from Actor. I create the player actor in my menu screen class and add it to the stage. It is suppose to appear flying up and down in the menu screen but the app crashes. I know it is my player actor because when I comment out the player actor in my menu screen class the app does not crash. I use texture packer on my plane sprite and load it into my game.
public class PlayerActor extends Actor{
private DrunkPilot pGame;
private static final int NUM_ROWS = 5, NUM_COLS = 5;
private Animation<TextureRegion> drunkFlying;
private float stateTimer;
private TextureRegion region;
private Texture planeTex;
public PlayerActor(){
planeTex = pGame.assetManager.manager.get(Constants.plane);
planeTex = new Texture(Gdx.files.internal(Constants.plane));
drunkFlyingAnimation();
}
private void drunkFlyingAnimation(){
TextureRegion[][] tmp = TextureRegion.split(planeTex, planeTex.getWidth() / NUM_COLS, planeTex.getHeight() / NUM_ROWS);
TextureRegion[] flyFrames = new TextureRegion[NUM_COLS * NUM_ROWS];
int index = 0;
for (int i = 0; i < NUM_ROWS; i++) {
for (int j = 0; j < NUM_COLS; j++) {
flyFrames[index++] = tmp[i][j];
}
}
drunkFlying = new Animation<TextureRegion>(0.025f, flyFrames);
stateTimer = 0;
region = drunkFlying.getKeyFrame(0);
}
@Override
public void draw(Batch batch, float alpha){
super.draw(batch, alpha);
GdxUtils.clearScreen();
stateTimer += Gdx.graphics.getDeltaTime();
TextureRegion drunk = drunkFlying.getKeyFrame(stateTimer, true);
batch.draw(drunk, getX(), getY(), getWidth() / 2, getHeight() / 2, getWidth(), getHeight(), getScaleX(), getScaleY(), getRotation());
}
public void dispose(){
planeTex.dispose();
}
}
Here is where I add my actor to the stage.
@Override
public void show() {
Gdx.input.setInputProcessor(menuStage);
menuTitle = new Image(menuTitleTexture);
menuStartImg = new Image(menuStartTexture);
menuTable = new Table();
menuTable.setFillParent(true);
menuTable.add(menuTitle).pad(20).align(Align.top);
menuTable.row();
menuTable.add(menuStartImg).align(Align.bottom).pad(30);
menuStage.addActor(parallaxBackground);
menuStage.addActor(menuTable);
menuStage.addActor(player);
}
Upvotes: 0
Views: 531
Reputation: 2037
When you use TexturePacker there is an easier way to create animations.
When we want to create a run animation
First you ad all animation images to TexturePacker you have to look that every frame has the same name + underscore + frameNumber.
So the name of the frames must be: run_1, run_2, run_3 etc.
When we created it we have a run.txt and run.png in our assets.
Now we can load the TextureAtlas with our AssetManager:
AssetManager assetManager = new AssetManager();
assetManager.load("run.txt", TextureAtlas.class);
assetManager.finishLoading();
TextureAtlas runAnimationAtlas = assetManager.get("run.txt", TextureAtlas.class);
With this TextureAtlas we can create our Animation:
float frameDuration = 0.1f; // the duration between the animation frames
Animation<TextureRegion> runAnimation = new Animation<TextureRegion>(frameDuration, runAnimationAtlas.findRegions("run"), Animation.PlayMode.LOOP);
And render the Animation:
batch.draw(runAnimation.getKeyFrame(stateTimer),posX, posY)
Upvotes: 1