diff --git a/cocos2d/actions/CCActionInstant.js b/cocos2d/actions/CCActionInstant.js index 4c324b92ba..37b3686aa2 100644 --- a/cocos2d/actions/CCActionInstant.js +++ b/cocos2d/actions/CCActionInstant.js @@ -75,7 +75,11 @@ cc.ActionInstant = cc.FiniteTimeAction.extend({ cc.Show = cc.ActionInstant.extend({ update:function (dt) { - this.target.visible = true; + var _renderComps = this.target.getComponentsInChildren(cc._SGComponent); + for (var i = 0; i < _renderComps.length; ++i) { + var render = _renderComps[i]; + render.enabled = true; + } }, reverse:function () { @@ -108,7 +112,11 @@ cc.show = function () { cc.Hide = cc.ActionInstant.extend({ update:function (dt) { - this.target.visible = false; + var _renderComps = this.target.getComponentsInChildren(cc._SGComponent); + for (var i = 0; i < _renderComps.length; ++i) { + var render = _renderComps[i]; + render.enabled = false; + } }, reverse:function () { @@ -141,7 +149,11 @@ cc.hide = function () { cc.ToggleVisibility = cc.ActionInstant.extend({ update:function (dt) { - this.target.visible = !this.target.visible; + var _renderComps = this.target.getComponentsInChildren(cc._SGComponent); + for (var i = 0; i < _renderComps.length; ++i) { + var render = _renderComps[i]; + render.enabled = !render.enabled; + } }, reverse:function () { diff --git a/cocos2d/core/CCNode.js b/cocos2d/core/CCNode.js index c5910124e6..a0ff8c0fb1 100644 --- a/cocos2d/core/CCNode.js +++ b/cocos2d/core/CCNode.js @@ -633,8 +633,8 @@ var Node = cc.Class({ }, /** - * !#en Returns all components of supplied type in any of its children. - * !#zh 递归查找所有子节点中指定类型的组件。 + * !#en Returns all components of supplied type in self or any of its children. + * !#zh 递归查找自身或所有子节点中指定类型的组件 * @method getComponentsInChildren * @param {Function|String} typeOrClassName * @return {Component[]} @@ -645,6 +645,7 @@ var Node = cc.Class({ getComponentsInChildren: function (typeOrClassName) { var constructor = getConstructor(typeOrClassName), components = []; if (constructor) { + findComponents(this, constructor, components); findChildComponents(this._children, constructor, components); } return components; diff --git a/cocos2d/core/components/CCComponent.js b/cocos2d/core/components/CCComponent.js index 27a27790aa..0c0d70444c 100644 --- a/cocos2d/core/components/CCComponent.js +++ b/cocos2d/core/components/CCComponent.js @@ -550,8 +550,8 @@ var Component = cc.Class({ }, /** - * !#en Returns the components of supplied type in any of its children using depth first search. - * !#zh 递归查找所有子节点中指定类型的组件。 + * !#en Returns the components of supplied type in self or any of its children using depth first search. + * !#zh 递归查找自身或所有子节点中指定类型的组件 * * @method getComponentsInChildren * @param {Function|String} typeOrClassName diff --git a/jsb/jsb-action.js b/jsb/jsb-action.js index f2f0659f6f..a7f4cddaad 100644 --- a/jsb/jsb-action.js +++ b/jsb/jsb-action.js @@ -123,6 +123,33 @@ cc.Follow.prototype.update = function(dt) { } }; +cc.Show.prototype.update = function (dt) { + var target = this.getTarget(); + var _renderComps = target._owner.getComponentsInChildren(); + for (var i = 0; i < _renderComps.length; ++i) { + var render = _renderComps[i]; + render.enabled = true; + } +}; + +cc.Hide.prototype.update = function (dt) { + var target = this.getTarget(); + var _renderComps = target._owner.getComponentsInChildren(); + for (var i = 0; i < _renderComps.length; ++i) { + var render = _renderComps[i]; + render.enabled = false; + } +}; + +cc.ToggleVisibility.prototype.update = function (dt) { + var target = this.getTarget(); + var _renderComps = target._owner.getComponentsInChildren(); + for (var i = 0; i < _renderComps.length; ++i) { + var render = _renderComps[i]; + render.enabled = true; + } +}; + // Special call func cc.callFunc = function (selector, selectorTarget, data) { var callback = function (sender, data) { diff --git a/test/qunit/unit/test-node.js b/test/qunit/unit/test-node.js index cd36278ad8..03b75e519d 100644 --- a/test/qunit/unit/test-node.js +++ b/test/qunit/unit/test-node.js @@ -183,7 +183,7 @@ test('get self components', function () { ok(obj.getComponents(MyComponent).length === 3, 'getComponents: can get my component array'); ok(obj.getComponentInChildren(MyComponent) === comp, 'getComponentInChildren: can get my component in children'); - ok(obj.getComponentsInChildren(MyComponent).length === 2, 'getComponentsInChildren: can get my components in children'); + ok(obj.getComponentsInChildren(MyComponent).length === 5, 'getComponentsInChildren: can get my components in children and self'); cc.js.unregisterClass(MyComponent); }); @@ -205,7 +205,26 @@ test('should not include self component', function () { obj1.parent = obj; ok(obj.getComponentInChildren(MyComponent) === null, 'getComponentInChildren should not include self component'); - ok(obj.getComponentsInChildren(MyComponent).length === 0, 'getComponentsInChildren should not include self component'); + + cc.js.unregisterClass(MyComponent); +}); + +test('should include self component', function () { + var MyComponent = cc.Class({ + name: 'MyComponent', + extends: cc.Component + }); + + var obj = new cc.Node("New Node"); + cc.director.getScene().addChild(obj); + obj.addComponent(MyComponent); + obj.addComponent(MyComponent); + obj.addComponent(MyComponent); + + var obj1 = new cc.Node("New Node 1"); + obj1.parent = obj; + + ok(obj.getComponentsInChildren(MyComponent).length === 3, 'getComponentsInChildren should include self component'); cc.js.unregisterClass(MyComponent); });