Merge pull request #459 from pandamicro/persist

Use id to identify persist root node
This commit is contained in:
pandamicro
2016-03-17 10:59:06 +08:00
4 changed files with 51 additions and 31 deletions
+15 -7
View File
@@ -471,7 +471,7 @@ cc.Director = Class.extend(/** @lends cc.Director# */{
* @param {Function} [onLaunched] - The function invoked at the scene after launch.
*/
runSceneImmediate: function (scene, onBeforeLoadScene, onLaunched) {
var i, node, game = cc.game;
var id, node, game = cc.game;
var persistNodes = game._persistRootNodes;
if (scene instanceof cc.Scene) {
@@ -480,8 +480,8 @@ cc.Director = Class.extend(/** @lends cc.Director# */{
}
// detach persist nodes
for (i = persistNodes.length - 1; i >= 0; --i) {
node = persistNodes[i];
for (id in persistNodes) {
node = persistNodes[id];
game._ignoreRemovePersistNode = node;
node.parent = null;
game._ignoreRemovePersistNode = null;
@@ -510,10 +510,18 @@ cc.Director = Class.extend(/** @lends cc.Director# */{
this._scene = scene;
sgScene = scene._sgNode;
// Re-attach persist nodes
for (i = 0; i < persistNodes.length; ++i) {
node = persistNodes[i];
node.parent = scene;
// Re-attach or replace persist nodes
for (id in persistNodes) {
node = persistNodes[id];
var existNode = scene.getChildByName(node.name);
// Scene contains the persist node, should not reattach, should update the persist node
if (existNode) {
persistNodes[id] = existNode;
existNode._persistNode = true;
}
else {
node.parent = scene;
}
}
scene._activate();
}
+11 -9
View File
@@ -47,7 +47,7 @@ var game = /** @lends cc.game# */{
_eventHide: null,
_eventShow: null,
_persistRootNodes: [],
_persistRootNodes: {},
_ignoreRemovePersistNode: null,
/**
@@ -343,10 +343,12 @@ var game = /** @lends cc.game# */{
* @param {Node} node - The node to be made persistent
*/
addPersistRootNode: function (node) {
if (!(node instanceof cc.Node))
if (!(node instanceof cc.Node) || !node.uuid) {
cc.warn('The target can not be made persist because it\'s not a cc.Node or it doesn\'t have _id property.');
return;
var index = this._persistRootNodes.indexOf(node);
if (index === -1) {
}
var id = node.uuid;
if (!this._persistRootNodes[id]) {
var scene = cc.director._scene;
if (cc.isValid(scene)) {
if (!node.parent) {
@@ -360,7 +362,7 @@ var game = /** @lends cc.game# */{
cc.warn('The node can not be made persist because it\'s not in current scene.');
return;
}
this._persistRootNodes.push(node);
this._persistRootNodes[id] = node;
node._persistNode = true;
}
}
@@ -373,11 +375,11 @@ var game = /** @lends cc.game# */{
*/
removePersistRootNode: function (node) {
if (node !== this._ignoreRemovePersistNode) {
var index = this._persistRootNodes.indexOf(node);
if (index !== -1) {
this._persistRootNodes.splice(index, 1);
var id = node.uuid || '';
if (node === this._persistRootNodes[id]) {
delete this._persistRootNodes[id];
node._persistNode = false;
}
node._persistNode = false;
}
},
+14 -6
View File
@@ -96,7 +96,7 @@ cc.js.mixin(cc.director, {
*/
runSceneImmediate: function (scene, onBeforeLoadScene, onLaunched) {
// detach persist nodes
var i, node, game = cc.game;
var id, node, game = cc.game;
var persistNodes = game._persistRootNodes;
// Run an Entity Scene
@@ -105,8 +105,8 @@ cc.js.mixin(cc.director, {
scene._load();
}
for (i = persistNodes.length - 1; i >= 0; --i) {
node = persistNodes[i];
for (id in persistNodes) {
node = persistNodes[id];
game._ignoreRemovePersistNode = node;
node.parent = null;
game._ignoreRemovePersistNode = null;
@@ -134,9 +134,17 @@ cc.js.mixin(cc.director, {
sgScene = scene._sgNode;
// Re-attach persist nodes
for (i = 0; i < persistNodes.length; ++i) {
node = persistNodes[i];
node.parent = scene;
for (id in persistNodes) {
node = persistNodes[id];
var existNode = scene.getChildByName(node.name);
// Scene contains the persist node, should not reattach, should update the persist node
if (existNode) {
persistNodes[id] = existNode;
existNode._persistNode = true;
}
else {
node.parent = scene;
}
}
scene._activate();
}
+11 -9
View File
@@ -34,7 +34,7 @@ cc.js.mixin(cc.game, {
// Scenes list
_sceneInfos: [],
_persistRootNodes: [],
_persistRootNodes: {},
_ignoreRemovePersistNode: null,
CONFIG_KEY: {
@@ -145,10 +145,12 @@ cc.js.mixin(cc.game, {
* @param {ENode} node - The node to be made persistent
*/
addPersistRootNode: function (node) {
if (!(node instanceof cc.Node))
if (!(node instanceof cc.Node) || !node._id) {
cc.warn('The target can not be made persist because it\'s not a cc.Node or it doesn\'t have _id property.');
return;
var index = this._persistRootNodes.indexOf(node);
if (index === -1) {
}
var id = node._id;
if (!this._persistRootNodes[id]) {
var scene = cc.director._scene;
if (cc.isValid(scene)) {
if (!node.parent) {
@@ -162,7 +164,7 @@ cc.js.mixin(cc.game, {
cc.warn('The node can not be made persist because it\'s not in current scene.');
return;
}
this._persistRootNodes.push(node);
this._persistRootNodes[id] = node;
node._persistNode = true;
}
}
@@ -175,11 +177,11 @@ cc.js.mixin(cc.game, {
*/
removePersistRootNode: function (node) {
if (node !== this._ignoreRemovePersistNode) {
var index = this._persistRootNodes.indexOf(node);
if (index !== -1) {
this._persistRootNodes.splice(index, 1);
var id = node._id || '';
if (node === this._persistRootNodes[id]) {
delete this._persistRootNodes[id];
node._persistNode = false;
}
node._persistNode = false;
}
},