Skip to content

Commit

Permalink
Improve persistRootNode transition to new scene (cocos#3018)
Browse files Browse the repository at this point in the history
  • Loading branch information
pandamicro authored Jul 26, 2018
1 parent b3a6310 commit 64edd72
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 34 deletions.
34 changes: 13 additions & 21 deletions cocos2d/core/CCDirector.js
Original file line number Diff line number Diff line change
Expand Up @@ -414,19 +414,27 @@ cc.Director.prototype = {
scene._load(); // ensure scene initialized
CC_BUILD && CC_DEBUG && console.timeEnd('InitScene');

// detach persist nodes
// Re-attach or replace persist nodes
CC_BUILD && CC_DEBUG && console.time('AttachPersist');
var persistNodeList = Object.keys(game._persistRootNodes).map(function (x) {
return game._persistRootNodes[x];
});
for (let i = 0; i < persistNodeList.length; i++) {
let node = persistNodeList[i];
game._ignoreRemovePersistNode = node;
node.parent = null;
game._ignoreRemovePersistNode = null;
var existNode = scene.getChildByUuid(node.uuid);
if (existNode) {
// scene also contains the persist node, select the old one
var index = existNode.getSiblingIndex();
existNode._destroyImmediate();
scene.insertChild(node, index);
}
else {
node.parent = scene;
}
}
CC_BUILD && CC_DEBUG && console.timeEnd('AttachPersist');

var oldScene = this._scene;

if (!CC_EDITOR) {
// auto release assets
CC_BUILD && CC_DEBUG && console.time('AutoRelease');
Expand Down Expand Up @@ -455,22 +463,6 @@ cc.Director.prototype = {
// Run an Entity Scene
this._scene = scene;

// Re-attach or replace persist nodes
CC_BUILD && CC_DEBUG && console.time('AttachPersist');
for (let i = 0; i < persistNodeList.length; i++) {
let node = persistNodeList[i];
var existNode = scene.getChildByUuid(node.uuid);
if (existNode) {
// scene also contains the persist node, select the old one
var index = existNode.getSiblingIndex();
existNode._destroyImmediate();
scene.insertChild(node, index);
}
else {
node.parent = scene;
}
}
CC_BUILD && CC_DEBUG && console.timeEnd('AttachPersist');
CC_BUILD && CC_DEBUG && console.time('Activate');
scene._activate();
CC_BUILD && CC_DEBUG && console.timeEnd('Activate');
Expand Down
17 changes: 4 additions & 13 deletions cocos2d/core/CCGame.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ var game = {
RENDER_TYPE_OPENGL: 2,

_persistRootNodes: {},
_ignoreRemovePersistNode: null,

// states
_paused: true,//whether the game is paused
Expand Down Expand Up @@ -436,12 +435,6 @@ var game = {
}
},

/**
* !#en cc.game is the singleton object for game related functions.
* !#zh cc.game 是 Game 的实例,用来驱动整个游戏。
* @class Game
*/

/**
* !#en Run game with configuration object and onStart function.
* !#zh 运行游戏,并且指定引擎配置和 onStart 的回调。
Expand Down Expand Up @@ -499,12 +492,10 @@ var game = {
* @param {Node} node - The node to be removed from persistent node list
*/
removePersistRootNode: function (node) {
if (node !== this._ignoreRemovePersistNode) {
var id = node.uuid || '';
if (node === this._persistRootNodes[id]) {
delete this._persistRootNodes[id];
node._persistNode = false;
}
var id = node.uuid || '';
if (node === this._persistRootNodes[id]) {
delete this._persistRootNodes[id];
node._persistNode = false;
}
},

Expand Down

0 comments on commit 64edd72

Please sign in to comment.