android - Andengine的动画速度异常快

标签 android animation andengine

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

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

我打印了日志以了解为什么我的忍者动画非常快并且当前的图 block 索引显示了一些有趣的事情...

** 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);
    }
}

有谁知道为什么我的动画 Sprite 会快速改变它的图 block 索引,而它应该缓慢改变它?

最佳答案

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

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

关于android - Andengine的动画速度异常快,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17159701/

相关文章:

java - AndEngine - 检查与 Sprite 或 Physics Body 的重叠?

java - 带滚动条的 TextView 可以处理 onTouchEvents

android - 在 iOS 和 Android 上都可用的 SQLite 之上的中级抽象

java - 将 BitmapFactory 存储在我的文件夹中,稍后可以根据名称检索它

android - 解码bitmapfactory两次有什么用?

android - 和引擎。执行碰撞序列

swift - Xcode 9 : SKEditorReference doesn't allow animations?

android - child Android 的 ExpandableListView 动画

animation - SVG 从中心点而不是左上角缩放动画

java - Andengine,平铺纹理大小需要是 2 的幂吗?