【问题标题】:I am not able to manipulate a Symbol that is nested within another Symbol我无法操作嵌套在另一个符号中的符号
【发布时间】:2017-03-03 18:23:34
【问题描述】:

我是一名新的编码员,也是第一次提问。我正在开发一个程序,该程序显示一个数学问题,其中每个数字都由一个符号表示。为了更容易操纵问题的大小,我想制作一个符号“问题”并将“数字”的符号放入其中。 “问题”是舞台上符号“问题”实例的名称。不同的数字是符号“数字”的实例,并命名为“数字01,数字02,数字03,数字11 ...”直到“数字33”我希望程序循环遍历所有数字以使它们停在第一帧。

这是我的代码的相关部分:

for (var u: int = 0; u < 4;u++)
{
   for (var v: int = 1; v < 4;v++)
   {
       this["problem.digit" + u + v].gotoAndStop(1);
   }
}

当我运行此程序时,我收到错误 #1069。 “在 Main_Math 上找不到problem.digit01,并且没有默认值。”

顺便说一句,当我像这样单独写数字时它会起作用:

problem.digit01.gotoAndStop(1);
problem.digit02.gotoAndStop(2);

等等。

感谢您提供的任何指导。谢谢!

【问题讨论】:

    标签: actionscript-3 nested symbols


    【解决方案1】:

    寻址不是那样工作的。您不能使用单个 [] 操作来处理孩子的孩子。此外,最好使用 getChildByName 方法,因为点语法访问仅适用于发布选项“自动声明阶段实例”。

    for (var u: int = 0; u < 4;u++)
    {
        for (var v: int = 1; v < 4;v++)
        {
            // Get a reference to a child and typecast it as MovieClip.
            var aDigit:MovieClip = problem.getChildByName("digit" + u + "" + v) as MovieClip;
            aDigit.gotoAndStop(1);
        }
    }
    

    让我们解释一些事情。

    首先,MovieClip 是一个动态类,这意味着您可以读取和写入其实例成员而无需实际声明它们(点和括号语法之间的区别在这里解释 Using . or [ ] to access Object properties - what's the difference? ):

    var M:MovieClip = new MovieClip;
    
    trace(M['a']); // undefined, no error raised
    M['a'] = 1;
    trace(M['a']); // 1
    trace(M.a]);   // 1
    

    其次,MovieClip 是一个 DisplayObjectContainer,因此它可以包含子项。这些孩子有名字,您可以通过调用 getChildByName("child name going here") 方法来引用它们,并且不要忘记对结果进行类型转换,因为有各种各样的孩子,因此默认情况下它们由其基 DisplayObject 类键入.子名称与对象成员名称相同(不过,正如我之前提到的,“自动声明阶段实例”在幕后将子引用分配给同名变量):

    // We proceed working with M from above.
    
    // Lets create a child for M.
    var C:MovieClip = new MovieClip;
    
    // MovieClip C has a name "D" as of now.
    C.name = "D";
    
    // C named "D" becomes a child of M.
    M.addChild(C);
    
    trace(M.C); // undefined, because C is not a member of M object, it's just a local variable
    trace(M.D); // undefined, because D is the name of MovieClip which is child of M MovieClip but not a member of M objest.
    trace(M.getChildByName("C")); // null, because M has no children with name "C".
    trace(M.getChildByName("D")); // [object MovieClip] because there is indeed a child with the name "D" inside of M.
    
    // We create a field with the name "E" inside M object.
    M.E = C;
    trace(M.getChildByName("E")); // null, because M has no children with name "E".
    trace(M.E); // [object MovieClip] because M now contains a field E with the reference to MovieClip C named "D".
    

    【讨论】:

    • @MichaelM 不要忘记标记正确答案。 :)
    • @MichaelM 经过一番思考后,我添加了一些解释,这可能有助于您了解这些事情的工作原理。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-08
    • 2011-10-10
    • 2019-05-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多