0

I am working on an Android project using AndEngine. I have a working animated sprite that moves to a touched location on the screen. My issue is I can't seem to figure out how to stop the animation once the sprite reaches the destination. Here is my code for the movement and animation of the sprite. Thanks in advance for any help.

Edit New Code---

@Override
public boolean onSceneTouchEvent (Scene m_Scene, TouchEvent pSceneTouchEvent)   {
    if (pSceneTouchEvent.getAction() == TouchEvent.ACTION_UP) {
        float touchX = pSceneTouchEvent.getX();
        float touchY = pSceneTouchEvent.getY();
        float[] minerLoc = sprMiner.getSceneCenterCoordinates();
        float minerX = minerLoc[0];
        float minerY = minerLoc[1];

        MoveModifier sprModifier = new MoveModifier(5, minerX, touchX, minerY, touchY)
        {
                protected void onModifierStarted(IEntity pItem)
                {
                        super.onModifierStarted(sprMiner);
                        // Start Walking Animation
                        sprMiner.animate(new long[] {150, 150, 150, 150, 150, 150, 150, 150}, 0, 7, true);
                }

                protected void onModifierFinished(IEntity pItem)
                {
                        super.onModifierFinished(sprMiner);
                        //Stop Walking Animation
                        sprMiner.stopAnimation(0);
                }
        };
        sprMiner.registerEntityModifier(sprModifier);
    }

    return false;
}
IrishCarBomb
  • 49
  • 1
  • 2
  • 11

1 Answers1

1

Modifier listeners:

Sometimes you will need to execute certain code in particular moment, like on starting or finishing modifier, to do it, you simply have to override some methods while creating new modifier. To do it, follow methods used below:

RotationModifier yourModifier = new RotationModifier(3, 0, 360)
{
        @Override
        protected void onModifierStarted(IEntity pItem)
        {
                super.onModifierStarted(pItem);
                // Your action after starting modifier
        }

        @Override
        protected void onModifierFinished(IEntity pItem)
        {
                super.onModifierFinished(pItem);
                // Your action after finishing modifier
                //STOP ANIMATION HERE!!!
        }
};

yourEntity.registerEntityModifier(yourModifier);

Source: http://www.matim-dev.com/entity-modifiers.html

LordRaydenMK
  • 13,074
  • 5
  • 50
  • 56
  • Dude! That is awesome. I was looking for the right way to do this. This makes so much more sense to me now. I am so used to writing application code, game logic is a little foreign to me yet. Thank you so much. – IrishCarBomb Jun 11 '14 at 23:49
  • check out the blog, there are a lot of useful articles. And good luck with your game – LordRaydenMK Jun 12 '14 at 06:49
  • One last question. I have redone the code to include your suggestion,I'm just not sure how to trigger the onModifierStarted and Finished. update - is it with the entitymodifierlistener? – IrishCarBomb Jun 12 '14 at 16:37
  • Got it working. The only thing I have now is the sprite jumps a bit at the beginning of the move and finishes slightly off from where the actual screen touch was. Almost as if the coordinates the code is getting are slightly off from the actual scene. Any thoughts? – IrishCarBomb Jun 12 '14 at 20:17