2016-10-17 220 views
0

所以這個程序我在有精靈。每個精靈都有其精靈可以擁有的幀數量的限制,並且我試圖弄清楚如何學習該限制,以便我可以對其進行修改。但是我一直在閱讀的代碼對我來說確實很難。我一直在閱讀它使用的一些東西(如DictionaryOut),但是當我嘗試將該閱讀應用於代碼時,它只是分崩離析。因此,如果有人願意分析代碼並告訴我它說了什麼,那就太好了。完整的可以發現here,但是這是我想特別爲:需要幫助的理解代碼

class FrameData { 
    Dictionary<FrameType, Dictionary<Enums.Direction, int>> frameCount; 
} 

public FrameData() { 
    frameCount = new Dictionary<FrameType, Dictionary<Enums.Direction, int>>(); 
} 

public void SetFrameCount(FrameType type, Enums.Direction dir, int count) { 
    if (frameCount.ContainsKey(type) == false) { 
     frameCount.Add(type, new Dictionary<Enums.Direction, int>()); 
    } 
    if (frameCount[type].ContainsKey(dir) == false) { 
     frameCount[type].Add(dir, count); 
    } else { 
     frameCount[type][dir] = count; 
    } 
} 

public int GetFrameCount(FrameType type, Enums.Direction dir) { 
    Dictionary<Enums.Direction, int> dirs = null; 
    if (frameCount.TryGetValue(type, out dirs)) { 
     int value = 0; 
     if (dirs.TryGetValue(dir, out value)) { 
      return value; 
     } else { 
      return 0; 
     } 
    } else { 
     return 0; 
    } 
} 
+0

嘗試張貼的問題在這裏-http://codereview.stackexchange.com/ – OldProgrammer

+3

@OldProgrammer這問題將不會成爲Code Review的主題,因爲它正在尋求關於代碼如何工作的建議。 CR代碼已經按照預期工作,並且可以理解,但您希望改進。 –

回答

1
//This bit declares the class. note that all the stuff after it should come inside the open and closed curly braces, so there's already a syntax error here. 
class FrameData { 
    Dictionary<FrameType, Dictionary<Enums.Direction, int>> frameCount; 
} 
// Public parameterless constructor. This gets called when someone creates an instance of the class, e.g. FrameData myframe = new FrameData() 
public FrameData() { 
    // initialize the instance variable frameCount with a new dictionary that takes a FrameType as the key and another dictionary of Enums.Direction and ints as key and value 
    frameCount = new Dictionary<FrameType, Dictionary<Enums.Direction, int>>(); 
} 
// Public method for adding or replacing a key and its value in the frameCount dictionary 
public void SetFrameCount(FrameType type, Enums.Direction dir, int count) { 
    // adds a new one if it didn't already have that key 
    if (frameCount.ContainsKey(type) == false) { 
     frameCount.Add(type, new Dictionary<Enums.Direction, int>()); 
    } 
    // adds a new key to the inner dictionary if it's not there 
    if (frameCount[type].ContainsKey(dir) == false) { 
     frameCount[type].Add(dir, count); 
    } else { 
     // otherwise just replaces what was already there 
     frameCount[type][dir] = count; 
    } 
} 
// fetches the nested value from the inner dictionary given the type and direction 
public int GetFrameCount(FrameType type, Enums.Direction dir) { 
    Dictionary<Enums.Direction, int> dirs = null; 
    if (frameCount.TryGetValue(type, out dirs)) { 
     int value = 0; 
     if (dirs.TryGetValue(dir, out value)) { 
      return value; 
     } else { 
      return 0; 
     } 
    } else { 
     return 0; 
    } 
} 
+0

謝謝!我想這可能不會是什麼設置一個spritesheet可以有多少幀,然後......篩選更多文件的時間x: – Tapu