在Action Script 3中从类访问根 [英] accessing root from the class in Action Script 3

查看:161
本文介绍了在Action Script 3中从类访问根的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它抛出以下警告,但工作,尽管警告。

TypeError:错误#1009:无法访问空对象引用的属性或方法。在play_fla :: MainTimeline / play_fla :: frame1()



以下是工作代码

  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;
$ b $ public function ClickButton()
{

}

public function GotoSession(sesBut:SimpleButton,frameLabel:String,sceneName:String ):void {
sesBut.addEventListener(MouseEvent.CLICK,gotoSes);
函数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);



$ b $ / code>

它跟踪以下内容:

sesBut = home,frameLabel = menu,sceneName = Home,this.stage = undefined,this.root = undefined

this.stage and this.root is undefined

解决方案

我真的不明白你为什么需要为此目的编写这个类,但是你可以试试这个来解决你的问题。

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;

函数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);主时间轴1:





  import ClickButton; 
进口flash.display.SimpleButton;

var cb_1ClickButton = new ClickButton();
的addChild(cb_1ClickButton);
cb_1ClickButton.GotoSession(new SimpleButton,frameLabel,sceneName);

输出:

  sesBut = [object SimpleButton] 
frameLabel = frameLabel
sceneName = sceneName
this.stage = [object Stage]
this.root = [object MainTimeline]

在ClickButton不是Event.ADDED或Event.ADDED_TO_STAGE不是有效。
当实例被添加时,您只能引用root或stage。



如果您想象出大局,您可以通过自己搜索和调试。 ;)

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

解决方案

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. ;)

这篇关于在Action Script 3中从类访问根的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆