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

GameTimeXNAのプロパティを使いたいと思います。弾丸を撃つときはいつでも、それが起こった時間を現在の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 に答える