c# - 方法不断将相同的东西添加到列表中
问题描述
我正在创建一个 Text Base 竞技场 rpg,每天都有新的怪物添加到列表中,但只有一个怪物重复添加到列表中,并且由于某种原因每天都在增加相同的统计数据。我需要的是创建具有不同值的不同对象而不是添加到列表中,第二部分效果很好。
此方法调用创建者。
public static List<Monster> MonsterOfTheDay()
{
int count = 0;
List<Monster> MonstersListOfTheDay = new List<Monster>();
while(count <= 5)
{
MonstersListOfTheDay.Add(Creator());
count++;
}
return MonstersListOfTheDay;
}
这是创造者
public static Monster Creator()
{
Random random = new Random();
Monster monsterChoosen = monsterListPrefab.Find(m => m.Id == random.Next(0, monsterListPrefab.Count -1));
monsterChoosen.Level = random.Next(monsterChoosen.Level, monsterChoosen.Level + 3);
//1Offensive, 2Defensive, 3Balance
monsterChoosen.Type = (Types)typeList.GetValue(random.Next(1, typeList.Length));
Console.WriteLine("Estou Aqui");
int atributes = monsterChoosen.Level * 3;
int spend = 0;
Console.WriteLine("Estou Aqui");
while(spend != atributes)
{
int chance = random.Next(0, 100);
if(monsterChoosen.Type == Types.Offensive)
{
if(chance >= 0 && chance <= 60)
{
monsterChoosen.Str++;
spend++;
}
if(chance >= 61 && chance <= 70)
{
monsterChoosen.Int++;
spend++;
}
if(chance >= 71 && chance <= 85)
{
monsterChoosen.Agi++;
spend++;
}
if(chance >= 86 && chance <= 100)
{
monsterChoosen.Vig++;
spend++;
}
}
else if(monsterChoosen.Type == Types.Defensive)
{
if(chance >= 0 && chance <= 60)
{
monsterChoosen.Vig++;
spend++;
}
if(chance >= 61 && chance <= 70)
{
monsterChoosen.Str++;
spend++;
}
if(chance >= 71 && chance <= 85)
{
monsterChoosen.Int++;
spend++;
}
if(chance >= 86 && chance <= 100)
{
monsterChoosen.Agi++;
spend++;
}
}
else if(monsterChoosen.Type == Types.Balance)
{
if(chance >= 0 && chance <= 25)
{
monsterChoosen.Str++;
spend++;
}
if(chance >= 26 && chance <= 50)
{
monsterChoosen.Int++;
spend++;
}
if(chance >= 51 && chance <= 75)
{
monsterChoosen.Agi++;
spend++;
}
if(chance >= 76 && chance <= 100)
{
monsterChoosen.Vig++;
spend++;
}
}
else if(monsterChoosen.Type == Types.Prefab)
{
spend++;
}
else
{
Console.WriteLine("Error");
}
}
return monsterChoosen;
}
解决方案
我偶然发现了 Creator 方法的最初 3 行,现在它可以正常工作了,原因是,正如 Phillipp 在游戏开发者堆栈中所说,旧代码保留了 monsterChoosen 变量的信息,再次调用时不会生成新的,保持相同的结果,只增加它的统计数据和级别,现在有了新的变量行,monsterChoosen 可以在每次调用时更新。
Random random = new Random();
int randId = random.Next(2);
Monster monsterChoosen = new Monster(monsterListPrefab.Find(m => m.Id == randId));
推荐阅读
- android - DummyDataSource 无法打开:Android ExoPlayer
- java - Java Do-While 循环 - 如何使用 Q 选项结束程序
- ios - 如何在 iOS 中轻松渲染视频帧,如 AVSampleBufferDisplayLayer
- database - 将模型从一个屏幕传递到另一个 FLUTTER 的更好方法
- c# - 创建未经授权访问的子目录(用于单元测试)
- linux - SLES SP5 中未提取 Sudoers.d 文件
- elasticsearch - Elasticsearch 服务关闭,因为“临时文件目录 [...] 不存在 [...]”
- gekko - 可变输出总和的约束
- python - 硒元素无法滚动到视图中
- firebase - 颤振:删除(firebase 存储)