【问题标题】:Limit Rendering Speed限制渲染速度
【发布时间】:2013-10-18 14:03:41
【问题描述】:

我这里有这个方法可以让我的玩家移动。它在 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;
    }
}

【问题讨论】:

  • 我确信有更好的方法,但首先想到的是(尽管是一种幼稚的方法):首先将 System.currentTimeMillis() 存储在变量中(例如 lastMoveTime),然后在每个调用你的 move 方法,将当前时间与存储时间进行比较;如果时间已过,请更改图像并更新存储的时间。调整口味。

标签: performance rendering limit


【解决方案1】:

这通常在计时器上完成。

  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;

【讨论】:

  • 我试图在我的代码中使用这些变量,但是 currentFrame 会去哪里?我尝试替换 currentFrame++;但是该程序产生的速度与以前相同。你能给我解释一下吗?
  • 我现在通过修改 frameTime 注意到速度发生了变化,但它似乎并没有变慢。有什么见解吗?
  • 尝试注释掉您设置 currentFrame 变量的所有其他位置。
  • ` public static void renderUpwardWalking() { ImageIcon[] frames = { CharacterSheet.up, CharacterSheet.upLeftLeg, CharacterSheet.upRightLeg };长帧时间 = 400L * 1000L; // 400 毫秒,以纳秒为单位 if (Key.up && Character.direction == "up") { currentFrame = (int) ((System.nanoTime() / frameTime) % frames.length); //if (currentFrame == 3) //currentFrame = 1; Character.character.setIcon(frames[currentFrame]); } else if (!Key.up && Character.direction == "up") { //currentFrame = 0; } }`
  • 对不起我的cmets。我不明白格式。速度还是很快的。但它看起来不同。不知道我做错了什么。
【解决方案2】:

您可以更改 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;
     }

}

【讨论】:

  • 效果很好,但我不断得到一个数组索引超出 3 的范围。
  • 对不起..您必须确保 currentFrameCounter 严格保持
猜你喜欢
  • 1970-01-01
  • 2015-09-17
  • 1970-01-01
  • 1970-01-01
  • 2013-11-26
  • 2011-10-04
  • 2012-06-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多