簡體   English   中英

需要幫助來了解代碼

[英]Need help understanding a code

所以我正在使用的這個程序有精靈。 每個精靈都有其精靈可以容納的幀數的限制,我正在嘗試弄清楚如何學習該限制,以便可以對其進行修改。 但是我一直在閱讀的代碼對我來說真的很困難。 我一直在閱讀它使用的一些內容(例如DictionaryOut ),但是當我嘗試將其應用於代碼時,它就會崩潰。

所以,如果有人願意分解代碼並告訴我它的意思,那將很棒。 可以在這里找到完整的內容,但這是我要特別閱讀的內容:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM