1

我这里有这个方法可以让我的玩家移动。它在 3 个图像之间交换,站立、左腿向前和右腿向前。

它交换图像的速度非常快,那么如何更改渲染速度?

public static void renderUpwardWalking() {
    ImageIcon[] frames = { CharacterSheet.up, CharacterSheet.upLeftLeg,
            CharacterSheet.upRightLeg };

    if (Key.up && Character.direction == "up") {
        currentFrame++;
        if (currentFrame == 3)
            currentFrame = 1;
        Character.character.setIcon(frames[currentFrame]);
    } else if (!Key.up && Character.direction == "up") {
        currentFrame = 0;
    }
}
4

2 回答 2

0

这通常在计时器上完成。

  1. 确定帧模式和频率。您似乎选择了框架模式 CharacterSheet.up、CharacterSheet.upLeftLeg、CharacterSheet.upRightLeg。假设您想每 400 毫秒交换一次帧。

  2. 从具有足够分辨率的时钟中获取时间。System.nanoTime()通常是足够准确的。

long frameTime = 400L * 1000000L; // 400 ms in nanoseconds 编辑

currentFrame = (System.nanoTime() / frametime) % frames.length;

于 2013-10-18T14:26:14.017 回答
0

您可以更改 currentFrame 计数器的比例,并使用其范围来控制帧速率:

 //Let  this go from 1...30
 int currentFrameCounter;


 .
 .
 .
 currentFrameCounter++;
 if(currentFrameCounter == 30) currentFrameCounter = 0;

 //Take a fraction of currentframeCounter for frame index  ~ 1/10 frame rate
 //Note care to avoid integer division
 currentFrame = (int) (1.0*currentFrameCounter / 10.0);  

将它们放在一个通用模型中:

 int maxCounter = 30; //or some other factor of 3 -- controls speed


 int currentFrameCounter;

 public static void renderUpwardWalking() {
     ImageIcon[] frames = { CharacterSheet.up, CharacterSheet.upLeftLeg,
        CharacterSheet.upRightLeg };

     if (Key.up && Character.direction == "up") {

         currentFrameCounter++;  //add
         if(currentFrameCounter == maxCounter) currentFrameCounter = 0;             
         currentFrame = (int) (1.0*currentFrameCounter / (maxCounter/3.0));  
         Character.character.setIcon(frames[currentFrame]);
     } else if (!Key.up && Character.direction == "up") {
         currentFrame = 0;
     }

}

于 2013-10-18T14:13:25.430 回答