Copy link to clipboard
Copied
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!
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.
...Copy link to clipboard
Copied
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);
}
}
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Sorry, I'm new to coding, how would I go about creating a public method? Thanks!
Copy link to clipboard
Copied
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'));
}
}
}
Copy link to clipboard
Copied
I got it now thank you both so much
Copy link to clipboard
Copied
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'));
}
}
}
Copy link to clipboard
Copied
per above, use the trace function to see that you're dispatching the event before the listener is added.
Copy link to clipboard
Copied
Yeah it's being called before the listeners being added, would you suggest a timer delay to fix this?
Find more inspiration, events, and resources on the new Adobe Community
Explore Now