1

I am trying to make a sprite move using MoveByModifier in Andengine. I want it to keep repeating after the modifier has finished but i am unable to achieve that. Following is my code if anyone can help...

MoveByModifier mod1 = new MoveByModifier(20, 150, 400){
        @Override
        protected void onModifierFinished(IEntity pItem) {
            super.reset(); 
        }
    };

Sprite.registerEntityModifier(mod1);
mMainScene.attachChild(Sprite);

I also tried LoopEntityModifier but still it does not repeat... I cant understand why?

Sprite.registerEntityModifier(new LoopEntityModifier(mod1));
Swati Rawat
  • 1,729
  • 1
  • 19
  • 34

2 Answers2

1

If you want repeating moving sprite from start position, you can use

MoveModifier mod1 = new MoveModifier(20,Sprite.getX(), Sprite.getY(), Sprite.getX()+150, Sprite.getY()+400);
Sprite.registerEntityModifier(new LoopEntityModifier(mod1));

Repeating MoveByModifier continues moving from current coordinates.

Alexey
  • 714
  • 8
  • 21
  • The syntax of the move modifier u have given is incorrect. Its MoveModifier(duration, fromX, toX, fromY, toY).. anyway it helped me.. thanks – Swati Rawat May 26 '13 at 09:13
  • `public MoveModifier(final float pDuration, final float pFromX, final float pFromY, final float pToX, final float pToY)` https://github.com/nicolasgramlich/AndEngine/blob/GLES2-AnchorCenter/src/org/andengine/entity/modifier/MoveModifier.java – Alexey May 26 '13 at 09:15
  • `public MoveModifier(final float pDuration, final float pFromX, final float pToX, final float pFromY, final float pToY)` in old vesion AndEngine – Alexey May 26 '13 at 09:16
  • oh ok... the version i have... which is gles2 only has the type public MoveModifier(final float pDuration, final float pFromX, final float pToX, final float pFromY, final float pToY) – Swati Rawat May 26 '13 at 09:24
0

No need to reset the modifier after finish it.You have to remove super.reset(); inside onModifierFinished method. Because when you call this, it removes the applied modifier from the Sprite.

For repeating the sprite from old postion to new one, write like

  MoveModifier moveModifier= new MoveModifier(20,Sprite.getX(), Sprite.getY(), Sprite.getX()+150, Sprite.getY()+400);
Sprite.registerEntityModifier(new LoopEntityModifier(moveModifier));
Shihab Uddin
  • 6,699
  • 2
  • 59
  • 74