我正在尝试循环ColorAction动画。 这是我的代码:
ColorAction action = new ColorAction();
ShapeRenderer shapeRenderer;
@Override
public void create () {
shapeRenderer = new ShapeRenderer();
action.setColor(Color.BLUE);
action.setEndColor(Color.GOLD);
action.setDuration(5);
}
@Override
public void render () {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
int fps = Gdx.graphics.getFramesPerSecond();
curFrame++;
if (curFrame == fps*5) {
action.restart();
curFrame = 0;
}
action.act(Gdx.graphics.getDeltaTime());
shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
shapeRenderer.setColor(action.getColor());
shapeRenderer.rect(100, 100, 40, 40);
shapeRenderer.end();
}
但这不能正常运作。它只是播放动画一次并停止。 有人可以解释一下我做错了什么吗?感谢。
经过几次改变后:
ColorAction actionBtG = new ColorAction();
ColorAction actionGtB = new ColorAction();
SequenceAction sequenceAction;
RepeatAction repeatAction = new RepeatAction();
ShapeRenderer shapeRenderer;
Color blue = new Color(Color.BLUE);
@Override
public void create () {
shapeRenderer = new ShapeRenderer();
actionBtG.setColor(blue);
actionBtG.setEndColor(Color.GOLD);
actionBtG.setDuration(5);
actionGtB.setColor(blue);
actionGtB.setEndColor(Color.BLUE);
actionGtB.setDuration(5);
sequenceAction = new sequenceAction(actionBtG,actionGtB);
repeatAction.setAction(sequenceAction);
repeatAction.setCount(RepeatAction.FOREVER);
}
@Override
public void render () {
repeatAction.act(Gdx.graphics.getDeltaTime());
Gdx.gl.glClearColor(1,1,1,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
shapeRenderer.setColor(blue);
shapeRenderer.rect(100, 100, 40, 40);
shapeRenderer.end();
}
答案 0 :(得分:0)
首先,对setColor
的调用告诉它要修改哪个Color实例,所以你不想传递像Color.BLUE这样的伪常量,否则你将修改&# 34;恒定"本身。 (Java不具有不可变对象。)您需要创建自己的Color实例以与该操作一起使用并将其传递。
myColor = new Color(Color.BLUE);
要进行颜色循环,您需要两个ColorAction(您在两种颜色之间切换一个),它们需要一起处于SequenceAction中。它们都应该设置为相同的Color实例。然后,如果您希望它循环多次,请将SequenceAction包装在RepeatAction中。
答案 1 :(得分:0)
这意味着你:尝试再次设置颜色,你将重新开始行动!
编辑: 我现在用自己的类替换了ColorAction:
public class ColorTransitionAnimation extends TemporalAction {
private final Color start = new Color();
private final Color end = new Color();
private final Color animatedColor = new Color();
private RepeatType repeatType = RepeatType.REVERSE;
public enum RepeatType { REVERSE, RESTART }
public void setRepeatType(RepeatType repeatType) {
this.repeatType = repeatType;
}
protected void update (float percent) {
float r = start.r + (end.r - start.r) * percent;
float g = start.g + (end.g - start.g) * percent;
float b = start.b + (end.b - start.b) * percent;
float a = start.a + (end.a - start.a) * percent;
animatedColor.set(r, g, b, a);
}
@Override
protected void end() {
super.end();
if(repeatType == RepeatType.REVERSE)
setReverse(!isReverse());
restart();
}
public Color getAnimationColor () {
return animatedColor;
}
public void setStartColor (Color color) {
start.set(color);
}
public void setEndColor (Color color) {
end.set(color);
}
}