问题
It throws the following warning but works in spite of warning.
TypeError: Error #1009: Cannot access a property or method of a null object reference. at play_fla::MainTimeline/play_fla::frame1()
Here is the working code
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);
}
}
}
}
Also it traces the following
sesBut = home, frameLabel = menu, sceneName = Home, this.stage = undefined, this.root = undefined
this.stage and this.root is undefined
回答1:
I really don't understand why You need to write this class for this purpose, but You may try this anyway to figure You out the issue.
Class 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);
}
}
}
Main Timeline frame 1 :
import ClickButton;
import flash.display.SimpleButton;
var cb_1ClickButton = new ClickButton();
addChild(cb_1ClickButton);
cb_1ClickButton.GotoSession(new SimpleButton, "frameLabel", "sceneName");
Output :
sesBut = [object SimpleButton]
frameLabel = frameLabel
sceneName = sceneName
this.stage = [object Stage]
this.root = [object MainTimeline]
You may not access the root nor stage until the ClickButton is not Event.ADDED or Event.ADDED_TO_STAGE is not effective. You may only refer to root or stage when the instance is added.
If you figure You out the big picture, You may search and debug this by Yourself. ;)
来源:https://stackoverflow.com/questions/40603368/accessing-root-from-the-class-in-action-script-3