flash as3 call function in one child class from another child class

I'm trying to call a function in one child class (Circle.as) from another child class (Wedge.as). Circle.as is instantiated by the document class (Tree.as), and Wedge.as is instantiated by Circle.as. How do I do this? This is my code:

Tree.as

package com.treediagram  
{  
  public class Tree extends MovieClip  
  {
    var firstCircle:Circle = new Circle();
    addChild(firstCircle);
  }
}

Circle.as:

package com.treediagram
{
  public class Circle extends MovieClip  
  {
    private var wedge:Wedge;
    public function Circle()  
    {
      var circleHolder:MovieClip = new MovieClip();
      var circleClip:MovieClip = new MovieClip();
      circleClip.addEventListener(MouseEvent.CLICK,circleClickedEvent);
    }

    private function circleClickedEvent(e:MouseEvent):void
    {
      var wedgeHolder:MovieClip = new MovieClip();
  circleHolderRef.addChildAt(wedgeHolder,1);
      var wedge:Wedge = new Wedge(wedgeHolderRef);
  wedge.addEventListener(MouseEvent.CLICK,wedgeClickEvent);
    }



    private function wedgeClickEvent(e.target){
      trace ('click'); //NOT WORKING
    } 
  }
}

Wedge.as

package com.treediagram
{
  public class Wedge extends MovieClip  
  {
    public function Wedge(wedgeHolderRef)
    {
      var wedgeClip:MovieClip = new MovieClip();
      wedgeClip.addEventListener(Event.ADDED_TO_STAGE, wedgeAddedEvent);
      wedgeHolderRef.addChild(wedgeClip);
    }
  }
}

I saw a similar question to this here:

How to call a function in a Class from another Class?

but it was a bit hard to follow as it relates to my problem. One of the solutions worked, but was commented as being bad form, and another did not work, so I want to make sure I structure my code properly.

Answers


Usually it is OK for a parent class to know about its children, but not the other way around. If you don't follow this principle, you'll end up creating Circular dependencies and end up with code that is tightly coupled and hard to maintain. The key then is to prevent a child from needing to call its parent's methods directly.

One solution is to use event-based communication. When something happens in the child class, it fires an event which a parent class may choose to handle or not.

For example:

public function Circle()  
{
  var wedge:Wedge = new Wedge();
  wedge.addEventListener(MouseEvent.CLICK, onWedgeClick);
}
private function onWedgeClick(e:MouseEvent) {
  deactiveCircle();
}

Need Your Help

JQuery rotator broken in IE8 & 9 only

javascript jquery internet-explorer internet-explorer-8

I'm running the following code to create a JQuery image fader: