Reputation: 1627
Quick question. I have been Googling this all morning, but it's either not there, or else written in a way that doesn't register. I am inclined to believe the latter, as this seems like it should be something completely trivial to me.
I made a small Flash file using AS 3.0, and this is the first time I've really been able to stick to the OOP way of doing things and not hack together a mix of stuff from the timeline to get around not having everything work in the classes.
So I'd like to keep it that way, but one thing is eluding me: I can't call a method of an instance of another class (than the one I'm calling from) without resorting to "DocumentClass(root).instanceName.method."
Intuition tells me there has to be a better way of doing this (like, without having to reference the document class every time I call another class instance's function; and CERTAINLY without having to use the word "root" - that just seems so Flash 5 to me.
Does anybody have a better way of doing this that they can share?
Thanks in advance! SS
edit: Here's some relevant code: Main class:
package myApp {
import flash.display.*;
import flash.events.*;
public class Main extends flash.display.MovieClip {
public var background;
public var control;
public function Main() {
//load the background
this.background = new Background();
this.addChild(background);
//load the control
this.control = new Control;
this.addChild(control);
}
}
}
Background class:
package myApp {
import flash.display.*;
import flash.events.*;
public class Background extends flash.display.MovieClip {
public function Background() {
this.x = 0;
this.y = 0;
}
public function doSomething() {
//do something here
}
}
}
Control class:
package myApp {
import flash.display.*;
import flash.events.*;
public class Control extends flash.display.MovieClip {
private var section01;
private var section02;
private var section03;
public function Control() {
//some constructor code here
}
public function doSomethingCaller() {
MyApp(root).background.doSomething(); <--- HERE
}
}
}
Upvotes: 0
Views: 1360
Reputation: 3207
You can try the following approach:
package
{
import com.A;
import com.B;
import flash.display.MovieClip;
public class Main extends MovieClip
{
private var _a:A;
private var _b:B;
public function get a():A
{
return _a;
}// end function
public function Main():void
{
init();
}// end function
private function init():void
{
_a = new A();
_b = new B(this);
}// end function
}// end class
}// end package
In your document class "Main", classes "A" and "B" are imported. The private properties "_a" and "_b" are declared in the "Main" class for the "A" and "B" class. Next there is a getter method called "a" that returns "_a". Next there is a private method called "init()" that you call from the "Main" class's constructor. In the method "init()", the "_a" and "_b" properties are instantiated, however for "_b", a reference to "Main" is parsed using the "this" keyword.
package com
{
import flash.display.MovieClip;
public class A extends MovieClip
{
public function A():void {}// end function
public function traceClassName():void
{
trace("Class name: A");
}// end function
}// end class
}// end package
In the class "A" there is a public method called "traceClassName()" that traces the string "Class name: A" when invoked.
package com
{
public class B
{
private var _main;
public function B(p_main):void
{
_main = p_main;
_main.a.traceClassName();
}// end function
}// end class
}// end package
In the class "B" you have a private property called "_main". In the "B" class's constructor you have a parameter for the reference of the Main class to be parsed when the "B" class is instantiated. The "_main" property is then assigned the reference to the "Main" class. Now you can access the instance of "A" through the reference of the "Main" class from the "B" class.
There are many approaches, this is just one of the simpler ways, although I think there are better ones. You should present the source code for your flash app/movie, so I and everyone else can get a better idea of which would suit you.
I hope this helped :)
Upvotes: 1