Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 915505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:53:55+00:00 2026-05-15T17:53:55+00:00

I have 2 files, 1 to dispatch the event and 1 to receive it

  • 0

I have 2 files, 1 to dispatch the event and 1 to receive it (of course in addition to the event class itself).

I’ve done this before with both dispatching and receiving files being mxml, but this time the dispatching file is an actionscript class, and it’s not working this time. Is it different for actionscript classes?

Here’s a dumbed down version

The dispatching class dispatches the event as soon as it’s created.

public class ASClass extends UIComponent{

     public function ASClass(){
        dispatchEvent(new MyEvents(MyEvents.FIRE_EVENT));
     }
}

in my main mxml app, I instantiate the ASClass which automatically dispatch the event as soon as it’s created and the main mxml app should receive back. But something’s not working.

protected function appCComplete(event:FlexEvent):void{
    addEventListener(MyEvents.FIRE_EVENT, gotEvent);
    var asClass:ASClass = new ASClass();

}

protected function gotEvent(event:MyEvents):void{
       Alert.show("got event");         
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T17:53:56+00:00Added an answer on May 15, 2026 at 5:53 pm
    addEventListener(MyEvents.FIRE_EVENT, gotEvent);
    

    is same as

    this.addEventListener(MyEvents.FIRE_EVENT, gotEvent);
    

    which listens for FIRE_EVENT dispatched by this object – your main mxml app. To listen for events fired from asClass object, you have to call addEventListener on that object.

    asClass.addEventListener(MyEvents.FIRE_EVENT, gotEvent);
    

    Thus, you cannot listen for events fired from the constructor. The following code shows the correct way to listen to the events fired by the asClass object (from anywhere but the constructor).

    protected function appCComplete(event:FlexEvent):void{
        var asClass:ASClass = new ASClass();
        asClass.addEventListener(MyEvents.FIRE_EVENT, gotEvent);
    }
    
    protected function gotEvent(event:MyEvents):void{
           Alert.show("got event");         
    }
    

    If you think about it, you can see that it makes sense. The constructor is used to create an object – event subscribers are normally interested in some actions/changes occurring to the object once the object is fully built and functional.

    That said, you can pass a reference to this or the function to the constructor of ASClass and then assign that function as the event listener from within the constructor if you want.

        public class ASClass extends UIComponent{
            public function ASClass(listener:Function){
                this.addEventListener(MyEvents.FIRE_EVENT, listener);
                dispatchEvent(new MyEvents(MyEvents.FIRE_EVENT));
            }
        }
    
        var asClass:ASClass = new ASClass(this.gotEvent);//pass the function.
    
    //or
    
        public class ASClass extends UIComponent{
            public function ASClass(listenerObj:Object){
                this.addEventListener(MyEvents.FIRE_EVENT, listenerObj.gotEvent);
                dispatchEvent(new MyEvents(MyEvents.FIRE_EVENT));
            }
        }
    
        var asClass:ASClass = new ASClass(this);//pass the object.
    

    Personally, I can’t think of many scenarios where I’d want to go this way: consider some redesigning so that you listen to the events after the constructor returns.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's assume that I have files a.cpp b.cpp and file c.h. Both of the
I have a class like this. Property isPag is based on filed ecboardid, I
I have a Course class which contains collection of CourseItems . Each CourseItem has
I have files served like so: AJAX request handler -> Include file I would
I have files which have many empty cells which appear as NaNs when I
I have files on the server. Originally their names are readable and users put
I have files a1 a2 a3 b1 b2 b3 and I need to exclude
In my Visual Studio 2010 project I have files with .mm file extension, that
Possible Duplicate: Regex for matching javadoc fragments I have files having content like /**
I'm about to walk into a project that will have files (mostly PHP) behind

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.