Android AndEngine 动画精灵动画不正确

发布于 2025-01-02 17:45:33 字数 1789 浏览 2 评论 0原文

我有一个名为播放器的动画精灵,我可以使用屏幕控件更改动画和方向。动画似乎卡住和/或重复第一帧,而没有经历完整的动画周期。我怀疑屏幕控制更新处理程序在有时间完成所有步骤之前正在重置动画?我不确定这里出了什么问题,这似乎应该有效:

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
    }   
       }
    }

I've got an animated sprite named player that I change the animation and direction of using an onscreencontrol. The animation seems to get stuck and/or repeat the first frame without ever going through a complete animation cycle. I suspect the onscreencontrol updatehandler is resetting the animation before it has time to go through all the steps? I'm not sure whats wrong here, this seems like it should work:

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
    }   
       }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

断肠人 2025-01-09 17:45:33

您使用此代码在每次更新时重置动画,

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范围之外

You're resetting the animation on every update with this code

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
}

you're not verifying if the direction has changed, to correct this you should add an external if to check if the direction has changed. Your code should be like

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
    }
}

the variable lastDirection should be external to you onControlChange scope

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文