Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Calling functions from the document class in other classes

New Here ,
Jul 05, 2017 Jul 05, 2017

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!

TOPICS
ActionScript
579
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Jul 05, 2017 Jul 05, 2017

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.

...
Translate
Community Expert ,
Jul 05, 2017 Jul 05, 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);
  
}
}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 05, 2017 Jul 05, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 05, 2017 Jul 05, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 05, 2017 Jul 05, 2017

I made a new project entirely with the Main class and AnotherClass...

This is the main class

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);
  }


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

and this is the AnotherClass

package Code
{

import Main;
import flash.display.MovieClip;
import flash.events.Event;

public class AnotherClass extends MovieClip
{

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

it still doesn't trigger the dispatch

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 05, 2017 Jul 05, 2017

The dispatch from AnotherClass will have already happened before you have set the listener. Try putting a public method into AnotherClass, that you can call from the document class after AnotherClass already exists.

There are simpler ways to let any class access the document class.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 05, 2017 Jul 05, 2017

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 05, 2017 Jul 05, 2017

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'));

  }

  }

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 05, 2017 Jul 05, 2017

I got it now thank you both so much

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 05, 2017 Jul 05, 2017
LATEST

if another class is a display object usually you would use an added_to_stage event to dispatch, if you're trying to dispatch asap.

package {

  import flash.display.MovieClip;

  import flash.events.Event;

  import Code.AnotherClass;

  public class Main extends MovieClip {

  var another: AnotherClass;

  public function Main() {

another=new AnotherClass();

  another.addEventListener('testE', testF);  // add listener before adding to display

  addChild(another)

  }

  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() {

this.addEventListener(Event.ADDED_TO_STAGE,addedF);

  }

  private function addedF(){

   dispatchEvent(new Event('testE'));

  }

  }

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 05, 2017 Jul 05, 2017

per above, use the trace function to see that you're dispatching the event before the listener is added.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 05, 2017 Jul 05, 2017

Yeah it's being called before the listeners being added, would you suggest a timer delay to fix this?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines