If I have a document class:
package {
import flash.display.MovieClip;
public class Main extends MovieClip {
public function Main() {
}
public function SomeRandomMethod():void {
}
}
}
How can I call SomeRandomMethod from here:
package {
public class AnotherClass {
public function AnotherClass() {
}
public function AnotherRandomMethod():void {
/* I need to use SomeRandomMethod here */
}
}
}
There are a few ways to achieve this. One way would be to pass a reference of the document class to the constructor of the other class:
or to the function itself
You could also use a singleton design pattern by declaring a global static variable and assigning the document class to it. Although singletons are regarded as an anti-pattern. For example:
then
Another way would be to make use of the Service Locator pattern (although some view it as an anti-pattern too). http://gameprogrammingpatterns.com/service-locator.html