2016-06-15 3 views
0

私はlibgdxを学んでいて、現在フラッディーな鳥のデモをやっています。楽しいときは、スコアが一定数に達したときに実装しようとしましたが、鳥のスプライトのテクスチャが更新され、別の色に変更されます。スプライトバッチを使用して色合いを変える代わりに、新しいテクスチャ(pngファイル)を作成したかったのです。Libgdxのアップデートアニメーション

問題は、アニメーションが必要なスプライトなので、翼がフラップするためです。実行時にテクスチャを更新しようとすると、アニメーションが動かなくなるだけです。ここで

は私の鳥のクラスである:ここで

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(); 
} 

}

は私のアニメーションクラスです:

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); 
} 

}

ここに私のレンダリングコードは、私の再生状態にあります。

@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(); 
} 

他のクラスが必要な場合は、質問してください。私はおそらく愚かな間違いをしているか、あるいは私が達成しようとしていることに対してまったく間違ってコーディングしています。

ありがとう、ジャクソン

+0

レンダリングループコードを投稿できますか? –

+0

なぜ更新のたびに新しい 'Animation'オブジェクトを作成していますか?それはひどい考えです。 – Zac

+0

私はその悪い考えを知っています。私は鳥が実際にテクスチャを変えるようにこれを行うだけです。 @ Zac –

答えて

0

テクスチャを1フレームごとにロードする必要はありません。 libgdxには独自のAnimationクラスがありますので、独自に作成する必要はありません。ここで

はlibgdxのgithubのからアニメーションの例です: 2D Animation

それを簡単にするために、ちょうど鳥の2 animaitonsを持って、あなたがする必要があるときにそれらを切り替えます。