title: Flash-AS3: Calling a function in a Class from another Class (Part 2) via DispatchEvent
body: <p>Hey this question is in reply to <a href="https://stackoverflow.com/users/87002/joel-hooks">Joel Hooks</a>'s comment on an older question of mine (<a href="https://stackoverflow.com/questions/1638537/how-to-call-a-function-in-a-class-from-another-class/1640146#1640146">How to call a function in a Class from another Class?</a>)</p>
  <p>I was able to fix my own problem using a public static var to reference one of my Classes, so in the other class I just needed to use the this keyword so it could be called.</p>
  <pre><code>instance = this; // in Navigation Class
  Navigation.instance.introPlayButtonClick(); // in Intro Class
  </code></pre>
  <p>Now it seems I'm not doing this in the best or <em>purist</em> way for <strong>OOP</strong>(can sense debate here), so I'm posing this question a 2nd time in hopes of getting this figured out correctly.</p>
  <h2>The Problem</h2>
  <p>I have 2 Classes, Intro and Navigation. There is a button created in the Intro class that needs call a function inside of the Navigation Class. I also have a CustomEvent class where I setup Custom Events. Below is the code, I've stripped out all of the unessential code relating to this problem.</p>
  <h3>The Intro Class</h3>
  <p>When the function <strong>clicked</strong> is called, it dispatches an Event that the Navigation class is listening for.</p>
  <hr />
  <pre><code>package src.display
  {
  import flash.events.*;
   import flash.display.*;
  
   // ☼ --- Imported Classes
   import src.events.CustomEvent;
   import src.model.Navigation;
  
   public class Intro extends Sprite 
   {
    private var playBtn:MovieClip;
    private var colorTransform:ColorTransform;
  
    // ☼ --- Constructor
    public function Intro():void 
    {
        this.addEventListener(Event.ADDED_TO_STAGE, init);
    }
  
    // ☼ --- Init
    public function init(event:Event):void 
    {
        draw();
        this.removeEventListener(Event.ADDED_TO_STAGE, init);
    }
  
    public function draw():void 
    {
        playBtn = new PlayThumb;
        playBtn.buttonMode = true;   
        playBtn.x = 552;   
        playBtn.y = 289;
        playBtn.alpha = 1;
  
        // ☼ --- add button 
        addChild(playBtn);
  
        // ☼ --- button listeners
        playBtn.addEventListener(MouseEvent.MOUSE_UP, clicked);
    }
    //-----------------------------------------------------
    // ☼ --- My Old Function
    /*private function clicked(e:MouseEvent):void 
    {
        Navigation.instance.introPlayButtonClick(); // Open tab1
    }*/
    //-----------------------------------------------------
    // ♦♦♦ Added to dispatch Event to JStriedl's function ♦♦♦
    private function clicked(e:MouseEvent):void 
        {
        dispatchEvent (new CustomEvent(CustomEvent.INTRO_CLICKED, {}));
        trace(&quot;Dispatch Event INTRO_CLICKED&quot;);
        }
   }
  }
  </code></pre>
  <h3>Navigation Class</h3>
  <p>This is the Navigation Class which contains a function that the Intro Class needs to be able to call.</p>
  <hr />
  <pre><code>package src.model 
  {
  import flash.events.*;
  import flash.display.*;
  
  // ☼ ---Imported  Classes
  import src.display.Intro;
  import src.model.Fonts;
  import src.events.CustomEvent;
  
  public class Navigation extends MovieClip //Needs MovieClip
  {  
      public var intro:Intro;
      public static var instance:Navigation;
  
      // ☼ --- Constructor
  public function Navigation()
  {
      this.addEventListener(Event.ADDED_TO_STAGE, init);
  }
  
  // ☼ --- Init
  public function init():void
  {
      instance = this;  
      intro = new Intro;
      attachListenerForIntro(intro);
      this.removeEventListener(Event.ADDED_TO_STAGE, init);
  }
  
  // ♦♦♦ JStriedl's code ♦♦♦
  public function attachListenerForIntro(introToCheckForClick:Intro)
  {
      intro = introToCheckForClick;
      intro.addEventListener(&quot;introClicked&quot;, introPlayButtonClick);
  }
  
  // Function that button in Intro needs to call
  public function introPlayButtonClick(e:CustomEvent):void
  {    
      trace(&quot;Navigation function called from Intro&quot;);
      intro.removeEventListener(&quot;introClicked&quot;, introPlayButtonClick);
  }
  }
  }
  </code></pre>
  <h2>Issues or Errors</h2>
  <p>So far when the button is clicked nothing happens :( I'm assuming I don't have the EventListener setup correctly inside of the Navigation Class?</p>