我正在制作一个太空射击游戏,我想知道如何延迟拍摄而不是发送垃圾邮件感谢:)
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);
}
答案 0 :(得分:4)
我认为你想要使用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();
}
}
}
答案 1 :(得分:0)
你可以有一个计数器来计算自上次拍摄以来的帧数。
int shootCounter = 0;
...
if(shootCounter > 15)
{
Shoot();
shootcounter = 0;
}
else
{
shootcounter++;
}
如果您想要更高级,可以使用默认更新方法通常为您提供的gameTime
来跟踪自您上次拍摄以来经过的时间。