【问题标题】:Libgdx - Handling different animation of same ActorLibgdx - 处理同一个 Actor 的不同动画
【发布时间】:2017-07-24 20:54:41
【问题描述】:

我对同一个 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 生命周期中移动。

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

谢谢!

【问题讨论】:

  • 这段代码不工作吗?如果不是,您的代码的当前输出是什么?
  • corde 工作正常,第一个动画完美显示,过了一会儿它切换。如果您注意到我正在使用 delta 切换动画,但这只是一个技巧,我想在运行时试行该切换决策。
  • 现在通过计数器,您正在更改 MainChar Actor 的动画,否则当您想从屏幕更改 Actor 的动画时,请保留您的 Actor 的引用并调用 setState() 方法。
  • setState 只接受布尔值...我不仅希望能够处理多个动画 2. 还有其他方法吗?我发现 Name 变量...应该用于此目的还是不好的做法?谢谢
  • 也许是 userObject....

标签: java android libgdx actor


【解决方案1】:

你可以这样试试:

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

【讨论】:

    猜你喜欢
    • 2014-09-18
    • 2017-01-22
    • 2018-06-04
    • 1970-01-01
    • 2017-12-07
    • 1970-01-01
    • 1970-01-01
    • 2017-02-21
    • 1970-01-01
    相关资源
    最近更新 更多