Этот ответ, по сути, такой же, как и ответ shanethehat, но также включает в себя составной шаблон дизайна Посмотрите на следующий пример:
ICompositeObject.as:
package com.flashdevelopprojects.patterns.composite
{
public interface ICompositeObject
{
function get parentObject():Object
}// end interface
}// end package
CompositeObject.as:
package com.flashdevelopprojects.patterns.composite
{
public class CompositeObject implements ICompositeObject
{
private var _parentObject:Object;
public function get parentObject():Object { return _parentObject }
public function CompositeObject(parentObject:Object)
{
_parentObject = parentObject;
}// end function
}// end class
}// end package
Main.as (класс документа):
package
{
import com.flashdevelopprojects.patterns.composite.CompositeObject;
import com.flashdevelopprojects.patterns.composite.ICompositeObject;
import flash.display.Sprite;
import flash.events.Event;
public class Main extends Sprite
{
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}// end function
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
var a:A = new A();
a.b.addEventListener(Event.ADDED_TO_STAGE, onBAddedToStage);
addChild(a.b);
}// end function
public function onBAddedToStage(e:Event):void
{
var b:ICompositeObject = ICompositeObject(e.target);
trace(b.parentObject); // output: A
}// end function
}// end class
}// end package
import com.flashdevelopprojects.patterns.composite.CompositeObject;
import com.flashdevelopprojects.patterns.composite.ICompositeObject;
import flash.display.MovieClip;
internal class A
{
private var _b:B;
public function get b():B { return _b }
public function A()
{
_b = new B(this);
}// end function
public function toString():String { return "A" }
}// end class
internal class B extends MovieClip implements ICompositeObject
{
private var _compositeObject:CompositeObject;
public function get parentObject():Object { return _compositeObject.parentObject }
public function B(parentObject:Object)
{
_compositeObject = new CompositeObject(parentObject);
}// end function
}// end class