更新所有列表项

时间:2015-10-31 11:55:05

标签: c# monogame

请注意,我对编程很新。

所以我为我游戏中的父精灵的所有子精灵(儿童)制作了一个列表,我创建了另一个列表(childrenSpeed),它存储了来自子精灵的每个毫秒的PerFrame浮点值,也附加到父精灵。

问题是:如果我需要更新(作为示例)playerHead的millisecondsPerFrame值,那么我需要输入:

playerHead.millisecondsPerFrame = (value);

但我想这样做,以便我可以使用一行代码更新所有子项的childrenSpeed列表中的所有值。

我尝试使用

player.childrenSpeed.ForEach(...)

为此,但我似乎无法让它发挥作用。

这是我的LoadContent方法:(播放器移动中的最后一个浮点数是millisecondsPerFrame值)

protected override void LoadContent()
{
    spriteBatch = new SpriteBatch(Game.GraphicsDevice);

    // Player sprite
    player = new PlayerMovement(Game.Content.Load<Texture2D>(@"Images/" + s1[0]), new Vector2(150, 150), new Point(20, 35), 0, new Point(0, 0), new Point(1, 11), 1.5f, 1.75f, 65f);
    playerHead = new PlayerMovement(Game.Content.Load<Texture2D>(@"Images/" + s1[2]), new Vector2(150, 150), new Point(20, 35), 0, new Point(0, 0), new Point(1, 11), 1.5f, 1.75f, 65f);
    // playerHead is stored in the children list attached to player
    player.children.Add(playerHead);
    // the millisecondsPerFrame value from playerHead is stored in the childrenSpeed list attached to player
    player.childrenSpeed.Add(playerHead.millisecondsPerFrame);

    // Base loadcontent method
    base.LoadContent();
}

这是我的更新方法:

public override void Update(GameTime gameTime)
{

    if (player.running == true)
    {
        Console.WriteLine("Lopen");
        player.millisecondsPerFrame = 30;
        // I need my childrenSpeed code for millisecondsPerFrame here
    }

    else if (player.running == false)
    {
        Console.WriteLine("gestopt");
        player.millisecondsPerFrame = 65;
        // I need my childrenSpeed code for millisecondsPerFrame here
    }

    if (player.walking == false)
    {
        // not done yet
    }

    player.Update(gameTime, Game.Window.ClientBounds);
    player.children.ForEach(c => c.Update(gameTime, Game.Window.ClientBounds));
    base.Update(gameTime);
}

所以我需要这样的东西:

public override void Update() 
{
    if (player.bool == ...)
    {   
        player.milliSecondsPerFrame = (value);
        // Each element in the player.childrenSpeed list = (value) (same as player)
    }
}

2 个答案:

答案 0 :(得分:0)

我展示了您可以运行和测试的代码的简化版本 伪Player

class Player
{
    public float milliSecondsPerFrame { get; set; }
    public bool running { get; set; }
    public List<float> childrenSpeed { get; set; }
}

Update方法(注意,我们实际上正在分配一个新列表)

public override void Update(Player player, float value)
{
    if (player.running) // == true not required
    {   
        player.milliSecondsPerFrame = value;
        player.childrenSpeed = player.childrenSpeed.Select(s => value).ToList();
    }
}

用法

var player = new Player { running = true, milliSecondsPerFrame = 1.5F, childrenSpeed = new List<float> { 2.5F, -3.5F } };

Update(player, 1.25F); //=> milliSecondsPerFrame = 1.25F, childrenSpeed = { 1.25F, 1.25F }

答案 1 :(得分:0)

它通过在Update方法中使用foreach循环来工作:

foreach (Sprite children in player.children) // player.children is the sprite list
{
    children.millisecondsPerFrame = value; // I assign the millisecondsPerFrame float value
}

但是谢谢你的答案。