Android AndEngine动画精灵没有正确动画

时间:2012-02-04 00:13:15

标签: android animation sprite andengine

我有一个名为player的动画精灵,我改变动画和使用onscreencontrol的方向。动画似乎卡住和/或重复第一帧而没有经历完整的动画循环。我怀疑onscreencontrol updatehandler在有时间完成所有步骤之前重置动画?我不确定这里有什么问题,这似乎应该有效:

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 个答案:

答案 0 :(得分:3)

您正在使用此代码

重置每次更新时的动画
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以检查方向是否已更改。您的代码应该像

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应该在你的onControlChange范围

之外