Ian
Ian

Reputation: 900

andengine pathmodifier velocity

I have the following code for a sprite to follow a path using Andengine. I need to change the speed (velocity) of the sprites moving along the path(). Does anyone know how to vary the velocity along a path?

private void follow_path5(final AnimatedSprite tSprite,int i) {
    final Path path = new Path(5)
    .to(20, startY)
    .to(416.0f, startY)
    .to(20.0f, startY)
    .to(416.0f,startY)
    .to(20.0f,startY);

    // Add the proper animation when a waypoint of the path is passed.
    tSprite.registerEntityModifier(new PathModifier(30.0f, path, null, new IPathModifierListener() {
        @Override
        public void onPathStarted(final PathModifier pPathModifier, final IEntity pEntity) {

        }

        @Override
        public void onPathWaypointStarted(final PathModifier pPathModifier, final IEntity pEntity, final int pWaypointIndex) {

            switch(pWaypointIndex) {
            case 0: case 1: case 2: case 3:
                final long[] frameDurations = new long[3];
                Arrays.fill(frameDurations, 500);
                tSprite.animate(frameDurations, 2, 4, true);

                break;

            case 4: case 5: case 6: case 7:
                final long[] frameDurations1 = new long[3];
                Arrays.fill(frameDurations1, 500);
                tSprite.animate(frameDurations1, 5, 7, true);

                break;

            case 8: case 9: case 10: case 11:
                final long[] frameDurations2 = new long[3];
                Arrays.fill(frameDurations2, 500);
                tSprite.animate(frameDurations2, 2, 4, true);

                break;

            case 12: case 13: case 14:
                final long[] frameDurations3 = new long[3];
                Arrays.fill(frameDurations3, 500);
                tSprite.animate(frameDurations3, 5, 7, true);

                break;
            }
        }

        @Override
        public void onPathWaypointFinished(final PathModifier pPathModifier, final IEntity pEntity, final int pWaypointIndex) {

        }

        @Override
        public void onPathFinished(final PathModifier pPathModifier, final IEntity pEntity) {
        }}));
    scene.attachChild(asprVamp[i]);
}

Upvotes: 2

Views: 2173

Answers (3)

Mustafa
Mustafa

Reputation: 10413

You can use SequenceEntityModifier to combine different entity modifiers into one, which will fulfill your request since each PathModifier can have different time lengths.

    Path a = new Path(2).to(0, 0).to(400,0);
    Path b = new Path(2).to(400, 0).to(800, 0);

    IEntityModifier[] modifier = new IEntityModifier[2];
    modifier[0] = new PathModifier(3, a);
    modifier[1] = new new PathModifier(1, b);

    IEntityModifier entMod = new SequenceEntityModifier(modifier);
    entity.registerEntityModifier(entMod);
    scene.attachChild(entity);

Upvotes: 2

TomBerlin
TomBerlin

Reputation: 11

My solution for a similar problem was deriving from the original modifier and adding a factor to the time passed:

@Override
public float onUpdate(float pSecondsElapsed, IEntity pItem) {
    return super.onUpdate(pSecondsElapsed*frequency, pItem);
}

Upvotes: 1

Jong
Jong

Reputation: 9125

You can only give the duraion of the move, 1 per path modifier. So, if you want different speeds at different path points, you'll have to create different path modifiers.

Upvotes: 2

Related Questions