Search code examples
javaandroidlibgdxactor

Libgdx - Handling different animation of same Actor


I've different animation for the same Actor object.

I would like to switch between them by passing a constant from the main game Screen class.

Example this is my main Actor:

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

This is the calling class:

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

I cut out the less interesting parts... Since I'm a beginner I've made some research about libgdx but can't find the proper way to do that at runtime while everything is moving by the libgdx lifecycle.

If you notice I'm using delta to switch animation but it's just a trick, I want to pilot that switch decision at runtime.

Thanks!


Solution

  • You can try in this way :

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

    Keep reference of your MainChar object and change Animation at run time.

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

    Change Animation

     mainChar.setAnimation(animation2);