首页 > cocos2d-js node addChild 中的tag的理解

cocos2d-js node addChild 中的tag的理解

js-test中的ActionManagerTest.js文件中,有以下一段代码,代码里面有个this.addChild,后面跟着三个参数,文档中有解释:

addChild(child, zOrder, tag)
"add" logic MUST only be on this method 
If the child is added to a 'running' node, then 'onEnter' and 'onEnterTransitionDidFinish' will be called immediately.

Parameters:
{cc.Node} child
A child node
{Number} zOrder Optional
Z order for drawing priority. Please refer to setZOrder(int)
{Number} tag Optional
A interger to identify the node easily. Please refer to setTag(int)

可是我依然不能理解,这个tag拿来干什么?为什么this.addChild(child, 1, TAG_GROSSINI);这里后面要跟着sequence的tag??为什么???

代码:


//------------------------------------------------------------------
//
// RemoveTest
//
//------------------------------------------------------------------
var RemoveTest = ActionManagerTest.extend({
    title:function () {
        return "Remove Test";
    },
    onEnter:function () {
        //----start3----onEnter
        this._super();

        var s = director.getWinSize();
        var l = new cc.LabelTTF("Should not crash", "Thonburi", 16);
        this.addChild(l);
        l.x = s.width / 2;
        l.y = 245;

        var move = cc.moveBy(2, cc.p(200, 0));
        var callback = cc.callFunc(this.stopAction, this);
        var sequence = cc.sequence(move, callback);
        sequence.tag = TAG_SEQUENCE;

        var child = new cc.Sprite(s_pathGrossini);
        child.x = 200;
        child.y = 200;

        this.addChild(child, 1, TAG_GROSSINI);
        child.runAction(sequence);
        //----end3----
    },

    stopAction:function () {
        //----start3----onEnter
        var sprite = this.getChildByTag(TAG_GROSSINI);
        sprite.stopActionByTag(TAG_SEQUENCE);
        //----end3----
    },

    //
    // Automation
    //
    testDuration:3.5,
    getExpectedResult:function() {
        return NOT_CRASHED_CONST;
    },
    getCurrentResult:function() {
        return NOT_CRASHED_CONST;
    }
});

你生了三个娃,不给他们起外号,等他们长大了,你对他们叫“孩子”,他们怎么知道你叫的是谁?
所以这里的TAG就相当于是子元素的“外号”,主要是为了以后能getChildByTag拿到这个子元素。

【热门文章】
【热门文章】