2013-04-10 65 views
0

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

 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

你是什么意思的“重新定位自己”吗? “currentLevel”是做什么的? – musefan 2013-04-10 08:05:43

+0

对不起,应该定义我的意思,但目前的水平。所以游戏的效果是,当你越过屏幕的顶部时,等级会上升一个。这是通过一个简单的基于整数的系统来处理的,其中平台的当前级别等于平台呈现的玩家级别。我的意思是重新定位基本上不是拥有我现在拥有的随机系统,而是拥有一个设计我设计的关卡的系统。 – user1994100 2013-04-10 08:10:59

+0

因此,不要在循环中随机选择位置,而应在将platofrm添加到列表时指定位置。这可以通过代码来分配,可能通过诸如“GetPlatformsForLevel(int level)”之类的方法,或者从外部源(DB,配置文件等)中提取数据。关于目前的水平,你绝对不希望这是随机的。因为你将存储你不需要或使用的平台 - 尽管我认为这取决于性能,即你的级别切换的速度有多快(是否有时间每次加载下一组平台?) – musefan 2013-04-10 08:17:04

回答

0

我不知道,如果我给你一个100%,但您可以通过提供像

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 
} 

之后的比较方法你Entity_Platform S中List<Entity_Platform>内进行排序,你可以使用

platforms.Sort(ComparePlatforms); 

有关MSDN示例,请参阅here

+0

我会采取这种方法计数,并感谢您的帮助。 – user1994100 2013-04-10 08:25:39

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