Reputation: 49
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;
}
Upvotes: 0
Views: 545
Reputation: 13321
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
Upvotes: 1