在 Libgdx 中重复序列操作
Repeat a Sequence Action in Libgdx
我有一个名为 arrow 的演员,我想对它重复一个序列动作。
此箭头指向一个演员,如果单击该演员,箭头应淡出。
这是我的代码:
Action moving = Actions.sequence(
(Actions.moveTo(arrow.getX(), arrow.getY() - 35, 1)),
(Actions.moveTo(arrow.getX(), arrow.getY(), 1)));
arrow.addAction(moving);
actor.addListener(new ClickListener() {
@Override
public void clicked(InputEvent event, float x, float y) {
arrow.addAction(Actions.fadeOut(1));
}
});
代码工作正常,但我想重复 'moving' 操作直到演员被点击。
我在这个问题中读到了 RepeatAction 但我不知道如何申请
在这种情况下,您可以使用 RepeatAction,Actions.forever()
:
final Action moving = Actions.forever(Actions.sequence(
(Actions.moveTo(arrow.getX(), arrow.getY() - 35, 1)),
(Actions.moveTo(arrow.getX(), arrow.getY(), 1))));
arrow.addAction(moving);
actor.addListener(new ClickListener() {
@Override
public void clicked(InputEvent event, float x, float y) {
// you can remove moving action here
arrow.removeAction(moving);
arrow.addAction(Actions.fadeOut(1f));
}
});
如果要在淡出后从Stage
中删除arrow
,可以使用RunnableAction
:
arrow.addAction(Actions.sequence(
Actions.fadeOut(1f), Actions.run(new Runnable() {
@Override
public void run() {
arrow.remove();
}
}))
);
我有一个名为 arrow 的演员,我想对它重复一个序列动作。
此箭头指向一个演员,如果单击该演员,箭头应淡出。
这是我的代码:
Action moving = Actions.sequence(
(Actions.moveTo(arrow.getX(), arrow.getY() - 35, 1)),
(Actions.moveTo(arrow.getX(), arrow.getY(), 1)));
arrow.addAction(moving);
actor.addListener(new ClickListener() {
@Override
public void clicked(InputEvent event, float x, float y) {
arrow.addAction(Actions.fadeOut(1));
}
});
代码工作正常,但我想重复 'moving' 操作直到演员被点击。
我在这个问题中读到了 RepeatAction
在这种情况下,您可以使用 RepeatAction,Actions.forever()
:
final Action moving = Actions.forever(Actions.sequence(
(Actions.moveTo(arrow.getX(), arrow.getY() - 35, 1)),
(Actions.moveTo(arrow.getX(), arrow.getY(), 1))));
arrow.addAction(moving);
actor.addListener(new ClickListener() {
@Override
public void clicked(InputEvent event, float x, float y) {
// you can remove moving action here
arrow.removeAction(moving);
arrow.addAction(Actions.fadeOut(1f));
}
});
如果要在淡出后从Stage
中删除arrow
,可以使用RunnableAction
:
arrow.addAction(Actions.sequence(
Actions.fadeOut(1f), Actions.run(new Runnable() {
@Override
public void run() {
arrow.remove();
}
}))
);