gpt4 book ai didi

c# - 如何为按键拍摄添加延迟

转载 作者:太空宇宙 更新时间:2023-11-03 21:41:04 25 4
gpt4 key购买 nike

我正在制作太空射击游戏,我想知道如何延迟射击而不是乱按空格键,谢谢 :)

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

最佳答案

我认为您想使用 XNA 的 GameTime 属性。每当你发射一颗子弹时,你应该记录它发生的时间作为当前的 GameTime.ElapsedGameTime 值。

这是一个TimeSpan,所以你可以像下面这样比较它:

// 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();
}
}
}

关于c# - 如何为按键拍摄添加延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19279856/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com