LibGDX Actor不会绘制Actions

时间:2016-08-18 22:19:01

标签: java libgdx scene2d

我有以下LibGDX Actor子类,它似乎不执行任何可视化Action,但它执行延迟的runnables(我可以在预期的延迟后看到所有Gdx.app.log消息)。

我在被覆盖的super.act(delta)方法中调用TileActor.act,我也在stage.act(); stage.draw()类中调用Stage

这些演员正确显示,所以唯一缺少的是缩放/淡化的视觉动作。

我该如何调试?

class TileActor extends Actor {
  private TileLogic logic;
  private boolean flipped;
  private enum AnimationState {
    IDLE,
    SCHEDULED,
    PROGRESS,
    COMPLETED
  }
  private AnimationState flippedAnimation;

  private float FLIP_DURATION = 1.05f;

  TileActor(TileLogic logic, BoardRenderer renderer) {
    this.logic = logic;
    flipped = logic.isFlipped();
    flippedAnimation = AnimationState.IDLE;
    column = logic.column;
    row = logic.row;
  }

  void place() {
    setPosition(calculateX(logic.column), calculateY(logic.row));
  }

  private float calculateX(int column) {
    return column * (getWidth() + renderer.TILE_GAP) + renderer.boardXPosition;
  }

  private float calculateY(int row) {
    return (renderer.manager.rows - row - 1) * (getHeight() + renderer.TILE_GAP) + renderer.boardYPosition;
  }

  @Override
  public void draw(Batch batch, float parentAlpha) {
    batch.draw(flipped ? front : back, calculateX(logic.column), calculateY(logic.row), getWidth(), getHeight());
  }

  @Override
  public void act(float delta) {
    super.act(delta);
    if (flippedAnimation == AnimationState.IDLE && flipped != logic.isFlipped()) {
      flippedAnimation = AnimationState.SCHEDULED;
    }
    if (flippedAnimation == AnimationState.SCHEDULED) {
      playFlipAnimation();
    }
    if (flippedAnimation == AnimationState.COMPLETED) {
      flippedAnimation = AnimationState.IDLE;
      Gdx.app.log("TileActor.act", logic.value + (flippedAnimation.name()));
    }
  }

  private void playFlipAnimation() {
    flippedAnimation = AnimationState.PROGRESS;
    Gdx.app.log("TileActor.playFlipAnimation", logic.value + (flippedAnimation.name()));
    addAction(sequence(
      parallel(// This does not happen, at least I can see no scale or fade animation
        scaleTo(1, 0, FLIP_DURATION, Interpolation.pow5),
        fadeOut(FLIP_DURATION, Interpolation.pow5)
      ),
      new Action() {// This is executed as expected, I can see the log message
        @Override
        public boolean act(float delta) {
          flipped = logic.isFlipped();
          Gdx.app.log("TileActor.playFlipAnimation", logic.value + (flipped ? " " : " NOT ") + " flipped");
          return true;
        }
      },
      parallel(// This does not happen, at least I can see no scale or fade animation
        scaleTo(1, 1, FLIP_DURATION, Interpolation.pow5),
        fadeIn(FLIP_DURATION, Interpolation.pow5)
      ),
      new Action() {// This is executed as expected, I can see the log message after the delay
        @Override
        public boolean act(float delta) {
          flippedAnimation = AnimationState.COMPLETED;
          Gdx.app.log("TileActor.playFlipAnimation", logic.value + (flippedAnimation.name()));
          return true;
        }
      }
    ));
  }
}

解决方案:

我的draw方法必须处理颜色更改并传递比例值,如下所示:

@Override
public void draw(Batch batch, float parentAlpha) {
  final Color color = getColor();
  batch.setColor(color.r, color.g, color.b, color.a * parentAlpha);
  batch.draw(flipped ? front : back,
    calculateX(logic.column), calculateY(logic.row),
    getOriginX(), getOriginY(),
    getWidth(), getHeight(),
    getScaleX(), getScaleY(),
    getRotation());
}

1 个答案:

答案 0 :(得分:1)

FadeActions和ColorActions修改了Actor的Color对象。绘图时必须将此颜色应用于批处理。

@Override
public void draw(Batch batch, float parentAlpha) {
    Color color = getColor();
    batch.setColor(color.r, color.g, color.b, color.a * parentAlpha);
    batch.draw(flipped ? front : back, calculateX(logic.column), calculateY(logic.row), getWidth(), getHeight());
}