Flash 参考集体行动脚本3中的阶段

Flash 参考集体行动脚本3中的阶段,flash,actionscript,Flash,Actionscript,基本上我是通过点击按钮在场景之间切换。我将帧标签和场景名称作为参数。MovieClip(root).gotoAndStop(frameLabel,sceneName);在舞台上表演得很好。但当我在类上使用相同的时,它抛出警告TypeError:Error#1009:无法访问null对象引用的属性或方法。我知道这是因为类没有根。有没有办法修复它。请在下面查找代码 //类别代码 package { import flash.events.KeyboardEvent; import f

基本上我是通过点击按钮在场景之间切换。我将帧标签和场景名称作为参数。MovieClip(root).gotoAndStop(frameLabel,sceneName);在舞台上表演得很好。但当我在类上使用相同的时,它抛出警告TypeError:Error#1009:无法访问null对象引用的属性或方法。我知道这是因为类没有根。有没有办法修复它。请在下面查找代码

//类别代码

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:当我试图编译你的代码时,我得到一个编译时错误:

你的课结束时少了一个“}”

我删除了未使用的内容:

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

因此:

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

这很有魅力

在框架标签“菜单”上:

[编辑]

如果我将可见性设置为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");

主页为空(您没有对名为主页的ClickButton的任何引用)

您还可以创建ClickSomeButton类 使用静态方法“gotoLand”

然后在你的佛罗里达州:

var btn2:Btn2 = new Btn2();
addChild(btn2);
btn2.x = 200;
ClickSomeButton.gotoLand(this,btn2, "menu", "Home")
stop();
库中Btn2的符号属性:

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

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

这在你的ClickButton类中很奇怪…

为你高兴@Rajesh我从未在AS3中创建过场景,所以我甚至现在都不知道如何创建一个场景,这对我来说是最困难的。但我认为你可以改进你的代码…所以这就是你想问的?现在更清楚了+1谢谢你的问题。是的,我以前也是这样。但是错误TypeError:error#1009:无法访问空对象引用的属性或方法,因为我已将某些电影剪辑对象的可见性设置为false,即mc1.visible=false,它位于该位置。当我评论这些行时,这个警告没有出现。这很奇怪。问题似乎不同,我在我的文件中尝试了这个,但在我登陆的地方没有发现任何问题:(mc1.visible=false没有给我任何错误消息。我不明白这个问题!@Rajesh奇怪!@Rajesh:我编辑了答案,我认为问题就在那里。如果mc_1没有明确声明为MovieClip,我无法更改alpha属性。因此,如果我添加var mc_1:MovieClip=mc_1,就没有问题了;
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);
            }
        }
    }
}
var btn2:Btn2 = new Btn2();
addChild(btn2);
btn2.x = 200;
ClickSomeButton.gotoLand(this,btn2, "menu", "Home")
stop();
//btn1.GotoSession(btn1, "menu", "Home");