射击c#游戏中的弹丸延迟 [英] Projectile delay in shooting c# game
本文介绍了射击c#游戏中的弹丸延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个游戏,我希望玩家能够在延迟的情况下发射激光.代码有效,但我想知道我这样做是否正确.
I have a game where I want the player to be able to shoot a laser with a delay on it. The code works but I'm wondering if I am going the right way about doing this.
我想知道添加延迟的正确方法是什么?
I was wondering what is the proper way to add the delay?
我尝试包含与问题相关的代码.
I tried to include the code relevant to the question.
private double laserDelay;
private TimeSpan laserShootInterval = TimeSpan.FromSeconds(6);
laserDelay = laserShootInterval.TotalSeconds;
if (currentKeyState.IsKeyDown(Keys.Space))
{
if(laserDelay == laserShootInterval.TotalSeconds)
{
Shoot();
laserDelay = laserDelay - laserShootInterval.TotalSeconds;
}
}
UpdateLasers(graphics);
if(laserDelay < laserShootInterval.TotalSeconds)
{
laserDelay++;
}
推荐答案
假设您没有使用增量时间(每秒固定的滴答数),我会这样做:
Presuming you are not using delta time (fixed amount of ticks per second) I would do it like so:
int delay = 6 * ticksPerSecond; // ticks to delay for
int cooldown = 0;
public void loop()
{
if (currentKeyState.IsKeyDown(Keys.Space))
{
if(cooldown <= 0)
{
Shoot();
cooldown = delay
}
}
UpdateLasers(graphics);
if(cooldown > 0){
cooldown -= 1;
}
}
这篇关于射击c#游戏中的弹丸延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文