好吧,基本上我正在尝试用其他 Action 装饰 Libgdx Actor 类
public Shake(Button buttonToBeDecorated) extends ButtonDecorator {
super(buttonToBeDecorated);
Array<Action> actions = buttonToBeDecorated.getActions();
for (Action action : actions)
addAction(action);
addAction(Actions.forever(new SequenceAction(
Actions.moveBy(10, 0, 0.5f),
Actions.moveBy(-10, 0, 0.5f)))
);
}
但是来自 toBeDecorated 类的 Action (也包含在 SequenceAction 中)不适用于 Shake 的实例。我确定操作已正确通过,因为我能够将它们打印出来。但我没有得到综合效果,也许你们中的一些人会知道为什么?谢谢
编辑:(基于@DHa 的新回复)
我相信我已经理解了您提出的这个 Group-Workaround。但是我仍然无法让它发挥作用。对于这个实例,假设我们用 Shake Action 装饰按钮对象,然后用 FadeOut Action 装饰按钮对象(这两个类都有从父类 ButtonDecorator 扩展的“Group”变量)。所以创建这种类型的对象看起来像这样:
Button button = new Decorators.FadeOut(new Decorators.Shake(new Buttons.PlayButton()));
和类:
//Shake class - we just simply add Shake actor to group and then add a specific action
//this works perfectly fine by itself - new Decorators.Shake(new Buttons.PlayButton())
public static class Shake extends ButtonDecorator {
public Shake(Button buttonToBeDecorated) {
super(buttonToBeDecorated);
group.addActor(this);
group.addAction(Actions.forever(new SequenceAction(
Actions.moveBy(10, 0, 0.5f),
Actions.moveBy(-10, 0, 0.5f))));
}
}
//In FadeOut we are trying to decorate Shake object with another Action
public static class FadeOut extends ButtonDecorator {
public FadeOut(Button buttonToBeDecorated) {
super(buttonToBeDecorated);
Array<Action> actions = buttonToBeDecorated.group.getActions(); //getting actions from Shake
group.addActor(buttonToBeDecorated);
/* I'm guessing that the whole workaround is in this line. We are adding
Shake-actor to FadeOut group so Shake-actions should no longer apply
to Shake-object and can be applied to our new FadeOut button */
group.addActor(this); //Adding FadeOut to it's own group
for (Action action : actions)
group.addAction(Actions.parallel(action,new SequenceAction(Actions.fadeOut(3), Actions.fadeIn(3))))
//besides adding shake actions to FadeOut object we are also adding parallel fadeout action
}
}
我不知道为什么,但仍然只有一个 Action (淡出)应用于创建的对象
最佳答案
使用 Group actor 组合多个 actor。应用于该组的操作将应用于该组中的所有参与者。
Group group = new Group();
group.addActor(actor1);
group.addActor(actor2);
group.addAction(...);
但是,一个 Actor 只能是一个组的一部分,因此您不能混合和匹配不同 Actor 之间的 Action 。
例如:
Group group1 = new Group();
group1.addActor(actor1);
group1.addActor(actor2);
Group group = new Group();
group2.addActor(actor2);
group2.addActor(actor3);
group1.addAction(...); // will only apply to actor1 since actor2 left group1 when joining group2
我想出一个比我以前的答案更好的答案,另一个答案在技术上仍然是正确的,所以我也会保留它。
关于java - Libgdx 将一个 Actor 的 Action 传递给另一个 Actor ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50048918/