Event listener called twice

i have a sprite that has a mouse click event listener…
when this sprite is clicked, the system will dispatch a custom event and this custom event has a call back that will add a sprite in a layer…

but it throws an error that the sprite node is already added to the layer…

and as i console log… the custom event call back is being called twice… any idea why it is happening? or is it a bug?

Can you show us the relevant code? It’s hard to tell what it could be exactly without it.

game.EventManager = cc.Class.extend({

    _gameListeners : null,
    _eventListener : null,
    _priority : 1,
    event_name : "CUSTOM_EVENT",
    ctor : function(event_name)
    {
        var self = this;
        self.event_name = event_name;
        self._gameListeners = new game.Map();
        self._eventListener = cc.EventListener.create({
            event: cc.EventListener.CUSTOM,
            eventName: self.event_name,
            callback : function (event)
            {
                  self.broadCastEvent(event);
            }
        });
        cc.eventManager.addListener(self._eventListener, self._priority);
    },

    broadCastEvent : function (event)
    {

        var elements = this._gameListeners.getElements();
        var size = elements.length;
        for(var i=0; i<size; i++)
        {
            if(elements[i].value)
                elements[i].value(event);
        }
    },

    addListener : function (key,listenerCallBack)
    {
        this._gameListeners.put(key,listenerCallBack);
    }

});

game.PadType = game.PadType || {};
game.PadType.CITY = 0;
game.PadType.FIELD = 1;
game.BuildingPad = cc.Class.extend({

    _node : null,
    _padType : null,
    isTouch : false,
    ctor : function(sprite,padType)
    {
        this._node = sprite;
        this._padType = padType;
    },

    onDown : function (e)
    {
        var node = this._node;
        var rect = node.getBoundingBox();
        if(cc.rectContainsPoint(cc.rect(0,0,rect.width,rect.height),node.convertToNodeSpace(e.getLocation())))
        {
            if(!this.isTouch)
            {
                this.isTouch = true;
            }
        }
    },

    getPosition : function()
    {
        return this._node.getPosition();
    },

    onUp : function (e)
    {
        var node = this._node;
        var rect = node.getBoundingBox();
        if(cc.rectContainsPoint(cc.rect(0,0,rect.width,rect.height),node.convertToNodeSpace(e.getLocation())))
        {
            if(this.isTouch)
            {
                this.isTouch = false;
                var event = new cc.EventCustom(game.Event.ON_PAD_SELECTED);
                event.setUserData({pad : this});
                cc.eventManager.dispatchEvent(event);
            }
        }
    },

    addInputLister : function ()
    {

        var self = this;
        if ( cc.sys.capabilities.hasOwnProperty( 'mouse' ) )
        {
            cc.eventManager.addListener(
                {
                    event: cc.EventListener.MOUSE,

                    onMouseDown: function( event )
                    {
                        if ( event.getButton() == cc.EventMouse.BUTTON_LEFT )
                        {
                            self.onDown(event);
                        }
                        return false;
                    },

                    onMouseUp: function(event)
                    {
                        if ( event.getButton() == cc.EventMouse.BUTTON_LEFT )
                        {
                            self.onUp(event);
                        }
                        return false;
                    }
                }, self._node );
        }

        if ( cc.sys.capabilities.hasOwnProperty( 'touches' ) )
        {
            cc.eventManager.addListener(
                {
                    event: cc.EventListener.TOUCH_ONE_BY_ONE,
                    // called when the touch first begins
                    onTouchBegan:function( touch, event )
                    {
                        self.onDown(touch);
                        return true;
                    },
                    // called when the user lifts their finger
                    onTouchEnded:function( touch, event )
                    {
                        self.onUp(touch);
                        return true;
                    },
                }, self._node );
        }
    }

});

Hi sir @grimfate :smiley:
above are my codes for event handling
so basically i will initialize this event manager at the start of my game

when the building pad is clicked it will dispatch a ON_PAD_SELECTED custom event
and the event manager will broadcast the event on all subscribed listeners of the certain event
this event creates a layer with a listview of all the buildings that my game has and add it to my UILayer.

i have test if the mouse click is called twice… but no its not…
then i test if the event callback is called twice… yes it is, wonder why its happening… (i use firefox debugger)
this cause a child already added to the layer error :disappointed_relieved:

i hope its not hard to read… thanks sir @grimfate

My first thought is, I think the event listener you add for touch will respond to mouse clicks as well. So, when you click with the mouse, it will trigger onMouseDown and onTouchBegan, and when it’s released, onMouseUp and onTouchEnded. Try commenting out

if ( cc.sys.capabilities.hasOwnProperty( 'touches' ) )
{
    cc.eventManager.addListener(
    {
        event: cc.EventListener.TOUCH_ONE_BY_ONE,
        // called when the touch first begins
        onTouchBegan:function( touch, event )
        {
            self.onDown(touch);
            return true;
        },
        // called when the user lifts their finger
        onTouchEnded:function( touch, event )
        {
            self.onUp(touch);
            return true;
        },
    }, self._node );
}

and see if this fixes your problem.

Thanks sir @grimfate …
I found the problem… its actually in my code…

I accidentally initialize my scene twice… making it have 2 copies of my custom classes…

hehe sorry for doubting cocos2d :blush: