Search code examples
androidcoordinatesspriteandengine

How to move a sprite after the touch from one location to another with a smooth movement with andengine?


I've a sprite that on touch have to start move along the Y coord. I need to stop the movement when it reach a default Y coord. I've tried with setposition() but don't make a smooth movement (like an animation) How can I do this smooth?

I've try the setvelocity() but I don't know how to stop the movement at a certain coordinates.

          @Override
            public boolean onAreaTouched(final TouchEvent pSceneTouchEvent,
                    final float pTouchAreaLocalX, final float pTouchAreaLocalY) {

                if(pSceneTouchEvent.isActionDown()){
                    //TOCCO DELLO SPRITE

                    PhysicsHandler physicsHandler = new PhysicsHandler(this);
                    this.registerUpdateHandler(physicsHandler);
                    physicsHandler.setVelocityY(-5f);



                    se.getChild(1).detachChild(this);
                    se.getChild(2).attachChild(this);


                }

I also tried the path but I did not understand very well how it works.

EDIT

according to Jong I've used the MoveYModifier. It works but on the avd the movement seems jerky. I'll try on a device soon, but is only an avd problem? I've done this way:

   if(pSceneTouchEvent.isActionDown()){

                    //TOCCO DELLO SPRITE

                    se.getChild(1).detachChild(this);
                    se.getChild(2).attachChild(this);


                    MoveYModifier mod = new MoveYModifier(3, getY(), pSceneTouchEvent.getY()- gap);
                    this.registerEntityModifier(mod);


                }

EDIT 2 tested on a device and works well! very smooth!


Solution

  • Use aMoveModifier, read more here.