简体   繁体   English

在AndEngine中对多个实体修饰符进行排队

[英]Queue multiple entity modifiers in AndEngine

I'm trying to have a sprite ("pointer" below) along two paths, one after the other. 我试图沿两条路径一个接一个地有一个精灵(下面的“指针”)。 Here is my code: 这是我的代码:

    scene.attachChild(pointer);

    pointer.clearEntityModifiers();
    pointer.registerEntityModifier(new MoveModifier(
        1.0f, 540, 960, 1000, 1000,
        new IEntityModifierListener() {
            public void onModifierStarted(IModifier<IEntity> pModifier, IEntity pItem) {}
            public void onModifierFinished(IModifier<IEntity> pModifier, IEntity pItem) {
                clickSound.play();

                pointer.clearEntityModifiers();
                pointer.registerEntityModifier(new MoveModifier(
                    1.0f, pointer.getX(), pointer.getY(), 500, 2500,
                    new IEntityModifierListener() {
                        public void onModifierStarted(IModifier<IEntity> pModifier, IEntity pItem) {}
                        public void onModifierFinished(IModifier<IEntity> pModifier, IEntity pItem) {
                            pointer.clearEntityModifiers();
                            pointer.detachSelf();
                        }
                    },
                    EaseCubicInOut.getInstance()
                ));
            }
        },
        EaseCubicInOut.getInstance()
    ));

The pointer moves along the first path as expected, and then the clickSound plays, and then nothing happens. 指针按预期沿第一个路径移动,然后clickSound播放,然后没有任何反应。 The second MoveModifier doesn't have any effect. 第二个MoveModifier没有任何效果。 What am I doing wrong here? 我在这做错了什么?

I'm not sure why your code is not working, but I think you can achieve the same thing using the SequenceEntityModifier : 我不确定为什么你的代码不起作用,但我认为你可以使用SequenceEntityModifier实现相同的功能:

scene.attachChild(pointer);

    pointer.clearEntityModifiers();
    pointer.registerEntityModifier(new SequenceEntityModifier(
        new MoveModifier#1(...),
        new MoveModifier#2(...)));

My first guess woud be, that the code after pointer.clearEntityModifiers(); 我的第一个猜测是, pointer.clearEntityModifiers();后的代码pointer.clearEntityModifiers(); isn't executed anymore, since it unregisters the modifiers. 不再执行,因为它取消注册修饰符。 I am just thinking out loud here, so please tell me if I am wrong. 我只是在这里大声思考,所以请告诉我,如果我错了。 Maybe you can try logging something to see if the method returns after this line. 也许你可以尝试记录一些东西,看看该方法是否在此行之后返回。 Something like: 就像是:

pointer.clearEntityModifiers();
Log.v("EntityModifier", "the method continues...");

I always find it hard to manage internal class definitions, because of the state of the variables that are accessed from inside. 我总是发现很难管理内部类定义,因为从内部访问的变量的状态。 Like in your case the pointer . 就像在你的情况下pointer If nothing else works, you could still try to do it all in one run. 如果没有其他工作,你仍然可以尝试在一次运行中完成所有操作。 So instead of disposing the first modifier listener and registering another, create your own Listener, with a flag somewhere to tell it what to do and reuse that listener: 因此,不是处置第一个修饰符侦听器并注册另一个,而是创建自己的侦听器,在某处使用标志来告诉它要做什么并重用该侦听器:

public class MyModifierListener extends IEntityModifierListener{

    private Pointer pointer;   // declare your pointer, so you have a reference within the listener
    private boolean firstRun;  // a flag to check if it is the first time the modifier is used

    public MyModifierListener(Pointer pointer){
        super();
        this.pointer = pointer;  // init the pointer within the constructor
        this.firstRun = true;    // should be true the first time
    }

    public void onModifierStarted(IModifier<IEntity> pModifier, IEntity pItem) {
    }

    public void onModifierFinished(IModifier<IEntity> pModifier, IEntity pItem) {
        if(firstRun){
             clickSound.play();
         firstRun=false;
             pointer.registerEntityModifier(new MoveModifier(1.0f, pointer.getX(), pointer.getY(), 500, 2500, this), EaseCubicInOut.getInstance());
        }else{
             pointer.detachSelf();
        }
    }
}

And to use it something like that: 并使用它类似的东西:

MyModifierListener myListener = new MyModifierListener(pointer);
pointer.registerEntityModifier(new MoveModifier(1.0f, 540, 960, 1000, 1000,     myListener), EaseCubicInOut.getInstance());

I couldn't test this, so these are just wild guesses. 我无法测试这个,所以这些只是猜测。 Tell me if you find something. 告诉我你是否找到了什么。

regards Christoph Christoph

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM