好吧基本上我正在尝试用其他动作装饰一个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的新回复)
我相信我已理解您提出的这个小组 - 解决方法。但是我仍然无法使其成功。对于这个实例,我们假设我们使用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
}
}
我不知道为什么但仍然只有一个动作(淡出)应用于创建的对象
答案 0 :(得分:1)
每个动作都有一个 actor目标,因此不应该为多个actor分配一个相同的动作。您必须复制操作才能达到预期效果。
将动作分配给第二个actor时应该发生的事情是动作目标切换到第二个actor,而不是两个actor现在都是动作的目标。
<强>演员强>
public void addAction (Action action) {
action.setActor(this);
<强>动作强>
public void setActor (Actor actor) {
this.actor = actor;
if (target == null) setTarget(actor);
MoveByAction (您正在使用的那个)
protected void updateRelative (float percentDelta) {
target.moveBy(amountX * percentDelta, amountY * percentDelta);
}
回复编辑:
新的解决方案包装动作而不是复制它,与原始解决方案相比没什么区别,动作一次不能在多个目标上使用。基本问题是演员没有有行动(簿记除外),行动有他们所采取行动的单一目标。
我可以看到一些方法(按照我的推荐顺序):