需要帮助理解代码

本文关键字:代码 助理 帮助 | 更新日期: 2023-09-27 18:13:15

所以这个程序有精灵。每个精灵所拥有的帧数都有一个限制,我正试图弄清楚如何了解这个限制,以便我能够修改它。但是我读的代码对我来说真的很难。我一直在阅读它使用的一些东西(如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;
    }
}