【问题标题】:issues passing variables from parent to child. AS3将变量从父级传递给子级的问题。 AS3
【发布时间】:2011-10-15 21:23:32
【问题描述】:

我正在尝试从我的子 MC 中的父级访问一些变量。

父代码:

var data_history:String;    
function finish_checkUp(event:Event):void{          
        var checkUp_stat:String;
        checkUp_stat = data.check_UP_STAT;
        if (checkUp_stat == "PASSED"){
            data_history = "FALSE";
            gotoAndPlay ("domain_check");
        }
        else if (checkUp_stat == "FAILED"){
            data_history = "TRUE";
            gotoAndPlay ("error_data_conflict");
        }
        else if (checkUp_stat == "FAILED_UN"){
            data_history = "TRUE";
            gotoAndPlay ("");
        }   
}

儿童主持人:

 contt_btn.addEventListener(MouseEvent.MOUSE_DOWN, mouseClick);
    contt_btn.addEventListener(MouseEvent.ROLL_OVER,contt_btnOver);
    contt_btn.addEventListener(MouseEvent.ROLL_OUT,contt_btnOut);
    function contt_btnOver(event:MouseEvent):void{
        contt_btn.gotoAndPlay("over");
    }
    function contt_btnOut(event:MouseEvent):void{
        contt_btn.gotoAndPlay("out");
    }
    function mouseClick(event:MouseEvent):void
    {
        trace (MovieClip(this.parent).data_history);
        if (data_history == "TRUE"){
    MovieClip(parent).gotoAndPlay("begin_erasing");
        }
        else if (data_history == "FALSE"){
            gotoAndPlay("");}
    }

现在如您所见,我尝试了trace 方法,但没有成功。 Flash 不会报告有关trace 方法的任何错误,但会报告两个未定义的变量(data_history)。我尝试在脚本顶部的所有函数之上使用跟踪方法,但仍然出现相同的错误。

有什么想法吗?

【问题讨论】:

    标签: flash actionscript-3 actionscript


    【解决方案1】:

    在跟踪中,您通过 this.parent 引用 data_history 属性。假设跟踪您的值,您需要调整 if...else 以通过 parent 引用该属性:

    function mouseClick(event:MouseEvent):void
    {
        trace (MovieClip(this.parent).data_history);
        if (MovieClip(this.parent).data_history == "TRUE"){
    MovieClip(parent).gotoAndPlay("begin_erasing");
        }
        else if (MovieClip(this.parent).data_history == "FALSE"){
            gotoAndPlay("");}
    }
    

    如果您在其中的跟踪引发错误,则该属性从不存在于父级上。

    【讨论】:

      【解决方案2】:

      子电影不应该以这种方式检查其父电影。

      试试这个:

      在孩子的文档类中:

      public var data_history:String;
      function mouseClick(event:MouseEvent):void{
                 if (data_history == "TRUE"){
                        MovieClip(parent).gotoAndPlay("begin_erasing");
                 }
                 else if (data_history == "FALSE"){
                         gotoAndPlay("");}
                 }
       }
      

      在父级中

      function finish_checkUp(event:Event):void{ 
                var checkUp_stat:String;
                checkUp_stat = data.check_UP_STAT;
                if (checkUp_stat == "PASSED"){
                    data_history = "FALSE";
                    if (childMC as ChildDocumentClass) {
                        (childMC as ChildDocumentClass).data_history = data_history;
                    }
                    gotoAndPlay ("domain_check");
                }
                else if (checkUp_stat == "FAILED"){
                    data_history = "TRUE";
                    if (childMC as ChildDocumentClass) {
                        (childMC as ChildDocumentClass).data_history = data_history;
                    }
                    gotoAndPlay ("error_data_conflict");
                }
                else if (checkUp_stat == "FAILED_UN"){
                    data_history = "TRUE";
                    if (childMC as ChildDocumentClass) {
                        (childMC as ChildDocumentClass).data_history = data_history;
                    }
                    gotoAndPlay ("");
                }
        }  
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-12-08
        • 2018-09-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多