Search code examples
javascriptcocos2d-iphonecocos2d-html5cocos2d-js

Cocos2d-JS 3.0 migration


I'm moving from cocos2d-html5 version 2.2 to cocos2d-js 3.0 and I have a problem with this code (which works perfectly in version 2.2):

        menuButtons = cc.Menu.create();
        for (var a = 1; a < 6; a++){

            var label = cc.LabelTTF.create("BUTTON " + a, "Arial", 20);
            var tmpBtn = cc.MenuItemLabel.create(label, function (e) {
                cc.log("TEST TAG: " + e.tag);
                //StartSomethingOther(e);
            }, this);

            tmpBtn.setPosition(50, a * 30);
            tmpBtn.tag = a;

            menuButtons.addChild(tmpBtn,2,1);

        }
        menuButtons.setPosition(10, 10);
        this.addChild(menuButtons, 1);

Any "button" is pressed the console always output "TEST TAG: 1" instead of putting the correct number. Any tip to solve the problem?


Solution

  • Change the .tag with .title or even better a ['data-'] identifier like in this example:

            menuButtons = cc.Menu.create();
            for (var a = 1; a < 6; a++){
    
                var label = cc.LabelTTF.create("BUTTON " + a, "Arial", 20);
                var tmpBtn = cc.MenuItemLabel.create(label, function (e) {
                    cc.log("TEST TAG: " + e['data-tag']);
                    //StartSomethingOther(e);
                }, this);
    
                tmpBtn.setPosition(50, a * 30);
                tmpBtn['data-tag'] = a;
    
                menuButtons.addChild(tmpBtn,2,1);
    
            }
            menuButtons.setPosition(10, 10);
            this.addChild(menuButtons, 1);