我正在制作垂直平台游戏。我放置平台的方式是通过列表:
public void LoadPlatforms(ContentManager content, Mechanic_Levels mech, Clide clide)
{
platforms.Add(new Entity_Platform());
platforms.Add(new Entity_Platform());
platforms.Add(new Entity_Platform());
platforms.Add(new Entity_Platform());
platforms.Add(new Entity_Platform());
platforms.Add(new Entity_Platform());
platforms.Add(new Entity_Platform());
platforms.Add(new Entity_Platform());
platforms.Add(new Entity_Platform());
// factory.Add(new Entity_Factory());
foreach (Entity_Platform platform in platforms)
{
platform.position = new Vector2(rand.Next(20, 280), rand.Next(20, 580));
platform.currentlevel = rand.Next(12);
platform.LoadPlatform(content);
}
}
public void LoadPlatforms(ContentManager content, Mechanic_Levels mech, Clide clide)
{
platforms.Add(new Entity_Platform());
platforms.Add(new Entity_Platform());
platforms.Add(new Entity_Platform());
platforms.Add(new Entity_Platform());
platforms.Add(new Entity_Platform());
platforms.Add(new Entity_Platform());
platforms.Add(new Entity_Platform());
platforms.Add(new Entity_Platform());
platforms.Add(new Entity_Platform());
// factory.Add(new Entity_Factory());
foreach (Entity_Platform platform in platforms)
{
platform.position = new Vector2(rand.Next(20, 280), rand.Next(20, 580));
platform.currentlevel = rand.Next(12);
platform.LoadPlatform(content);
}
}
如果我想随机放置平台,这是有效的,但我如何设置它,以便根据当前级别平台重新定位自己?我知道这可能意味着我不能使用列表。
答案 0 :(得分:0)
我不确定我是否能得到100分,但您可以通过提供比较方法({/ p>)对Entity_Platform
内的List<Entity_Platform>
进行排序
private static int ComparePlatforms(Entity_Platform x, Entity_Platform y)
{
//compare your platforms according to chosen critieria
//should return 1 if x > y, 0 if x == y, and -1 if x < y
}
之后你可以使用
platforms.Sort(ComparePlatforms);
有关MSDN示例,请参阅here。
答案 1 :(得分:0)
我想也许一些答案(而不是评论)是恰当的。我想你要问的是如何根据预先设计为每个级别加载一组平台。
保持当前的LoadPlatforms
方法,我会添加另一种方法来获取基于该级别的平台。例如:
public List<Entity_Platform> GetPlatformsForLevel(int level)
{
//for this example I will hard-code the platforms, you can pull them from another source if you wish
List<Entity_Platform> platforms = new List<Entity_Platform>();
switch(level)
{
case 1:
{
platforms.Add(new Entity_Platform() { currentLevel = level, position = new Vector2(100, 50) });
platforms.Add(new Entity_Platform() { currentLevel = level, position = new Vector2(200, 100) });
platforms.Add(new Entity_Platform() { currentLevel = level, position = new Vector2(300, 75) });
}
break;
case 2:
{
platforms.Add(new Entity_Platform() { currentLevel = level, position = new Vector2(80, 20) });
platforms.Add(new Entity_Platform() { currentLevel = level, position = new Vector2(160, 200) });
platforms.Add(new Entity_Platform() { currentLevel = level, position = new Vector2(250, 50) });
}
break;
}
return platforms;
}
然后你可以在LoadPlatforms
方法中调用它,如下所示:
public void LoadPlatforms(ContentManager content, Mechanic_Levels mech, Clide clide)
{
int currentLevel = 1;//you need to track the current level somewhere
List<Entity_Platform> platforms = GetPlatformsForLevel(currentLevel);
foreach (Entity_Platform platform in platforms)
{
platform.LoadPlatform(content);
}
}