Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何增加按键射击的延迟_C#_Xna - Fatal编程技术网

C# 如何增加按键射击的延迟

C# 如何增加按键射击的延迟,c#,xna,C#,Xna,我正在制作一个太空射击游戏,我想知道如何延迟拍摄,而不是在空格键上发送垃圾邮件。谢谢: public void Shoot() { Bullets newBullet = new Bullets(Content.Load<Texture2D>("PlayerBullet")); newBullet.velocity = new Vector2((float)Math.Cos(rotation), (float)Math.Sin(rotation

我正在制作一个太空射击游戏,我想知道如何延迟拍摄,而不是在空格键上发送垃圾邮件。谢谢:

public void Shoot()
    {
        Bullets newBullet = new Bullets(Content.Load<Texture2D>("PlayerBullet"));
        newBullet.velocity = new Vector2((float)Math.Cos(rotation), (float)Math.Sin(rotation)) * 5f;
        newBullet.position = playerPosition + playerVelocity + newBullet.velocity * 5;
        newBullet.isVisible = true;

        if (bullets.Count() >= 0)
            bullets.Add(newBullet);
    }

您可以使用一个计数器来计算自上次拍摄以来的帧数

int shootCounter = 0;


如果您想更高级,可以使用默认更新方法通常提供给您的游戏时间来跟踪自上次拍摄以来经过的时间。

您可以使用计数器计算自上次拍摄以来的帧数

int shootCounter = 0;


如果您想更高级,可以使用默认更新方法通常提供给您的gameTime来跟踪自上次拍摄以来经过的时间。

我认为您需要使用XNA的gameTime属性。无论何时射击子弹,都应将其发生的时间记录为当前GameTime.ElapsedGameTime值

int shootCounter = 0;
这是一个时间跨度,因此您可以按如下方式进行比较:

// Create a readonly variable which specifies how often the user can shoot.  Could be moved to a game settings area
private static readonly TimeSpan ShootInterval = TimeSpan.FromSeconds(1);

// Keep track of when the user last shot.  Or null if they have never shot in the current session.
private TimeSpan? lastBulletShot;

protected override void Update(GameTime gameTime)
{
    if (???) // input logic for detecting the spacebar goes here
    {
        // if we got here it means the user is trying to shoot
        if (lastBulletShot == null || gameTime.ElapsedGameTime - (TimeSpan)lastBulletShot >= ShootInterval)
        {
            // Allow the user to shoot because he either has not shot before or it's been 1 second since the last shot.
            Shoot();
        }
    }
}

我想你应该使用XNA的游戏时间属性。无论何时射击子弹,都应将其发生的时间记录为当前GameTime.ElapsedGameTime值

这是一个时间跨度,因此您可以按如下方式进行比较:

// Create a readonly variable which specifies how often the user can shoot.  Could be moved to a game settings area
private static readonly TimeSpan ShootInterval = TimeSpan.FromSeconds(1);

// Keep track of when the user last shot.  Or null if they have never shot in the current session.
private TimeSpan? lastBulletShot;

protected override void Update(GameTime gameTime)
{
    if (???) // input logic for detecting the spacebar goes here
    {
        // if we got here it means the user is trying to shoot
        if (lastBulletShot == null || gameTime.ElapsedGameTime - (TimeSpan)lastBulletShot >= ShootInterval)
        {
            // Allow the user to shoot because he either has not shot before or it's been 1 second since the last shot.
            Shoot();
        }
    }
}

用逻辑修正更新。谢谢:D我还没有修正它,但这教会了我一些东西:用逻辑修正更新。谢谢:D我还没有修正它,但这教会了我一些东西:仅供参考:xna中的更新方法应该每秒调用60次,但有时会延迟。update调用与Draw方法的调用速率完全不同,因此这将比frames调用更多。但这并不是什么大不了的事,只是说说而已。如果电脑速度更快的人可以拍摄得更快,这有点大不了:仅供参考:xna中的更新方法据说每秒调用60次,但有时可能会滞后。update调用与Draw方法的调用速率完全不同,因此这将比frames调用更多。但这没什么大不了的,只是说说而已。如果电脑速度更快的人能拍得更快,那就有点大不了了: