Libgdx - 处理同一 Actor 的不同动画

Libgdx - Handling different animation of same Actor

我为同一个 Actor 对象制作了不同的动画。

我想通过从主游戏屏幕传递常量来在它们之间切换 class。

例如这是我的主要演员:

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

这是调用 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();
    }
}

我删掉了不那么有趣的部分... 由于我是初学者,我已经对 libgdx 进行了一些研究,但无法在运行时找到正确的方法,因为一切都在 libgdx 生命周期中移动。

如果您注意到我正在使用 delta 来切换动画,但这只是一个技巧,我想在运行时试验该切换决定。

谢谢!

你可以这样试试:

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 对象的引用并在 运行 时间更改动画。

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