XNA:列表中实体的位置

XNA:列表中实体的位置,xna,Xna,我正在做一个垂直的平板。我放置平台的方式是通过列表: 如果我想随机放置平台,这是可行的,但是我如何设置它,以便根据当前的级别,平台各自重新定位?我知道这可能意味着我不能使用列表。我不确定我是否能给你100%的分数,但你可以通过提供如下比较方法对列表中的实体\u平台进行排序 private static int ComparePlatforms(Entity_Platform x, Entity_Platform y) { //compare your platforms ac

我正在做一个垂直的平板。我放置平台的方式是通过列表:


如果我想随机放置平台,这是可行的,但是我如何设置它,以便根据当前的级别,平台各自重新定位?我知道这可能意味着我不能使用列表。

我不确定我是否能给你100%的分数,但你可以通过提供如下比较方法对
列表中的
实体\u平台
进行排序

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
}

有关MSDN示例,请参阅。

我认为也许一点答案(而不是注释)是合适的。我认为您要问的是如何根据预先确定的设计为每个级别加载一组平台

保留当前的
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;
}

你说的“重新定位自己”是什么意思?那么
currentLevel
有什么作用呢?对不起,我应该定义一下我的意思,但不是当前级别。因此,游戏的工作原理是,当你越过屏幕顶部时,关卡会上升一级。这是由一个简单的基于整数的系统处理的,如果平台的当前级别等于玩家级别,则平台渲染。我所说的重新定位,基本上是取代了随机系统,现在我有了一个设置了我设计的等级的系统。因此,在循环中,你应该在列表中添加platofrm时指定位置,而不是随机位置。这可以通过代码进行分配,可能通过
GetPlatformsForLevel(int-level)
等方法,或者从外部源(DB、配置文件等)提取数据。关于目前的水平,你肯定不希望它是随机的。因为您将存储您不需要或不使用的平台—尽管我认为这取决于性能,即级别切换的速度(每次都有时间加载下一组平台吗?),我将尝试一下,谢谢您的帮助。对不起,有点不清楚。
platforms.Sort(ComparePlatforms);
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;
}
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);
    }
}