removeEventListener不起作用

时间:2013-05-09 14:56:15

标签: actionscript-3 routes addeventlistener event-listener

function Drag(event:MouseEvent):void {
    if ((event.target.parent == InventoryMenu) && (event.target is item)) {
        var picked:item = item(event.target);
        stage.addEventListener(MouseEvent.MOUSE_UP, Drop);
        InventoryArrowDown.addEventListener(MouseEvent.MOUSE_OVER, InventoryNav("down"));
        InventoryArrowUp.addEventListener(MouseEvent.MOUSE_OVER, InventoryNav("up"));

        function Drop(event:MouseEvent):void {
            if ((event.target.parent == InventoryMenu) && (event.target is item)) {
                var dropped:item = item(event.target);          
                if ((event.target is item) && (event.target.parent == InventoryMenu)) {
                    if (picked.itemdata("workswith") == dropped.name) {
                        var itemname:item = item(FetchResult(picked, dropped));
                        itemname.addChild(itemname.itemdata("filename"));
                        InventoryMenu.removeChild(picked);
                        InventoryMenu.removeChild(dropped);
                        InventoryMenu.addChild(itemname);
                        InventoryUpdate();
                    } else if (picked.name != dropped.name) {
                        trace("No son compatibles");
                    }
                    stage.removeEventListener(MouseEvent.MOUSE_UP, Drop);
                    InventoryArrowDown.removeEventListener(MouseEvent.MOUSE_OVER, InventoryNav("down"));
                    InventoryArrowUp.removeEventListener(MouseEvent.MOUSE_OVER, InventoryNav("up"));
                }
            }
        }
    }
}

由于某种原因,InventoryArrowDown和InventoryArrowUp上的removeEventListener不起作用。我很确定路由是正确的,因为它是来自addEventListener的直接复制粘贴,并且它不使用任何变量。

有任何疑问是什么问题?

1 个答案:

答案 0 :(得分:0)

很难在没有看到InventoryNav代码的情况下帮助您,但问题可能是您应该在测试之前删除事件监听器。

此外,您应该编写两个不同的处理程序,而不是使用一个并像您一样传递参数。

以下是您的代码的修改版本,可能有所帮助:

private function drag(event:MouseEvent):void {
    if ((event.target.parent == inventoryMenu) && (event.target is Item)) {
        var picked:Item = Item(event.target);
        stage.addEventListener(MouseEvent.MOUSE_UP, drop);
        inventoryArrowDown.addEventListener(MouseEvent.MOUSE_OVER, inventoryNavDown);
        inventoryArrowUp.addEventListener(MouseEvent.MOUSE_OVER, inventoryNavUp);
    }
}

private function drop(event:MouseEvent):void {
    stage.removeEventListener(MouseEvent.MOUSE_UP, drop);
    inventoryArrowDown.removeEventListener(MouseEvent.MOUSE_OVER, inventoryNavDown);
    inventoryArrowUp.removeEventListener(MouseEvent.MOUSE_OVER, inventoryNavUp);

    if ((event.target.parent == inventoryMenu) && (event.target is Item)) {
        var dropped:Item = Item(event.target);          
        if ((event.target is Item) && (event.target.parent == inventoryMenu)) {
            if (picked.itemdata("workswith") == dropped.name) {
                var itemname:Item = Item(fetchResult(picked, dropped));
                itemname.addChild(itemname.itemdata("filename"));
                inventoryMenu.removeChild(picked);
                inventoryMenu.removeChild(dropped);
                inventoryMenu.addChild(itemname);
                inventoryUpdate();
            } else if (picked.name != dropped.name) {
                trace("No compatible sons");
            }
        }
    }
}