From 86cfa26baff9d70b207f670dcfa607a7192cb996 Mon Sep 17 00:00:00 2001 From: Cocos Ass Date: Fri, 2 Mar 2018 11:29:39 +0800 Subject: [PATCH] forbid Hierarchy change test example add a new test in test-component-scheduler.js forbid Hierarchy change part and example update data make my local file get reset forbid Hierarchy change part test example update a new test content about setSiblingIndex change the report of error test --- DebugInfos.js | 1 + cocos2d/core/utils/base-node.js | 11 +++ .../unit-es5/test-component-scheduler.js | 79 +++++++++++++++++++ 3 files changed, 91 insertions(+) diff --git a/DebugInfos.js b/DebugInfos.js index dbb8e5381f..808ca6d68c 100644 --- a/DebugInfos.js +++ b/DebugInfos.js @@ -410,6 +410,7 @@ if (CC_DEBUG) { "3818": "Failed to read or parse project.json", //_loadConfig "3819": "Warning: target element is not a DIV or CANVAS", //_initRenderer "3820": "The renderer doesn't support the renderMode %s", //_initRenderer + "3821": "Cannot change hierarchy while activating or deactivating nodes.", //Animation: 3900 "3900": "Invalid clip to add", //addClip "3901": "Invalid clip to remove", //removeClip diff --git a/cocos2d/core/utils/base-node.js b/cocos2d/core/utils/base-node.js index 60fbd34153..8c649c991b 100644 --- a/cocos2d/core/utils/base-node.js +++ b/cocos2d/core/utils/base-node.js @@ -33,6 +33,7 @@ var eventManager = require('../event-manager'); var JS = cc.js; var Destroying = Flags.Destroying; var DontDestroy = Flags.DontDestroy; +var Deactivating = Flags.Deactivating; var CHILD_ADDED = 'child-added'; var CHILD_REMOVED = 'child-removed'; @@ -366,10 +367,16 @@ var BaseNode = cc.Class({ this._onSetParent(value); + if (CC_DEBUG && (this._objFlags & Deactivating)) { + cc.errorID(3821); + } if (value) { if (!CC_JSB) { eventManager._setDirtyForNode(this); } + if (CC_DEBUG && (value._objFlags & Deactivating)) { + cc.errorID(3821); + } value._children.push(this); value.emit(CHILD_ADDED, this); } @@ -551,6 +558,10 @@ var BaseNode = cc.Class({ if (!this._parent) { return; } + if ((this._parent._objFlags & Deactivating)) { + cc.errorID(3821); + return; + } var siblings = this._parent._children; index = index !== -1 ? index : siblings.length - 1; var oldIndex = siblings.indexOf(this); diff --git a/test/qunit/unit-es5/test-component-scheduler.js b/test/qunit/unit-es5/test-component-scheduler.js index 5426f979e5..4f60b7bd3e 100644 --- a/test/qunit/unit-es5/test-component-scheduler.js +++ b/test/qunit/unit-es5/test-component-scheduler.js @@ -165,6 +165,85 @@ test('disable component during onEnable', function () { childComp.onEnable.once('could re-enable component'); }); +test('change parent during onDisable', function () { + + var rootNode , preNode , testNode , child , childComp; + + var vendorError = cc.errorID; + cc.errorID = new Callback().enable(); + + //Pattern one : the Hierarchy change during onDisable + + var nodes1 = createNodes({ + preNode: {}, + testNode: { + comps: cc.Component, + }, + }); + rootNode = nodes1.root; + preNode = nodes1.preNode; + testNode = nodes1.testNode; + testNodeComp = nodes1.testNodeComps[0]; + + testNodeComp.onDisable = function() { + testNode.parent = preNode; + }; + + nodes1.attachToScene(); + + testNode.active = false; + cc.errorID.once('Should report error when changing target node\'s parent during its deactivation'); + + //Pattern two : the Hierarchy change during onDisable + + var nodes2 = createNodes({ + preNode: {}, + testNode: { + comps: cc.Component, + }, + }); + rootNode = nodes2.root; + preNode = nodes2.preNode; + testNode = nodes2.testNode; + testNodeComp = nodes2.testNodeComps[0]; + + testNodeComp.onDisable = function() { + preNode.parent = testNode; + } + + nodes2.attachToScene(); + + testNode.active = false; + cc.errorID.once('Should report error when append new child to a target node during its deactivation'); + + //Pattern three : the Hierarchy change during onDisable + + var nodes3 = createNodes({ + testNode: { + child1: { + comps: cc.Component, + }, + child2: {} + }, + }); + rootNode = nodes3.root; + testNode = nodes3.testNode; + child1 = nodes3.child1; + child1Comp = nodes3.child1Comps[0]; + child2 = nodes3.child2; + + child1Comp.onDisable = function() { + child1.setSiblingIndex(1); + }; + + nodes3.attachToScene(); + + testNode.active = false; + cc.errorID.once('Should report error when setting node\'s sibling index during its parent\'s deactivation'); + + cc.errorID = vendorError; +}); + (function () { // Test deactivate during activating or conversely