对于一个项目,我必须让一个精灵跳跃,我不知道该怎么做,我已经测试了这个
while(sfRenderWindow_pollEvent(window, &event)) {
if (event.type == sfEvtClosed) {
sfRenderWindow_close(window);
}
if (event.type == sfEvtKeyPressed) {
if (event.key.code == sfKeySpace || sfKeyUp)
jumping = 1;
}
}
if (jumping) {
while (clock < 60) {
printf("%fn", dino_pos.y);
if (clock < 30) {
dino_pos.y -= 2;
sfSprite_setPosition(dino, dino_pos);
clock++;
} else if (clock < 60) {
dino_pos.y += 2;
sfSprite_setPosition(dino, dino_pos);
clock++;
}
}
if (clock >= 60)
clock = 0;
printf("Jumpn");
}
jumping = 0;
但他不工作,我的程序是60帧/秒,所以我希望跳跃的持续时间是1秒。Printf在这里查看我的恐龙的位置,但我想跳跃是瞬间的,所以我没有时间看到跳跃。你有跳的想法吗?
您的程序逻辑应该如下所示:
注意:这只是一个简化版本,不是一个工作示例。它应该给你一个如何做的提示。
int main()
{
openWindow();
while (active) {
getTime();
processMessages();
doLogic();
renderScene();
swapBuffers();
}
closeWindow();
}
void getTime()
{
// you have to know the time unit (sec, ms, us, ns, etc.)
current_time = systemTime();
}
void processMessages()
{
while (nextEvent()) {
if (EventType == EvtClosed) {
active = false;
}
if ((EventType == KeyPressed) && (KeyType == KeySpace) && !jumping) {
jump_start = current_time;
jumping = true;
}
}
}
void doLogic()
{
if (jumping) {
// time_frac has to be in range [0.0..1.0]
time_frac = (current_time - jump_start) / time_units_per_second;
// linear interpolation based on time fraction
pos = start_pos * (1 - time_frac) + end_pos * time_frac;
}
}
void renderScene()
{
printf("%fn", pos);
}