基本上我编码通过单击按钮在场景之间切换。我给帧标签砂场景名称作为参数。 MovieClip(root).gotoAndStop(frameLabel,sceneName);在舞台上工作得很好。但是当我在类上使用相同的时候会抛出警告TypeError:错误#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");
答案 0 :(得分:1)
这里有两个问题,我真的不明白你为什么这样工作,但是,
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,这会将我带到标签“menu”的“Home”场景。
这就像一个魅力。
在frameLabel“菜单”上:
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为null(你没有任何对名为home的ClickButton的引用)...... ???
答案 1 :(得分:1)
您也可以创建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的符号属性:
这只是因为我不知道为什么你必须将Button作为参数传递给你,如果你调用这个实例的方法... 就像你在原始问题中所做的那样:
//btn1.GotoSession(btn1, "menu", "Home");
这在ClickButton类中很奇怪......