java - Libgdx 将一个演员的动作传递给另一个演员
问题描述
好的,所以基本上我正在尝试用其他动作装饰一个 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 类的动作(也包含在 SequenceAction 中)不适用于 Shake 的实例。我确信动作会正确传递,因为我可以将它们打印出来。但是我没有得到综合效果,也许你们中的一些人会知道为什么?谢谢
编辑:(基于@DHa 的新回复)
我相信我已经理解了您提出的这个 Group-Workaround。但是我仍然无法使其工作。对于这个实例,假设我们先用 Shake 动作装饰按钮对象,然后使用 FadeOut 动作(这两个类都有从父类 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
}
}
我不知道为什么,但仍然只有一个动作(淡出)应用于创建的对象
解决方案
使用 Group Actor 组合多个 Actor。应用于该组的操作将应用于该组中的所有参与者。
Group group = new Group();
group.addActor(actor1);
group.addActor(actor2);
group.addAction(...);
但是,一个演员只能是一个组的一部分,因此您不能在不同演员之间混合和匹配动作。
例如:
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 - 我如何使用 Java 流库来简化它?
- arrays - 在 groovy 中从 Json 中的索引中过滤/选择数组值
- codeigniter-4 - 如何在 Codeigniter4 的视图中使用库
- c - 为什么 gcc 给我 OPENCV 3.4 的 math.wrap 错误
- python - 如何处理驱动器 api 的最大导出限制大小文件
- python - Matplotlib - 绘制超过 10 种颜色的线条
- python - 假设是简单的 Django slug 类别,但我得到一个 404,我不知道为什么
- shibboleth - 不同 SP 的多个 attribute-resolver.xml
- c# - 如何用简单的代码建立 XPO 一对一的关系?
- javascript - 使用 jQuery 或 JS (owl-carousel) 给一个项目另一个项目的孩子的类