Andengine的动画速度异常快

时间:2013-06-18 02:30:18

标签: android animation andengine

我正在使用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);
    }
}

有没有人知道为什么我的动画精灵会快速改变其拼贴指数,而它应该慢慢改变它?

2 个答案:

答案 0 :(得分:2)

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

int animationSpeed = (int) (ANIMATION_SPEED * (currentSpeed / MAX_MOVEMENT_SPEED));

答案 1 :(得分:0)

我猜AnimatedSprite并不意味着它的帧持续时间在飞行中如此剧烈地改变(从&gt; 2000到<1000)。我不知道为什么它比预期更快地改变帧,但我设法通过在帧持续时间超过500时停止动画并在低于500时重新启动它来解决我的问题。