开发者问题收集

从 Action Script 3 类引用阶段

2016-11-17
61

基本上,我编写的代码是通过单击按钮在场景之间切换。我将帧标签和场景名称作为参数。MovieClip(root).gotoAndStop(frameLabel, sceneName); 在舞台上工作正常。但是当我在类上使用相同代码时,它会引发警告 TypeError:错误 #1009:无法访问空对象引用的属性或方法。我知道这是因为类没有根。有什么方法可以修复它吗?请查看下面的代码。

//类代码

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");
2个回答

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

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

你的课程末尾缺少“}”。

我删除了未使用的内容:

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 类,如下所示:

在此处输入图像描述

所以:

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

如果我单击 btn1,这会将我带到标签处的“主页”场景“菜单”。

这非常有效。

在 frameLabel “菜单”上:

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

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

[编辑]

如果我将visibility设置为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的任何引用)......???

tatactic
2016-11-18

您还可以创建一个 ClickSomeButton 类 带有静态方法“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 的符号属性:

library symbol properties

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

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

您的 ClickButton 类中的这一点很奇怪...

tatactic
2016-11-18