Search code examples
flashactionscript-2flash-cs6

Can variables from the main script be read inside of class functions?


I'm learning ActionScript 2.0 by dissecting some freely available source code and modifying it, and I've run into a little hitch.

I've got 3 .as files that go with my .fla and they are:

"script.as" contains all of the basic functions that go with the flash interface I've built.
"skin.as" contains the interface skin class declaration and class functions.
"defaultskin.as" contains a class declaration and functions that extend the "skin" class.

The first frame of the .fla simply #includes "script.as" and I have linked the "interface" movieclip (which is my main flash interface) to the "defaultskin" class through the right click / properties menu under ActionScript linkage.

I've got a variable declared in "script.as" with a simple var statement. Can this variable be read in one of the functions inside of "skin.as" in one of the class functions as is? Or do I need to do something special for it to be readable to that class?

How can this be done without making the variable a public static and without passing it as an argument of the function?

For additional clarification, script.as looks something like this (assume that m_interface is the name of the movieclip object):

//imports

var myVar;

onLoad()
{
 m_interface.Initialize();
}

skin.as looks something like:

//imports

class skin
{
 //variable declarations

 public function skin()
 {
  // code
 }

 function Initialize()
 {
  trace(myVar);
 }
}

without writing all of the fluff code, defaultskin.as declares a class as such:

class defaultskin extends skin

Solution

  • by default then you use var it gives it internal access modifier. so it could be accessed only by the class or any class within the same package so if you don't use any other modifiers, or not pass this varible somehow to your class, the answer is "no, it is not accessable" if you use public modifier, you give access to variable to any class, but there is some other restrictions as you already noticed to be accessed you add static modifier, which means you do not need to make instance of class to have access to it public static variable.

    so, to access variable of one class inside another you need this class be imported and this variable be static. if it not static you should create instance of class before you could have access

    var newClassInstance = new ClassName();
    trace(newClassInstance.varible);
    

    the other way is then your class skin instance situated inside script.as class... so somewhere inside script.as there is the variable with reference to instance of skin. in that case you could have access to variables of script.as instance using _parent from skin.as