I am currently learning to use separate .as files and classes, but after reading a lot everything seems to work different from what I've read. I'm posting this question for the purpose of learning, not just to make the code work. The examples here are tested, simplified recreations of my real project.
My file "MyApp.fla" has 1 frame with a shape as background, the DocumentClass is set to "MyApp". The library holds a symbol "Page1" of 1 frame with another background shape, with it's class set to "Page1"
MyApp.as:
package {
trace("1: DocumentClass file, before class");
import flash.display.MovieClip;
public class MyApp extends MovieClip {
trace("2: DocumentClass file, in the class")
public var setting1:int = 2; //this is a variable which i want to be accesible to other classes, so to the pages being loaded
private var currentPage:MovieClip; //I wanted to create this var in the constructor, but I'm afraid something will explode again :<
public function MyApp() {
trace("3: DocumentClass file, in constructor function");
currentPage = new Page1;
addChild(currentPage);
}
}
}
Page1.as:
package {
trace("4: Page1 file, before class");
import flash.display.MovieClip;
public class Page1 extends MovieClip {
trace("5: Page1 file, in class, before constructor");
public function Page1() {
trace("6: Page1 file, in constructor")
trace(setting1) //According to everything i read this should work since setting1 is public, but it gives me: "1120 Acces of undefined property setting1" so i commented this out for the output.
trace(root);
trace(stage); //both trace null since i haven't used addChild() yet, but now i dont know how to try to reference my variables through the main timeline.
}
}
}
Output:
While both background shapes display as expected the generated output is completely out of order from what i expected. My main question is: Why are my traces not ordered 1 -6?
My next question: Why can't the Page1() constructor refer to the public var setting1?, i assume i can work around by passing setting1 as an argument into the Page1() constructor, but I'm avoiding that for learning purposes.
setting1 is a variable in the parent object, not the object you are attempting to call it from. This just wont work.
trace(this.parent.setting1);
trace(MovieClip(parent).setting1);
Replacing it with ONE of the above should work, as you'd be calling the variable form where it is referenced, which is the parent.
The reason for the order is:
5 - the class itself is imported before anything else
4 - while the class loads, it checks for additional imports
2 - after it finished importing all classes, it loads its own class
1 - again, while the class loads it check for imports
3 - after everything is loaded, it's free to run the main function of the class
6 - same as 3, but the child always walks behind the parent
Remember in object orientated programming, it needs to load ALL the objects (or classes) before it tries to do anything with them. Imagine if subclass.as had a variable and class.as tried to access it before it loaded.
Hope this helps :). If you need more details feel free to ask, I kinda just summarized each step.