从Action Script 3中的类访问root

时间:2016-11-15 06:24:51

标签: flash actionscript

它会抛出以下警告,但仍会发出警告。

TypeError:错误#1009:无法访问空对象引用的属性或方法。 at play_fla :: MainTimeline / play_fla :: frame1()

这是工作代码

package {
    import flash.events.KeyboardEvent;
    import flash.events.MouseEvent;
    import flash.display.SimpleButton;
    import flash.display.*;
    import flash.text.*; 
    import flash.events.Event;
    import flash.display.MovieClip;

    public class ClickButton extends SimpleButton {
    public var fLabel:String;
    public var sName:String;
    public var sNumber:Number;

    public function ClickButton()
    {

    }    

    public function GotoSession(sesBut:SimpleButton, frameLabel:String, sceneName:String):void {           
    sesBut.addEventListener(MouseEvent.CLICK, gotoSes);         
    function gotoSes(event:MouseEvent):void {
    trace("sesBut = " + sesBut.name);
    trace("frameLabel = " + frameLabel);
    trace("sceneName = " + sceneName);
    trace("this.stage = " + this.stage);
    trace("this.root = " + this.root);
    MovieClip(root).gotoAndStop(frameLabel, sceneName);
    }
    }
}
}

它还跟踪以下

sesBut = home,frameLabel = menu,sceneName = Home,this.stage = undefined,this.root = undefined

this.stage和this.root未定义

1 个答案:

答案 0 :(得分:0)

我真的不明白为什么你需要为此目的写这个课程,但你可以尝试这个来解决这个问题。

类ClickButton:

package {
    import flash.events.MouseEvent;
    import flash.display.SimpleButton;
    import flash.display.*;

    public class ClickButton extends SimpleButton{
        public var fLabel:String;
        public var sName:String;

        function ClickButton() {

        }
        public function GotoSession(sesBut:SimpleButton, frameLabel:String, sceneName:String):void {
            fLabel = frameLabel;
            sName = sceneName;
            trace("sesBut = " + sesBut);
            trace("frameLabel = " + frameLabel);
            trace("sceneName = " + sceneName);
            trace("this.stage = " + this.stage);
            trace("this.root = " + this.root);
        }
    }
}

主时间轴第1帧:

import ClickButton;
import flash.display.SimpleButton;

var cb_1ClickButton = new ClickButton();
addChild(cb_1ClickButton);
cb_1ClickButton.GotoSession(new SimpleButton, "frameLabel", "sceneName");

输出:

sesBut = [object SimpleButton]
frameLabel = frameLabel
sceneName = sceneName
this.stage = [object Stage]
this.root = [object MainTimeline]

在ClickButton不是Event.ADDED或Event.ADDED_TO_STAGE无效之前,您可能无法访问root或stage。 添加实例时,您只能引用root或stage。

如果你想出全局,你可以自己搜索和调试。 ;)