开发者

Get composed MovieClip's containing (parent) class after event

I have a MovieClip that is composed in a parent (non-display object) class. We register an event listener against that movieclip - a CLICK handler for example.

With event.target I can get a reference to the MovieClip from within the event handler. But how can I pull a reference to its composing class?

I could simply add a "pare开发者_StackOverflow社区ntClass" property on the dynamic MovieClip class, but I'm wondering if there's a more elegant/idiomatic way of doing it that I should consider?


If the class that creates your MovieClip is not a display object then it cannot really be considered its parent. The parent element will be that which your MovieClip is attached to. All that the creating class contains is a reference to an object, just the same as if you then refer to the MovieClip elsewhere.

My preferred way would be to create a descendant class of MovieClip that can contain a reference to the creating class, which you would then use instead of MovieClip.

package {
    import flash.display.MovieClip;

    public class MovieClipWithRef extends MovieClip
    {
        private var _parentRef:Object;  //obviously cast this as accurately as possible

        public function MovieClipWithRef($ref:Object):void
        {
            _parentRef = $ref;
        }

        public function get parentRef():Object
        {
            return _parentRef;
        }
        //no setter makes this property read-only once set by the constructor
    }
}


This answer is essentially the same as shanethehat's answer but also incorporates the composite design pattern. Look at the following example:

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(document class):

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜