我在这里有这种方法,让我的球员运动。它在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;
}
}
答案 0 :(得分: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;
}
}
答案 1 :(得分:0)
这通常在计时器上完成。
确定帧模式和频率。您似乎选择了帧模式CharacterSheet.up,CharacterSheet.upLeftLeg,CharacterSheet.upRightLeg。我们假设你想每400毫秒交换一次帧。
从具有足够分辨率的时钟中获取时间。 System.nanoTime()
通常足够准确。
long frameTime = 400L * 1000000L; // 400 ms in nanoseconds
修改
currentFrame = (System.nanoTime() / frametime) % frames.length;