1

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

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

2 回答 2

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();
        }
    }
}
于 2013-10-09T18:36:40.633 回答
0

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

int shootCounter = 0;

...

if(shootCounter > 15)
{
    Shoot();
    shootcounter = 0;
}
else
{
    shootcounter++;
}

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

于 2013-10-09T18:34:17.853 回答