0

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

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”方法中收到:(这是什么原因???

4

1 回答 1

1

在构造函数中,您有:

this.addEventListener(ItemEvent.PICKED_UP, removeItem);

你有Szene对象在听自己——它应该在听适当的FigurControl对象:

myFigurControl.addEventListener(ItemEvent.PICKED_UP, removeItem);
于 2012-06-08T17:30:08.887 回答