在事件之后获取包含(父)类的组合MovieClip

时间:2011-08-13 16:11:24

标签: actionscript-3 events composition

我有一个由父(非显示对象)类组成的MovieClip。我们针对该movieclip注册一个事件监听器 - 例如CLICK处理程序。

使用event.target我可以从事件处理程序中获取对MovieClip的引用。但是我怎样才能引用它的作曲类?

我可以在动态MovieClip类上添加一个“parentClass”属性,但我想知道是否有一种更优雅/惯用的方法,我应该考虑?

2 个答案:

答案 0 :(得分:1)

如果创建MovieClip的类不是显示对象,则它实际上不能被视为其父级。父元素将是您的MovieClip附加到的元素。创建类包含的所有内容都是对对象的引用,就像在其他地方引用MovieClip一样。

我首选的方法是创建一个MovieClip的后代类,它可以包含对创建类的引用,然后您将使用它而不是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
    }
}

答案 1 :(得分:1)

这个答案与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