简体   繁体   English

极限渲染速度

[英]Limit Rendering Speed

I have this method here which renders my players movement. 我在这里有这种方法,可以使我的球员运动。 It swaps between 3 images, standing, left leg forward, and right leg forward. 它在3张图像(站立,左腿向前和右腿向前)之间交换。

It swaps images very fast so how can I change the speed of the rendering? 它可以非常快速地交换图像,那么如何更改渲染速度?

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

You can change the scale of your currentFrame counter, and use its range to control your frame rate: 您可以更改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);  

Putting it all together in a general model: 将它们放到一个通用模型中:

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

} }

This is usually done on timer. 这通常是在计时器上完成的。

  1. Decide on a frame pattern and a frequency. 确定帧模式和频率。 You seem to have chosen the frame pattern CharacterSheet.up, CharacterSheet.upLeftLeg, CharacterSheet.upRightLeg. 您似乎已经选择了帧模式CharacterSheet.up,​​CharacterSheet.upLeftLeg,CharacterSheet.upRightLeg。 Let's say you want to swap frame every 400 ms. 假设您想每400毫秒交换一次帧。

  2. Get the time from a clock with sufficient resolution. 从具有足够分辨率的时钟获取时间。 System.nanoTime() is usually accurate enough. System.nanoTime()通常足够准确。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM