Android AndEngine анимированный спрайт некорректно анимирует - PullRequest
0 голосов
/ 04 февраля 2012

У меня есть анимированный спрайт с именем player, который я изменяю анимацию и направление использования onscreencontrol.Анимация, кажется, застревает и / или повторяет первый кадр, не проходя полный цикл анимации.Я подозреваю, что обработчик обновлений onscreencontrol сбрасывает анимацию до того, как успеет пройти все шаги?Я не уверен, что здесь не так, похоже, это должно работать:

final AnalogOnScreenControl velocityOnScreenControl = new AnalogOnScreenControl(x1, y1, this.BoundChaseCamera,this.ScreenControlBaseTextureRegion,this.ScreenControlKnobTextureRegion, 0.1f,new IAnalogOnScreenControlListener() {
    @Override
    public void onControlChange(
    final BaseOnScreenControl pBaseOnScreenControl,
    final float pValueX, final float pValueY) {
    /* player position */
    float spotX = player.getX() + 1 * TILE_WIDTH * pValueX;
    float spotY = player.getY() + 1 * TILE_HEIGHT * pValueY;
    /* if player position within bounds of map */
        if (spotX < TMXMapLayer.getWidth()
        && spotY < TMXMapLayer.getHeight()
        && spotX >= 0 && spotY >= 0) {
        /* Set player velocity */
final Vector2 velocity = Vector2Pool.obtain(pValueX * 3, pValueY * 3);
PlayerBody.setLinearVelocity(velocity);
Vector2Pool.recycle(velocity);
/* Math to determine the direction of movement */
double dirDouble = (Math.atan2(pValueX, pValueY)
    / (Math.PI / 2) + 2);
float direction = (int) Math.round(dirDouble)
    % DirectionState;
       /* if movement is N,E,S or W, do animation */
    if (direction == 0) { // If Go North
    player.animate(new long[] { 200, 200, 200 }, 0,2, true); // north

    } else if (direction == 1) { // If Go West
    player.animate(new long[] { 200, 200, 200 }, 9,11, true); // west

    } else if (direction == 2) { // If Go South
    player.animate(new long[] { 200, 200, 200 }, 6,8, true); // south

    } else if (direction == 3) { // If Go East
    player.animate(new long[] { 200, 200, 200 }, 3,5, true); // east
    }   
       }
    }

1 Ответ

3 голосов
/ 10 февраля 2012

Вы сбрасываете анимацию при каждом обновлении с помощью этого кода

if (direction == 0) { // If Go North
player.animate(new long[] { 200, 200, 200 }, 0,2, true); // north

} else if (direction == 1) { // If Go West
player.animate(new long[] { 200, 200, 200 }, 9,11, true); // west

} else if (direction == 2) { // If Go South
player.animate(new long[] { 200, 200, 200 }, 6,8, true); // south

} else if (direction == 3) { // If Go East
player.animate(new long[] { 200, 200, 200 }, 3,5, true); // east
}

вы не проверяете, изменилось ли направление, чтобы исправить это, вы должны добавить внешнее, если проверить направление изменения. Ваш код должен быть похож на

if(direction != lastDirection){
    lastDirection = direction;
    if (direction == 0) { // If Go North
    player.animate(new long[] { 200, 200, 200 }, 0,2, true); // north

    } else if (direction == 1) { // If Go West
    player.animate(new long[] { 200, 200, 200 }, 9,11, true); // west

    } else if (direction == 2) { // If Go South
    player.animate(new long[] { 200, 200, 200 }, 6,8, true); // south

    } else if (direction == 3) { // If Go East
    player.animate(new long[] { 200, 200, 200 }, 3,5, true); // east
    }
}

переменная lastDirection должна быть внешней для вас в области видимости ControlChange

...