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

时间:2017-03-04 20:29:02

标签: java android libgdx actor

我对同一个Actor对象有不同的动画。

我想通过从主游戏Screen类传递一个常量来切换它们。

示例这是我的主要演员:

var arr= '5632 Hello 545 Thing 6532 Have'.match(/\d+ \w+/g)
var pairs= new Array()
arr.forEach(function(v) {
   pair = v.split(/\s/g);
   pairs.push({number: pair[0], name: pair[1]})
})

console.log('number:', pairs[0]['number'], 'name:',pairs[0]['name'])

// Output:
// number: 5632 name: Hello

这是调用类:

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

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

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

谢谢!

1 个答案:

答案 0 :(得分:0)

您可以尝试这种方式:

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