简体   繁体   中英

AS3 accessing stage from class

Timeline code:

import as3.first;

first._this=this;

var str1:String='this is timeline';

Class code:

package as3 {

import flash.display.MovieClip;

public class first extends MovieClip {

public static var _this:Object;

trace(_this.str1);

}

}

Error message:

TypeError: Error #1009: Cannot access a property or method of a null object reference.

Trying to wrap my mind around how classes work. Need to access timeline functions and variables from class. What am I doing wrong and how can I make this work?

All in all what you are doing is somewhat weird. May be, you just want a document class for your SWF root? You as well could add a class to any movieclip in your library: both ways grant you access to timeline.

package as3 
{
    import flash.display.MovieClip;
    public class first extends MovieClip 
    {
        public static var _this:Object;
        trace(_this.str1); // you may place code here... but consider this:
                           // this area is STATIC, the code here 
                           // executes only once when class gets initialized,
                           // so, this happens BEFORE you assign first._this=this;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM