C# 更新所有列表项
请注意,我对编程相当陌生 因此,我为游戏中父精灵的所有子精灵(children)创建了一个列表,并创建了另一个列表(childrenSpeed),该列表存储了子精灵(也连接到父精灵)的每个毫秒SperFrame浮点值 问题是:如果我需要更新playerHead(例如)的毫秒sperFrame值,那么我需要键入:C# 更新所有列表项,c#,monogame,C#,Monogame,请注意,我对编程相当陌生 因此,我为游戏中父精灵的所有子精灵(children)创建了一个列表,并创建了另一个列表(childrenSpeed),该列表存储了子精灵(也连接到父精灵)的每个毫秒SperFrame浮点值 问题是:如果我需要更新playerHead(例如)的毫秒sperFrame值,那么我需要键入: playerHead.millisecondsPerFrame = (value); 但我想这样做,以便我可以使用一行代码来更新所有子项的childrenSpeed列表中的所有值 我试
playerHead.millisecondsPerFrame = (value);
但我想这样做,以便我可以使用一行代码来更新所有子项的childrenSpeed列表中的所有值
我试着用一个
player.childrenSpeed.ForEach(...)
但我似乎不能让它工作
这是我的LoadContent方法:(PlayerMove中的最后一个浮点值是毫秒sperFrame值)
所以我需要这样的东西:
public override void Update()
{
if (player.bool == ...)
{
player.milliSecondsPerFrame = (value);
// Each element in the player.childrenSpeed list = (value) (same as player)
}
}
我展示了您可以运行和测试的简化版代码。
伪
Player
类
class Player
{
public float milliSecondsPerFrame { get; set; }
public bool running { get; set; }
public List<float> childrenSpeed { get; set; }
}
用法
var player=newplayer{running=true,毫秒sperframe=1.5F,childrenSpeed=newlist{2.5F,-3.5F};
更新(播放器,1.25F);//=>毫秒帧=1.25F,子帧速度={1.25F,1.25F}
我展示了您可以运行和测试的简化版代码。伪
Player
类
class Player
{
public float milliSecondsPerFrame { get; set; }
public bool running { get; set; }
public List<float> childrenSpeed { get; set; }
}
用法
var player=newplayer{running=true,毫秒sperframe=1.5F,childrenSpeed=newlist{2.5F,-3.5F};
更新(播放器,1.25F);//=>毫秒帧=1.25F,子帧速度={1.25F,1.25F}
它通过在更新方法中使用foreach循环来工作:
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);
}
foreach (Sprite children in player.children) // player.children is the sprite list
{
children.millisecondsPerFrame = value; // I assign the millisecondsPerFrame float value
}
但是感谢您的回答。它通过在更新方法中使用foreach循环来工作:
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);
}
foreach (Sprite children in player.children) // player.children is the sprite list
{
children.millisecondsPerFrame = value; // I assign the millisecondsPerFrame float value
}
但是谢谢你的回答。你可以使用
player.childrenSpeed=player.childrenSpeed.Select(s=>value.ToList()如果你想在一行中完成。哦,我不知道你能做到!谢谢编辑:但该代码仅适用于int类型的值。如果我使用浮点,我需要更改什么?(抱歉,重复注释)。我试过了,但没用。还有什么其他的建议,或者我可能做错了什么?我知道你是编程新手,也是编程新手。因此,为了充分利用So,请始终提供一个最小的可复制代码示例,以便其他人可以处理该示例并为您提供解决方案。你能告诉我你的代码的具体部分吗。展示你做了什么,你被困在哪里。是的,我是新来的。我遇到的问题是,我创建了一个名为childrenSpeed的列表,其中包含children列表中精灵的浮点值(如第一个代码部分所示)。我想在一行代码中更改这些值(来自childrenSpeed列表)。(列表中的值对于每个(儿童)精灵都是相同的)我将稍微更改问题中的代码部分,以便更容易显示我的意思。您可以使用player.childrenSpeed=player.childrenSpeed.Select(s=>value.ToList()如果你想在一行中完成。哦,我不知道你能做到!谢谢编辑:但该代码仅适用于int类型的值。如果我使用浮点,我需要更改什么?(抱歉,重复注释)。我试过了,但没用。还有什么其他的建议,或者我可能做错了什么?我知道你是编程新手,也是编程新手。因此,为了充分利用So,请始终提供一个最小的可复制代码示例,以便其他人可以处理该示例并为您提供解决方案。你能告诉我你的代码的具体部分吗。展示你做了什么,你被困在哪里。是的,我是新来的。我遇到的问题是,我创建了一个名为childrenSpeed的列表,其中包含children列表中精灵的浮点值(如第一个代码部分所示)。我想在一行代码中更改这些值(来自childrenSpeed列表)。(列表中的值对于每个(儿童)精灵都是相同的)我将稍微更改问题中的代码部分,以便更容易显示我的意思。谢谢您的回答。我会尽我所能试试这个!直到我将其更改为:.ToList();->我用console.WriteLine检查了这一点,列表的值实际上发生了变化,但playerHead(playerHead.millissecondsPerFrame)的值没有变化(我可以看到这一点,因为playerHead的移动与我使用的console.WriteLine相同。)这是因为.ToList()吗;实际创建一个新列表?为什么要int
?在最初的问题中,您提到的值是float
!如果需要int
,可以将类型和值更改为int。除此之外,上面的代码工作正常。是的,正如我在回答中已经指出的,ToList()
实际上创建了一个新列表。对于所有的问题,很抱歉,列表会更改值,但playerHead(最初添加到childrenSpeed列表中)的浮点值不会。。。因此,您的代码示例可以工作,但我想要更改的原始值(使用列表)没有更改。仅使用新值创建了一个新列表。感谢您的回复。我会尽我所能试试这个!直到我将其更改为:.ToList();->我用console.WriteLine检查了这一点,列表的值实际上发生了变化,但playerHead(playerHead.millissecondsPerFrame)的值没有变化(我可以看到这一点,因为playerHead的移动与我使用的console.WriteLine相同。)这是因为.ToList()吗;实际创建一个新列表?为什么要int
?在最初的问题中,您提到的值是float
!如果需要int
,可以将类型和值更改为int。除此之外,上面的代码工作正常。是的,正如我在回答中已经指出的,ToList()
实际上创建了一个新列表。对于所有的问题,很抱歉,列表会更改值,但playerHead(最初添加到childrenSpeed列表中)的浮点值不会。。。因此,您的代码示例可以工作,但我的原始