1

我正在使用 Andengine 在 Android 上构建我的游戏,并使用 AnimatedSprite 来绘制一个正在运行的忍者。我正在使用 changeFrameDuration 方法根据忍者移动的速度来修改动画速度。

我不知道为什么动画有时非常快,而根据我设置的帧持续时间值它应该很慢。

我打印了日志,看看为什么我的忍者动画速度非常快,而当前的瓷砖索引告诉了一些有趣的事情......

** Here the ninja is animated very slowly like supposed **
06-17 23:03:08.868: I/Ninja(15495): Animation frame duration : 1639 with current tile index : 0
06-17 23:03:08.888: I/Ninja(15495): Animation frame duration : 2048 with current tile index : 0
06-17 23:03:08.908: I/Ninja(15495): Animation frame duration : 2561 with current tile index : 0
06-17 23:03:08.928: I/Ninja(15495): Animation frame duration : 2561 with current tile index : 0
06-17 23:03:08.948: I/Ninja(15495): Animation frame duration : 2561 with current tile index : 0
06-17 23:03:08.968: I/Ninja(15495): Animation frame duration : 2561 with current tile index : 0
06-17 23:03:08.988: I/Ninja(15495): Animation frame duration : 2561 with current tile index : 0
06-17 23:03:09.008: I/Ninja(15495): Animation frame duration : 2561 with current tile index : 0
06-17 23:03:09.028: I/Ninja(15495): Animation frame duration : 2561 with current tile index : 0
06-17 23:03:09.048: I/Ninja(15495): Animation frame duration : 2561 with current tile index : 0
06-17 23:03:09.068: I/Ninja(15495): Animation frame duration : 2561 with current tile index : 0
06-17 23:03:09.088: I/Ninja(15495): Animation frame duration : 2561 with current tile index : 0
06-17 23:03:09.108: I/Ninja(15495): Animation frame duration : 2561 with current tile index : 0
06-17 23:03:09.129: I/Ninja(15495): Animation frame duration : 2561 with current tile index : 0
06-17 23:03:09.149: I/Ninja(15495): Animation frame duration : 2561 with current tile index : 0
06-17 23:03:09.169: I/Ninja(15495): Animation frame duration : 2561 with current tile index : 0
06-17 23:03:09.189: I/Ninja(15495): Animation frame duration : 2561 with current tile index : 0
06-17 23:03:09.209: I/Ninja(15495): Animation frame duration : 2561 with current tile index : 0

** Here it starts behing animated very fast (when supposed to be not very fast) **
06-17 23:03:09.229: I/Ninja(15495): Animation frame duration : 741 with current tile index : 0
06-17 23:03:09.249: I/Ninja(15495): Animation frame duration : 521 with current tile index : 2
06-17 23:03:09.269: I/Ninja(15495): Animation frame duration : 421 with current tile index : 3
06-17 23:03:09.289: I/Ninja(15495): Animation frame duration : 361 with current tile index : 3
06-17 23:03:09.309: I/Ninja(15495): Animation frame duration : 320 with current tile index : 4
06-17 23:03:09.329: I/Ninja(15495): Animation frame duration : 290 with current tile index : 5
06-17 23:03:09.349: I/Ninja(15495): Animation frame duration : 266 with current tile index : 5
06-17 23:03:09.369: I/Ninja(15495): Animation frame duration : 248 with current tile index : 6
06-17 23:03:09.389: I/Ninja(15495): Animation frame duration : 232 with current tile index : 7
06-17 23:03:09.409: I/Ninja(15495): Animation frame duration : 220 with current tile index : 7
06-17 23:03:09.429: I/Ninja(15495): Animation frame duration : 208 with current tile index : 8
06-17 23:03:09.449: I/Ninja(15495): Animation frame duration : 199 with current tile index : 8
06-17 23:03:09.469: I/Ninja(15495): Animation frame duration : 190 with current tile index : 9
06-17 23:03:09.489: I/Ninja(15495): Animation frame duration : 183 with current tile index : 9
06-17 23:03:09.509: I/Ninja(15495): Animation frame duration : 176 with current tile index : 10
06-17 23:03:09.529: I/Ninja(15495): Animation frame duration : 170 with current tile index : 10
06-17 23:03:09.549: I/Ninja(15495): Animation frame duration : 165 with current tile index : 11
06-17 23:03:09.569: I/Ninja(15495): Animation frame duration : 160 with current tile index : 11

** Here it starts behing animated at a normal rate (slowly accelerating) **
06-17 23:03:09.589: I/Ninja(15495): Animation frame duration : 155 with current tile index : 0
06-17 23:03:09.609: I/Ninja(15495): Animation frame duration : 151 with current tile index : 0
06-17 23:03:09.629: I/Ninja(15495): Animation frame duration : 147 with current tile index : 0
06-17 23:03:09.649: I/Ninja(15495): Animation frame duration : 143 with current tile index : 0
06-17 23:03:09.669: I/Ninja(15495): Animation frame duration : 140 with current tile index : 0
06-17 23:03:09.689: I/Ninja(15495): Animation frame duration : 137 with current tile index : 0
06-17 23:03:09.709: I/Ninja(15495): Animation frame duration : 134 with current tile index : 1
06-17 23:03:09.729: I/Ninja(15495): Animation frame duration : 131 with current tile index : 1
06-17 23:03:09.749: I/Ninja(15495): Animation frame duration : 128 with current tile index : 1
06-17 23:03:09.769: I/Ninja(15495): Animation frame duration : 126 with current tile index : 1
06-17 23:03:09.789: I/Ninja(15495): Animation frame duration : 124 with current tile index : 1
06-17 23:03:09.809: I/Ninja(15495): Animation frame duration : 121 with current tile index : 1
06-17 23:03:09.829: I/Ninja(15495): Animation frame duration : 119 with current tile index : 2
06-17 23:03:09.849: I/Ninja(15495): Animation frame duration : 117 with current tile index : 2
06-17 23:03:09.869: I/Ninja(15495): Animation frame duration : 115 with current tile index : 2
06-17 23:03:09.889: I/Ninja(15495): Animation frame duration : 113 with current tile index : 2
06-17 23:03:09.909: I/Ninja(15495): Animation frame duration : 112 with current tile index : 3
06-17 23:03:09.929: I/Ninja(15495): Animation frame duration : 110 with current tile index : 3
06-17 23:03:09.949: I/Ninja(15495): Animation frame duration : 108 with current tile index : 3
06-17 23:03:09.969: I/Ninja(15495): Animation frame duration : 107 with current tile index : 3
06-17 23:03:09.989: I/Ninja(15495): Animation frame duration : 105 with current tile index : 3
06-17 23:03:10.009: I/Ninja(15495): Animation frame duration : 104 with current tile index : 4
06-17 23:03:10.029: I/Ninja(15495): Animation frame duration : 103 with current tile index : 4
06-17 23:03:10.049: I/Ninja(15495): Animation frame duration : 101 with current tile index : 4
06-17 23:03:10.070: I/Ninja(15495): Animation frame duration : 100 with current tile index : 4
06-17 23:03:10.090: I/Ninja(15495): Animation frame duration : 99 with current tile index : 5
06-17 23:03:10.110: I/Ninja(15495): Animation frame duration : 98 with current tile index : 5
06-17 23:03:10.130: I/Ninja(15495): Animation frame duration : 97 with current tile index : 5

现在关于我的代码,这里是重要的部分。

首先,我有一个扩展 BaseScene 的 GameScene。

public class GameScene extends BaseScene {
private static final String TAG = "GameScene";
private static final int TIME_BETWEEN_EACH_POSITION_BROADCAST = 250;

private AutoParallaxBackground parallaxBackground;
private Ninja player;
private Vector<Ninja> ninjas;
public int time;
public boolean isStarted = false;
private int timeSinceLastPositionBroadcast = 0;

@Override
protected void onManagedUpdate(float pSecondsElapsed) {
    super.onManagedUpdate(pSecondsElapsed);
    if(isStarted){
        int timeElapsed = Math.round(pSecondsElapsed*1000);
        Log.d(TAG, "Last time: "+time+", time elapsed: "+timeElapsed+", current time: "+(time+timeElapsed));
        time += timeElapsed;
        for(Ninja ninja : ninjas){
            ninja.update(pSecondsElapsed);
        }
        centerScreenOnPlayer();
        parallaxBackground.setParallaxChangePerSecond(player.getSpeed()/120f);
        timeSinceLastPositionBroadcast += timeElapsed;
        if(timeSinceLastPositionBroadcast >= TIME_BETWEEN_EACH_POSITION_BROADCAST){
            timeSinceLastPositionBroadcast -= TIME_BETWEEN_EACH_POSITION_BROADCAST;
            MessageManager.getInstance().broadcastPosition(time, player.getX(), player.getY());
        }
    }
}

private void centerScreenOnPlayer(){
    float newCenterX = player.getX()+200;
    float newCenterY = player.getY()+25;
    camera.setCenter(newCenterX, newCenterY);
}
}

然后我有了扩展 AnimatedSprite 的 Ninja 类。

public class Ninja extends AnimatedSprite {

    public void update(float secondsElapsed){
        updateX(secondsElapsed);
        updateY();
        updateAnimationSpeed();
        float maxSpeedParticleEmitterX = this.getX() + this.getWidth()/4;
        float maxSpeedParticleEmitterY = this.getY() + this.getHeight()/4;
        if(this.getWidth() != this.getWidthScaled()){
            maxSpeedParticleEmitterX -= this.getWidth()/4 * scaleModificationPercentage;
            maxSpeedParticleEmitterY -= this.getHeight()/4 * scaleModificationPercentage;
        }
        maxSpeedParticleEmitter.setCenter(maxSpeedParticleEmitterX, maxSpeedParticleEmitterY);
        maxSpeedParticleSystem.setCurrentTileIndex(this.getCurrentTileIndex());
        slice.setX(this.getX()+this.getWidth());
        slice.setY(this.getY());
    }

    private void updateAnimationSpeed(){
        float divideBy = currentSpeed;
        if(divideBy < 10)
            divideBy = 10;
        int frameDuration = (int) (ANIMATION_SPEED * MAX_MOVEMENT_SPEED / divideBy);    //y = k / x ... where k = 50 * 600 ... (normal max animation speed * max speed)
        if(this.isPlayer)
            Log.i(TAG, "Animation frame duration : "+frameDuration+" with current tile index : "+this.getCurrentTileIndex());
        this.changeFrameDuration(frameDuration);
    }
}

有谁知道为什么我的动画精灵正在迅速改变它的平铺索引,而它应该慢慢改变它?

4

2 回答 2

2

我认为您想要做的是使用 ANIMATION_SPEED * (currentSpeed/MAX_MOVEMENTSPEED) -括号很重要。你想要一个百分比(当前/最大值)乘以一个常数(AnimationSpeed)

int animationSpeed = (int) (ANIMATION_SPEED * (currentSpeed / MAX_MOVEMENT_SPEED));
于 2013-06-18T23:34:27.570 回答
0

我猜 AnimatedSprite 并不意味着它的帧持续时间会在运行中发生如此剧烈的变化(从 > 2000 到 < 1000)。我不知道为什么它改变帧的速度比预期的要快,但我设法通过在帧持续时间超过 500 时停止动画并在它低于 500 时重新开始来解决我的问题。

于 2013-06-19T01:04:34.883 回答