LibGDX Actor 不会绘制 Actions
LibGDX Actor won't draw Actions
我有以下 LibGDX Actor subclass,它似乎没有执行任何视觉操作,但它执行延迟的可运行程序(我可以在之后看到所有 Gdx.app.log 消息预期的延迟)。
我在重写的 TileActor.act
方法中调用 super.act(delta)
,我还在 Stage
class 中调用 stage.act(); stage.draw()
。
这些演员显示正常,所以唯一缺少的是 scaling/fading 的视觉动作。
我该如何调试它?
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());
}
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());
}
我有以下 LibGDX Actor subclass,它似乎没有执行任何视觉操作,但它执行延迟的可运行程序(我可以在之后看到所有 Gdx.app.log 消息预期的延迟)。
我在重写的 TileActor.act
方法中调用 super.act(delta)
,我还在 Stage
class 中调用 stage.act(); stage.draw()
。
这些演员显示正常,所以唯一缺少的是 scaling/fading 的视觉动作。
我该如何调试它?
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());
}
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());
}