【问题标题】:Building a new Dictionary out of an old one? Help with Dictionary recursion用旧词典构建新词典?帮助字典递归
【发布时间】:2010-09-01 07:40:30
【问题描述】:

我正在使用大量分层分类术语,其中每个术语(“203”)在舞台上都有一个匹配的“term203”电影剪辑,并且无法让递归函数返回所有给定的术语的后代。

有一个主要的Dictionary() 对象,每个术语都有以下嵌套组织:

{ [object Movie Clip] : { "tid":203, "parent":99, "name":"Culture", selected:false, "otherData":"etc" } }

...[object Movie Clip] 的实例名称将是 "term203"。所有这些 object:subObjectArray 项(“术语”)都存储在主 taxonomy:Dictionary() 对象中。

我一直在尝试制作一个递归函数(它本身已经有点超出我的想象了),它接受影片剪辑的click.target 并返回一个新的Dictionary() 对象与该术语的所有子孙和曾孙(等)在上述同一个嵌套组织中。

下面的代码跟踪正确数量的递归循环,但返回的 Dictionary() 对象仅包含第一次运行的术语(仅包含所请求术语的直接子项)。

var taxonomy:Dictionary = new Dictionary();

// ...Term info is loaded into taxonomy from a JSON-style text file)
// ...MOUSE_OVER event listeners are added to each

function revealChildren(hvr:MouseEvent):void {

    trace("Spotlighting " + taxonomy[hvr.target].name + "'s children...");

    for(var key:Object in getAllChildren(taxonomy[hvr.target].tid)) {
        trace("Animating " + taxonomy[key].tid); // Traces only immediate children
        var revealTween = new Tween(key, "alpha", Regular.easeInOut, key.alpha, 1, 1, true);
    }
}

function getAllChildren(origin):Dictionary {

    var children:Dictionary = new Dictionary();

    for(var element:Object in taxonomy) {
        if(taxonomy[element].parent == origin) {
            var subSet = getAllChildren(taxonomy[element].tid);
            children[element] = subSet; // *CAN'T ACCESS 'subSet' PROPERLY*
            trace("Parent = " + origin);
            trace("Matched! Adding " + taxonomy[element].tid + " as key and setting its value to " + subSet); // Traces correct amount of times, one for each descendent
        }
        else {
        }
    }
    return children;
}

我当然不声称自己是最高效的 AS3 程序员,所以我对其他配置持开放态度。但是,在尝试了静态和嵌套数组之后,我更愿意继续使用 Dictionary() 对象作为我的主池。

如前所述,只有直接子代最终会在 revealChildren() 函数中生成动画。这让我很困惑,在getAllChildren() 函数中,所有后代在输出窗口中按顺序(没有特定顺序)跟踪。

我也无法从subSet 对象中获取任何名称或属性。这可能是问题所在。

我只测试了“两代”,但似乎只有第一轮调用该函数成功地将这些术语添加到新的 Dictionary() 对象并将其原封不动地返回给动画函数。

太糟糕了dict.filter(getDescendants) 不起作用。请帮忙!

【问题讨论】:

    标签: actionscript-3 dictionary recursion


    【解决方案1】:

    为了简化,我添加了一个名为children 的输出参数。这是我们的函数将存储其结果的Dictionary。它有一个默认值,所以你不需要指定一个。在这种情况下,它将为自己创建一个新实例。

    function getAllChildren(origin:*, children:Dictionary = null):Dictionary {
        if (children = null) children = new Dictionary();
    
        for(var element:* in taxonomy) {
            if(taxonomy[element].parent == origin) {
                children[element] = taxonomy[element];
                getAllChildren(taxonomy[element].tid, children);
            }
        }
        return children;
    }
    

    当一个孩子被发现时,它会被完全复制:children[element] = taxonomy[element];
    接下来,函数递归调用自身,为其提供与它一直使用的相同的输出字典。


    编辑:
    回应您的评论...您的代码最初是在找到一个名为 element 的孩子后说的:
    children[element] = getAllChildren(taxonomy[element].tid);
    

    您在这里使children[element] 等于Dictionary 对象。您创建的是一个树结构,将MovieClip 对象映射到包含其子代的类似映射的Dictionary 对象。在这个结构上使用for in 循环只会给你顶级的孩子。它不会递归遍历整个树。

    【讨论】:

    • 像魅力一样工作 - 非常感谢!我不知道我可以对函数参数如此灵活。
    • 为了概念上的理解,有人能解释一下为什么原来的递归没有按预期工作吗?
    • @atw:我在答案末尾添加了解释。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-20
    • 2019-01-28
    • 2019-08-15
    • 1970-01-01
    • 2022-12-09
    • 1970-01-01
    相关资源
    最近更新 更多