AS 3.0 - 未触发自定义事件

时间:2012-06-08 17:25:38

标签: actionscript-3 flash

我有一个场景类,当一个项目被拾取但事件未被触发或不会被接收时应该接收一个事件。

public class Szene extends MovieClip {

    private var items : Array;


    public function Szene() {


        this.addEventListener(ItemEvent.PICKED_UP, removeItem);

    }

    public function removeItem(index : int)
    {
        trace("remove");
        this.removeChild(items[index]);
    }

...

}

public class FigurControl extends MovieClip {

    public function update(event : Event) 
    {


        for(var j=0; j < items.length; j++)
        {
            if(this.hitTestObject(items[j]))
            {
                trace("dispatch");
                this.dispatchEvent(newItemEvent(ItemEvent.PICKED_UP,j));
            }
        }
...


public class ItemEvent extends Event {

    public static const PICKED_UP : String = "pickedUp";

    public var data : int;

    public function ItemEvent(type : String, data : int, bubbles : Boolean=false, cancelable : Boolean=false)
    {
        super(type,bubbles,cancelable);
        this.data = data;
    }

    override public function clone() : Event
    {
        return new ItemEvent(type,data,bubbles,cancelable);
    }
}






        }

我得到输出“dispatch”但是从未在方法“removeItem”中收到:( 什么原因???

1 个答案:

答案 0 :(得分:1)

在构造函数中,您有:

this.addEventListener(ItemEvent.PICKED_UP, removeItem);

您有Szene个对象正在监听自己 - 它应该正在侦听相应的FigurControl对象:

myFigurControl.addEventListener(ItemEvent.PICKED_UP, removeItem);