简体   繁体   English

射击C#游戏中的弹丸延迟

[英]Projectile delay in shooting c# game

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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM