来自 class Action Script 3 的引用阶段

Referring stage from the class Action Script 3

基本上我正在编写代码以通过单击按钮在场景之间切换。我将帧标签沙子场景名称作为参数。 MovieClip(root).gotoAndStop(frameLabel, sceneName);在舞台上工作得很好。但是当我在 class 上使用相同的方法时,它会抛出警告 TypeError: Error #1009: Cannot access a 属性 or method of a null object reference。我知道它会发生,因为 class.Is 没有 root 有任何方法可以修复它。请在下面找到代码。

//class代码

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 {       
    MovieClip(root).gotoAndStop(frameLabel, sceneName);
    }
    }
}

//AS3编码

var btn1 = new ClickButton();
addChild(btn1);
btn1.GotoSession(home, "menu", "Home");

你这里有两个问题,我真的不明白你为什么这样工作但是,

1:当我尝试编译您的代码时,出现编译时错误:

您的 Class 末尾缺少一个“}”。

我删除了不用的:

package {
    import flash.events.MouseEvent;
    import flash.display.SimpleButton;
    import flash.display.MovieClip;
    public class ClickButton extends SimpleButton {
        public function ClickButton() {
        }
        public function GotoSession(sesBut:SimpleButton,frameLabel:String,sceneName:String):void {
            sesBut.addEventListener(MouseEvent.CLICK,gotoSes);
            function gotoSes(event:MouseEvent):void {
                MovieClip(root).gotoAndStop(frameLabel,sceneName);
               // and if You want to remove the ClickButton instance :;
               // ADD those two lines :
               sesBut.removeEventListener(MouseEvent.CLICK,gotoSes);
               MovieClip(root).removeChild(sesBut);
               // DO NOT forget to remove the Listeners before to remove an instance!
            }
        }
    }
}

我想您的库中有一个链接到 ClickButton Class 的按钮,如下所示:

所以:

var btn1:ClickButton = new ClickButton();
addChild(btn1);
btn1.GotoSession(btn1, "menu", "Home");
stop();

如果我单击 btn1,这会将我带到标签 "menu" 处的场景 "Home"。

这很有魅力。

在框架标签上 "menu" :

stop();
trace("currentScene.name = " + this.currentScene.name);
trace("currentFrameLabel = " + this.currentFrameLabel);

/*
OUTPUT : 
currentScene.name = Home
currentFrameLabel = menu
*/

[编辑]

如果我将可见性设置为 false,然后再次设置为 true,如果我想更改 mc_1 的 alpha 属性,我会遇到同样的问题。 这适用于我的文件:

import flash.display.MovieClip;
stop();
trace("currentScene.name = " + this.currentScene.name);
trace("currentFrameLabel = " + this.currentFrameLabel);
var mc_1:MovieClip = mc_1;
// If I don't add this line, I have the same problem when I set the visibility to true
var mc_2:MovieClip = mask_mc;
var mc_3:MovieClip = red_mc;
// I do the same for mc_2 labeled "mask_mc"
// mc_1 is now always recognized as a MovieClip as mc2.
mc_1.visible = false;
mc_1.visible = true;
// No more problem if I add the line var mc_1:MovieClip = mc_1;
// If I don't do this, I cannot access mc_1 as a MovieClip
mc_1.alpha = 0.5;
mc_1.mask = mc_2;
mc_3.alpha = 0.5;
mc_3.visible = false;
mc_3.visible = true;
mc_3.alpha = 0.9;
// It seems that You have to declare the MC variables before to change the properties

[/编辑]

但我不明白你的台词:

//btn1.GotoSession(home, "menu", "Home");

home 为空(您没有对名为 home 的 ClickButton 的任何引用)...???

您也可以创建一个 ClickSomeButton Class 使用静态方法 "gotoLand"

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

public class ClickSomeButton{
    public static function gotoLand(target:MovieClip,sesBut:SimpleButton,frameLabel:String,sceneName:String):void {
            sesBut.addEventListener(MouseEvent.CLICK,gotoSes);
            function gotoSes(event:MouseEvent):void {
                target.gotoAndStop(frameLabel,sceneName);
                target.removeEventListener(MouseEvent.CLICK,gotoSes);
                target.removeChild(sesBut);
            }
        }
    }
}

然后在你的 fla 中:

var btn2:Btn2 = new Btn2();
addChild(btn2);
btn2.x = 200;
ClickSomeButton.gotoLand(this,btn2, "menu", "Home")
stop();

库中 Btn2 的符号属性:

这只是因为我不知道为什么如果你调用这个实例的方法,你必须将 Button 作为参数传递... 就像您在原始问题中所做的那样:

//btn1.GotoSession(btn1, "menu", "Home");

这在您的 ClickButton 中很奇怪 Class...