【发布时间】:2016-03-06 19:20:57
【问题描述】:
我很难找到一种方法来在我的 for 循环中创建一个方程式,该方程式将根据角色所在的级别在列表 collectibleList 中生成许多对象。就像现在一样,我的列表每个级别只创建一个collectible。我猜这是因为i < currentLevel 绑定。但我不知道我应该应该使用什么样的绑定,或者如何在等式中实现i,以便可以根据currentLevel将更多collectibles添加到我的列表中。
// Set up each level the player encounters
public void NextLevel()
{
collectibleList.Clear();
currentLevel++;
timer = 10;
player.LevelScore = 0;
player.Position = new Rectangle(GraphicsDevice.Viewport.Width/2, GraphicsDevice.Viewport.Height/2, player.Position.Width, player.Position.Height);
// Random number generator that will help generate a random position of the collectible sprite
Random rng = new Random();
for (int i = 0; i < currentLevel; i++)
{
Collectible collectible = new Collectible(rng.Next(0, GraphicsDevice.Viewport.Width), rng.Next(0, GraphicsDevice.Viewport.Height), 70, 91, true);
collectible.ObjectSprite = collectibleSprite;
collectibleList.Add(collectible);
}
}
【问题讨论】:
-
“创建公式”是什么意思?每个级别你想要多少个对象?计算您想要的数字,将其存储在变量中。然后在 for 循环中使用该变量而不是
currentLevel。 -
我同意@Blorgbeard:问题/任务定义模糊,问题需要澄清。谢谢和问候,
-
@Blorgbeard 我想我正在寻找一个依赖于
i的方程式,以便每个级别都有更多的收藏品,具体取决于级别。像collectibleList.Add(collectible + 2i)之类的,但显然这是无效的 -
我还是无法理解你的问题。 @Blorgbeard 的方法怎么样?
-
据我所知,每个级别当前都有更多的收藏品,具体取决于级别。每个级别一个。更多关卡 = 更多收藏品。如果你想要不同的等式,请将
i < currentLevel替换为i < currentLevel * 4或i < Math.Pow(currentLevel, 2)或其他任何内容。