Queue multiple entity modifiers in AndEngine

跟風遠走 提交于 2019-12-10 10:28:50

问题


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. The second MoveModifier doesn't have any effect. What am I doing wrong here?


回答1:


I'm not sure why your code is not working, but I think you can achieve the same thing using the SequenceEntityModifier:

scene.attachChild(pointer);

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



回答2:


My first guess woud be, that the code after 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. 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



来源:https://stackoverflow.com/questions/11170486/queue-multiple-entity-modifiers-in-andengine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!