Bdcoll
Bdcoll

Reputation: 3

Calling a function from one class, from another

I'm having a slight issue with ActionScript 3 and I have come here to ask for some help.

I have two classes. One called Sledge and one called Sock, there is also the document class called Main.

My issues are as follows:

Upvotes: 0

Views: 119

Answers (1)

Jonathan Dunlap
Jonathan Dunlap

Reputation: 2631

There's some ambiguity issues with how you expressed your question. It would help if you posted a short form of the code for the problem.

However, I'll try to answer the first question:

Inside of Sledge, I call a function that is defined inside of the Main document class. How would I go about telling the class to go to the document class and run that function?

You would want to pass the Main class to the Sledge class or use events which is preferable. If pass the class it will look like this...

class Sledge {
   private var main:Main;
   function Sledge(main:Main) {
      this.main = main;
   }
   function doSomething():void {
     main.runSomeFunction();
   }
}

Or if using events:

class Main {
private var sledge:Sledge;
   function Main() {
      sledge = new Sledge();
      sledge.addEventListener("mainDoSomething", doSomething);
   }
   private function doSomething(e:Event):void {
     // .... do stuff
   }
}
class Sledge extends EventDispacter {
   function Sledge() {
   }
   public function doSomething():void {
     dispatchEvent(new Event("mainDoSomething"));
   }
}

Upvotes: 1

Related Questions