2013-04-10 70 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); 
    } 
}