1

我用 Microsoft XNA 制作了一个简单的游戏,我有一个Enemy.cs包含属性的类enemyMoveSpeed

public class Enemy
{
    float enemyMoveSpeed { get; set }
}

Game1.cs更新部分中,我放置了一个计时器,它会不断更改以下属性。

protected override void Update(GameTime gameTime)
{
    float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
    timer -= elapsed;
    if (timer < 0)
    {
        foreach (Enemy E in enemies)
        {
            E.enemyMoveSpeed += 1;
        }

        timer = TIMER; //Reset Timer
    }
}

更改后,每个新的 Enemy 实例都会受到影响。有人可以给我一个想法如何实现这个吗?

4

3 回答 3

1

为新速度使用静态属性,在您的 Enemy 构造函数中,您可以设置实例属性以匹配它。

就像是

class Enemy
{
    public static int NewEnemySpeed { get; set;}

    public int Speed {get; set;}

    public Enemy()
    {
        Speed = NewEnemySpeed;
    }

}

从您的 Game1.Update 中,您可以执行此操作

Enemy.NewEnemySpeed = ..//some new speed
于 2013-06-06T13:12:20.467 回答
1

创建列表

List<Enemy> MyEnemyList;
//of course you will have to add Enemies to this list somehow. Probably you already have a list or an array, wich behaves the same for this purpose.

foreach (Enemy E in MyEnemyList)
{
    E.enemyMoveSpeed += 1; //if you are incrementing the speed of everyone by 1.
    //or any other option you think is better
}
于 2013-06-06T13:07:39.270 回答
0

也许这样的事情会帮助你。

public class Enemy
{
    public Enemy()
    {
        EnemyState = new EnemyState { IsDirty = true, Position = Vector2.Zero };
        Speed = 3.0f;
    }

    public EnemyState State { get; set }
    public float Speed { get; set; }
}

public class EnemyState
{
    public bool IsDirty { get; set }
    public Vector2 Position { get; set; }
}

protected override void Update(GameTime gameTime)
{
    float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;

    // If should spawn enemy
    // var enemy = new Enemy();
    // enemy.State.Position = // TODO insert starting position
    // enemies.Add(enemy);
    // Find all enemies that should be moved
    // Set IsDirty = true;

    // Move each enemy that IsDirty
    timer -= elapsed;
    if (timer < 0)
    {
        foreach (Enemy enemy in enemies.Where(e => e.State.IsDirty))
        {
            enemy.State.Position.X += enemy.Speed;
            enemy.State.IsDirty = false;
        }

        timer = TIMER; //Reset Timer
    }
}
于 2013-06-06T13:39:28.123 回答