0

我正在制作一个垂直平台游戏。我放置平台的方式是通过列表:

      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);
           }
        }

这适用于如果我想随机放置平台但我将如何设置它以便根据当前级别平台单独重新定位自己?我知道这可能意味着我不能使用列表。

4

2 回答 2

0

我不确定我是否给你一个 100%,但你可以通过提供一个比较方法来对你Entity_Platform的 s进行排序,比如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 示例,请参见此处。

于 2013-04-10T08:10:51.330 回答
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);
    }
}
于 2013-04-10T08:26:15.447 回答