Bind eventListener issue

  • xVector
    Likes 0

    Problem Description

    I have a issue with the binding of my events with your respective sprite.

    This is my code:

     

    var GameLayer = cc.Layer.extend({
        sprite:null,
        ctor:function () {
            this._super();
            var size = cc.winSize;
    
            var gradient = cc.LayerGradient(cc.color(0,0,0,255),cc.color(0x46,0x82,0xB4,255));
            this.addChild(gradient);
    
            for(i=0;i<16;i++){
                var tile = new MemoryTile();
                this.addChild(tile,0);
                tile.setPosition(49+i%4*74,400-Math.floor(i/4)*74);
            }
    
            return true;
        }
    });
    
    
    var MemoryTile = cc.Sprite.extend({
        ctor:function() {
            this._super();
            this.initWithFile(res.cover);
    
            var listener = cc.EventListener.create({
            event: cc.EventListener.TOUCH_ONE_BY_ONE,
            swallowTouches: true,
            onTouchBegan: function (touch, event) {
                var target = event.getCurrentTarget();
                var location = target.convertToNodeSpace(touch.getLocation());
                var targetSize = target.getContentSize();
                var targetRectangle = cc.rect(0, 0, targetSize.width, targetSize.height);
                    if (cc.rectContainsPoint(targetRectangle, location)) {
                        console.log("I picked a tile!!");
                    }
                    //console.log('touche po oe: ' + targetSize);    
                    return true;
                }
            });
    
            cc.eventManager.addListener(listener.clone(), this);
        }
    });

     

    So, the problem is that only the first sprite created still with the eventListener (the touch works), i think the listener.clone() is not working, any ideas of what i'm doing wrong?

    The version of my cocos2d is 3.7.

  • Sonar Systems admin
    Likes 0

    Did you see listener.clone somewhere so you used it?

  • xVector
    Likes 0

    In the official documentation of cocos2d-js, the method .clone still appear, but i think is deprecated now.

    I fix my problem with the answer in StackOverflow, http://stackoverflow.com/questions/32505699/bind-eventlistener-issue

    BTW, thx for the answer :)

     

  • Sonar Systems admin
    Likes 1

    Great to hear, could you post the answer as a solution on Sonar Learning please so the entire community can benefit from your wisdom.

  • xVector
    Likes 1

    SOLVED:

    the create method is deprecated, this should work just fine inside memory tile instead of the var listener and the add listener:

    cc.eventManager.addListener({
                event: cc.EventListener.TOUCH_ONE_BY_ONE,
                swallowTouches: false,
                onTouchBegan: function (touch, event) {
                    var target = event.getCurrentTarget();
                    var location = target.convertToNodeSpace(touch.getLocation());
                    var targetSize = target.getContentSize();
                    var targetRectangle = cc.rect(0, 0, targetSize.width, targetSize.height);
                    if (cc.rectContainsPoint(targetRectangle, location)) {
                        console.log("I picked a tile!!");
                    }
                    //console.log('touche po oe: ' + targetSize);   
                    return true;
                }
            }, this);

     


    This reply has been verified.
  • Sonar Systems admin
    Likes 0

    CHEERS     

Login to reply