简体   繁体   中英

Libgdx updating animation

I'm learning libgdx and currently doing a flappy bird demo. For fun I tried to implement when the score reached a certain number the bird sprite texture will update and change to another color. Instead of using the spritebatch and changing the color through tinting I wanted to create a new texture(png file).

The problem is since it is a sprite it needs to be animated so the wings will flap. When I try and update the texture at runtime it will only work but the animation wont play.

Here is my bird class:

public class Bird {

private static final int GRAVITY = -15;
private static final int MOVEMENT = 100;
private Vector3 position;
private Vector3 velocity;
private Rectangle bounds;
private Animation birdAnimation;
private Texture birdTexture;
private TextureRegion textureRegion;
private Sound flap;



public Bird(int x, int y){
    position = new Vector3(x, y, 0);
    velocity = new Vector3(0, 0, 0);

    textureRegion = new TextureRegion(returnTexture());
    birdAnimation = new Animation(textureRegion, 3, 0.5f);

    bounds = new Rectangle(x, y, returnTexture().getWidth() / 3, returnTexture().getHeight());
    flap = Gdx.audio.newSound(Gdx.files.internal("sfx_wing.ogg"));

}

public void update(float dt){
    textureRegion = new TextureRegion(returnTexture());
    birdAnimation = new Animation(textureRegion, 3, 0.5f);

    birdAnimation.update(dt);

    if(position.y > 0){
        velocity.add(0, GRAVITY, 0);
    }
    velocity.scl(dt);
    position.add(MOVEMENT * dt, velocity.y, 0);
    if(position.y < 0){
        position.y = 0;
    }

    velocity.scl(1/dt);
    bounds.setPosition(position.x, position.y);
}

public TextureRegion getTexture() {
    return birdAnimation.getFrame();
}

public Texture returnTexture(){
    if(PlayState.score > 1){
        return birdTexture = new Texture("birdanimation1.png");
    }else{
        return birdTexture = new Texture("birdanimation.png");

    }
}

public Vector3 getPosition() {
    return position;
}

public void jump(){
    velocity.y = 250;
    flap.play(0.15f);
}

public Rectangle getBounds(){
    return bounds;
}

public void dispose(){
    returnTexture().dispose();
    flap.dispose();
}

}

Here is my animation class:

public class Animation {
private Array<TextureRegion> frames;
private float maxFrameTime;
private float currentFrameTime;
private int frameCount;
private int frame;

public Animation(TextureRegion region, int frameCount, float cycleTime){
    frames = new Array<TextureRegion>();
    int frameWidth = region.getRegionWidth() / frameCount;
    for(int i = 0; i < frameCount; i++){
        frames.add(new TextureRegion(region, i * frameWidth, 0, frameWidth, region.getRegionHeight()));
    }
    this.frameCount = frameCount;
    maxFrameTime = cycleTime / frameCount;
    frame = 0;
}

public void update(float dt){
    currentFrameTime += dt;
    if(currentFrameTime > maxFrameTime){
        frame++;
        currentFrameTime = 0;
    }
    if(frame >= frameCount){
        frame = 0;
    }
}

public TextureRegion getFrame(){
    return frames.get(frame);
}

}

Here's my render code in my play state:

@Override
public void render(SpriteBatch sb) {
    sb.setProjectionMatrix(cam.combined);
    sb.begin();
    sb.draw(bg, cam.position.x - (cam.viewportWidth /2), 0);
    sb.draw(bird.getTexture(), bird.getPosition().x, bird.getPosition().y);
    for(Tube tube : tubes){
        sb.draw(tube.getTopTube(), tube.getPosTopTube().x, tube.getPosTopTube().y);
        sb.draw(tube.getBottomTube(), tube.getPosBotTube().x, tube.getPosBotTube().y);
    }
    sb.draw(ground, groundPos1.x, groundPos1.y);
    sb.draw(ground, groundPos2.x, groundPos2.y);
    font.draw(sb, text, cam.position.x - gl.width / 2, cam.position.y + 200);
    sb.end();
}

If you need any other classes just ask. I'm probably making a stupid mistake or just coding it entirely wrong for what I'm trying to achieve.

Thanks, Jackson

You need to NOT load your textures every single frame. libgdx has its own Animation class so you don't need to make your own.

Here is an example on animation from libgdx's github: 2D Animation

To make it simple, just have 2 animaitons on Bird and switch between them when you need to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM