Skip to main content
Participating Frequently
July 5, 2017
Answered

Calling functions from the document class in other classes

  • July 5, 2017
  • 1 reply
  • 728 views

Hi there! I'm fairly new to ActionScript and as the title suggests I'm trying to call a function in the document class from another class. I've tried a few methods online such as this that people suggest:

class AnotherClass
{
  
private var _main:Main;

  
public function AnotherClass(main:Main)
  
{
  _main
= main;
  _main
.test(); // Success!
  
}
}

class Main
{
  
public function Main()
  
{
  
var another:AnotherClass = new AnotherClass(this);
  
}

  
public function test():void
  
{
  trace
("Success!");
  
}
}

This doesn't work, I get errors where it can't find what main is....does anyone have a better solution or one that won't produce errors? Thanks!

This topic has been closed for replies.
Correct answer Colin Holgate

Sorry, I'm new to coding, how would I go about creating a public method? Thanks!


Here's your code changed to make 'another' and then do the test dispatch:

package {

  import flash.display.MovieClip;

  import flash.events.Event;

  import Code.AnotherClass;

  public class Main extends MovieClip {

  var another: AnotherClass = new AnotherClass();

  public function Main() {

  another.addEventListener('testE', testF);

  another.testNow();

  }

  private function testF(e: Event): void {

  trace("Success!, called from", e.currentTarget);

  }

  }

}

package Code {

  import Main;

  import flash.display.MovieClip;

  import flash.events.Event;

  public class AnotherClass extends MovieClip {

  public function AnotherClass() {

  }

  public function testNow(){

       this.dispatchEvent(new Event('testE'));

  }

  }

}

1 reply

kglad
Community Expert
Community Expert
July 5, 2017

1. neither of those could be a document class.

2. that would work, though it's not good coding, if the files were save properly.

3. this would be better,

class AnotherClass
{
import needed classes


  
public function AnotherClass()
  
{
this.dispatchEvent(new Event('testE'));

  
}
}

class Main
{

import needed classes
  
public function Main()
  
{
  
var another:AnotherClass = new AnotherClass();

another.addEventListener('testE',testF);
  
}

  
private function testF(e:Event):void
  
{
  trace
("Success!, called from",e.currentTarget);
  
}
}

RozuAuthor
Participating Frequently
July 5, 2017

Thank you for the fast response, am I correct to assume the document class is the one you attach to the .fla file itself inside the properties panel?

Also with the code you supplied, I get no errors but the trace isn't appearing, I would assume it isn't being called...

What I have is a class called Floor which has this function...

public function updateGold(e:Event):void

  {

   Main.currentGold += goldIncome;

   /*Main.updateText();*/

   this.dispatchEvent(new Event('testE'));

   trace (Main.currentGold);

  }

...this is being called off a timer every second, the trace for currentGold is working correctly, but the dispatch doesn't seem to be going through back to the Main class, and this is the bit of code I have in my Main class...

var floor:Floor = new Floor();

  floor.addEventListener('testE', updateText);

private function updateText(e:Event):void

  {

   /*CurrentGold_txt.text = currentGold.toString();*/

   trace ("Success!, called from", e.currentTarget);

   trace ("OK");

  }

 

is there a reason why it might not be calling updateText? Sorry for my inexperience.

kglad
Community Expert
Community Expert
July 5, 2017

there are a lot of reasons for possible failure when snippets are posted.

you should create a new test project in another directory with a main class and a floor class and test your code.