射击c#游戏中的弹丸延迟
Projectile delay in shooting c# game
我有一个游戏,我希望玩家能够延迟发射激光。该代码有效,但我想知道我这样做是否正确。
我想知道添加延迟的正确方法是什么?
我试图包含与问题相关的代码。
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++;
}
假设您没有使用增量时间(每秒固定的滴答数)我会这样做:
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;
}
}
我有一个游戏,我希望玩家能够延迟发射激光。该代码有效,但我想知道我这样做是否正确。
我想知道添加延迟的正确方法是什么?
我试图包含与问题相关的代码。
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++;
}
假设您没有使用增量时间(每秒固定的滴答数)我会这样做:
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;
}
}