2017-03-04 4 views
1

同じActorオブジェクトのアニメーションが違っています。Libgdx - 同じActorの異なるアニメーションの処理

メインゲームのScreenクラスから定数を渡して、それらの間を切り替えたいと思います。

例これは私のメインの俳優である:

public class MainChar extends Actor { 
    private float showTime; 
    private Animation<TextureRegion> animation; 
    private Animation<TextureRegion> animation2; 
    private boolean state = false; 
    private TextureAtlas texture = new TextureAtlas(Gdx.files.internal("actors/MainChar/a.pack"));//mainanim.pack 
    private TextureAtlas textureFF = new TextureAtlas(Gdx.files.internal("actors/MainChar/b.pack")); 
    private long longCounter = 0; 
    public boolean isState() { 
     return state; 
    } 

    public void setState(boolean state) { 
     this.state = state; 
    } 
    public BitmapFont font; 
    public MainChar(){ 

     font = new BitmapFont(); 
     animation = new Animation<TextureRegion>(1/5f, texture.getRegions()); 
     animation2 = new Animation<TextureRegion>(1/7f, textureFF.getRegions()); 
    } 

    float myDelta; 
    @Override 
    public void act(float delta){ 
     super.act(delta); 
     myDelta = delta; 
     showTime += delta; 
    } 

    @Override 
    public void draw(Batch batch, float parentAlpha){ 
     longCounter++; 

     if(longCounter== 500) 
      setState(true); 
     super.draw(batch, parentAlpha); 

     if(!isState()) 
      batch .draw(animation .getKeyFrame(showTime, false), 0, 0); 
     else 
      batch.draw(animation2.getKeyFrame(showTime, true), 0, 0); 
    } 
} 

これは、呼び出し元のクラスです:

public class GameScreen implements Screen { 
    myGame myGame; 
    SpriteBatch batch; 
    Sprite sprite; 
    Texture img; 
    Texture imgDialogBoxGood; 
    OrthographicCamera camera; 

    private Stage stage; 
    MainChar mainChar; 

    public GameScreen(myGame myGame) { 

     this.myGame = myGame; 
     camera = new OrthographicCamera(); 
     camera.setToOrtho(false, 480, 800); 
     mainChar = new MainChar(); 

     batch = new SpriteBatch(); 
     stage = new Stage(); 
     stage.getViewport().setCamera(camera); 
     stage.addActor(mainChar); 
     batch.setProjectionMatrix(camera.combined); 
    } 

    @Override 
    public void render(float delta) { 
     Gdx.gl.glClearColor(1, 0, 0, 1); 
     Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 
     camera.update(); 
     batch.begin(); 
     stage .act(Gdx.graphics.getDeltaTime()); 
     stage.draw(); 
     batch.end(); 
    } 

    @Override 
    public void resize(int width, int height) { } 

    @Override 
    public void dispose() { 
     stage.dispose(); 
     myGame.dispose(); 
    } 
} 

私は「初心者ですので、私はあまり興味深い部分... を切り取りますlibgdxに関するいくつかの調査を行いましたが、すべてがlibgdxライフサイクルによって動いている間に実行時にそれを行う適切な方法を見つけることはできません。

デルタを使用してアニメーションを切り替えているがわかりましたが、これは単なるトリックです。実行時にその切り替えの決定をパイロットする必要があります。

ありがとうございます!

public class MainChar extends Actor { 

    private float showTime; 
    private Animation<TextureRegion> animation; 
    private boolean state = false; 
    private long longCounter = 0; 

    public BitmapFont font; 
    public MainChar(Animation animation){ 

     font = new BitmapFont(); 
     this.animation = animation; 
    } 

    public void setAnimation(Animation animation){ 
     this.animation=animation; 
    } 

    float myDelta; 
    @Override 
    public void act(float delta){ 
     super.act(delta); 
     myDelta = delta; 
     showTime += delta; 
    } 

    @Override 
    public void draw(Batch batch, float parentAlpha){ 

     super.draw(batch, parentAlpha); 
     batch.draw(animation2.getKeyFrame(showTime), 0, 0); 
    } 
} 

があなたのMainCharオブジェクトの参照を保持し、実行時にアニメーションを変更:

+0

このコードは動作しませんか?あなたのコードの現在の出力は何ですか? – Aryan

+0

コーデが機能し、最初のアニメーションが完全に表示され、しばらくしてから切り替わります。私がデルタを使ってアニメーションを切り替えているのに気がついているのですが、ただのトリックだと思うので、実行時にそのスイッチの決定をパイロットしたいと思います。 –

+0

あなたはMainChar俳優のアニメーションを変更しています。それ以外の場合は俳優の参照を保持し、俳優のアニメーションを画面から変更したいときはsetState()メソッドを呼び出します。 – Aryan

答えて

0

あなたはこの方法で試すことができます。

TextureAtlas texture = new TextureAtlas(Gdx.files.internal("actors/MainChar/a.pack")); 
Animation animation = new Animation<TextureRegion>(1/5f, texture.getRegions()); 

private TextureAtlas textureFF = new TextureAtlas(Gdx.files.internal("actors/MainChar/b.pack")); 
Animation animation2 = new Animation<TextureRegion>(1/7f, textureFF.getRegions()); 
... 
... 
create number of Animation in your screen 


MainChar mainChar=new MainChar(animation); 
stage.addActor(mainChar); 

変更アニメーション

mainChar.setAnimation(animation2); 
関連する問題