有没有办法跟踪对象更新的次数?

时间:2015-04-13 13:53:17

标签: c# xna-4.0

我有一个2D游戏,我的工作类似于Minecraft,意味着需要很多块,每个都需要相应更新(以检测玩家何时想要删除它们)。目前它运行良好,但我过去曾遇到帧速率和块数问题。

目前通过列出如下

来添加块
 if (blockcheck && game.build.BlockID == 1)
 {
    position2 = new Vector2(game.cursor.cursorPos.X + (float)30f, game.cursor.cursorPos.Y - (float)30f);
    position = new Vector2((int)(position2.X / 58) * 58, (int)(position2.Y / 58) * 58);
    game.blocktex1 = game.grass1;
    block = new Block(game, game.blocktex1, new Vector2(position.X, position.Y), layer, 0);
    blockpos1.Add(position);
    blocklayer1.Add(layer);
    blocklist.Add(block);
    placeblock = 200.0f;

 }

然后使用简单的foreach语句

更新每个块
 foreach (Block b in blocklist)
 {
    b.Update(gameTime);
 }

我想要做的是每次更新一个块时加一个+ 1。通过这样做,我想我应该能够告诉我的更新上限是什么,我唯一看到的问题是,如果我简单地说

 foreach (Block b in blocklist)
 {
    b.Update(gameTime);
    int updatenumber += 1;

 }

它会不断地为每个块添加一个,我觉得它应该像是一个块正在更新,更新号应该是一个,如果两个块正在更新,那么数字应该是两个,依此类推。

如何制作它以便计算更新的对象数量,并且不会不断添加?

1 个答案:

答案 0 :(得分:1)

在这种情况下,事件应该会有所帮助:

public class UpdatingEventArgs : EventArgs
{
    public int Id { get; set; }
}

public class UpdatedEventArgs : EventArgs
{
    public int Id { get; set; }
    public int NewValue { get; set; }
}


public class GameObject
{
    public event EventHandler<UpdatingEventArgs> OnUpdating;
    public event EventHandler<UpdatedEventArgs> OnUpdated;

    public int Id { get; set; }
    public int Value { get; set; }

    public void Update()
    {
        var updatingHandler = OnUpdating;
        if (updatingHandler != null)
        {
            updatingHandler(this, new UpdatingEventArgs {Id = Id});
        }

        if (Value > 0)
        {
            return;
        }

        Value++;

        var updatedHandler = OnUpdated;
        if (updatedHandler != null)
        {
            updatedHandler(this, new UpdatedEventArgs {Id = Id, NewValue = Value});
        }
    }
}

class Program
{
    static void Main(string[] args)
    {
        var r = new Random();
        var gameObjects = Enumerable.Range(0, 100)
            .Select(x => new GameObject
            {
                Id = x, 
                Value = r.Next(-10, 10)
            })
            .ToList();

        var updatingCounter = 0;
        var updatedCounter = 0;

        EventHandler<UpdatedEventArgs> updatedLambda = (sender, arg) => updatedCounter++;
        EventHandler<UpdatingEventArgs> updatingLambda = (sender, arg) => updatingCounter++;

        foreach (var gameObject in gameObjects)
        {
            gameObject.OnUpdating += updatingLambda;
            gameObject.OnUpdated += updatedLambda;

            gameObject.Update();

            gameObject.OnUpdating -= updatingLambda;
            gameObject.OnUpdated -= updatedLambda;
        }

        Console.WriteLine(updatingCounter);
        Console.WriteLine(updatedCounter);
        Console.ReadKey();
    }
}

或者您只需将Update方法的返回类型更改为bool:

 foreach (Block b in blocklist)
 {
    if (b.Update(gameTime))
    {
        int updatenumber += 1;
    }
 }