Skip to content

Commit aa69d8c

Browse files
committed
Improve ActionManager performance and fix memory leak caused by element pool
1 parent 1fa0ac4 commit aa69d8c

File tree

1 file changed

+20
-60
lines changed

1 file changed

+20
-60
lines changed

cocos2d/core/CCActionManager.js

Lines changed: 20 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -30,25 +30,13 @@
3030
* @example
3131
* var element = new cc.HashElement();
3232
*/
33-
cc.HashElement = cc.Class.extend(/** @lends cc.HashElement# */{
34-
actions:null,
35-
target:null, //ccobject
36-
actionIndex:0,
37-
currentAction:null, //CCAction
38-
currentActionSalvaged:false,
39-
paused:false,
40-
/**
41-
* Constructor
42-
*/
43-
ctor:function () {
44-
this.actions = [];
45-
this.target = null;
46-
this.actionIndex = 0;
47-
this.currentAction = null; //CCAction
48-
this.currentActionSalvaged = false;
49-
this.paused = false;
50-
}
51-
});
33+
cc.HashElement = function () {
34+
this.actions = [];
35+
this.target = null;
36+
this.actionIndex = 0;
37+
this.currentAction = null; //CCAction
38+
this.paused = false;
39+
};
5240

5341
/**
5442
* cc.ActionManager is a class that can manage actions.<br/>
@@ -64,10 +52,6 @@ cc.HashElement = cc.Class.extend(/** @lends cc.HashElement# */{
6452
* var mng = new cc.ActionManager();
6553
*/
6654
cc.ActionManager = cc.Class.extend(/** @lends cc.ActionManager# */{
67-
_hashTargets:null,
68-
_arrayTargets:null,
69-
_currentTarget:null,
70-
_currentTargetSalvaged:false,
7155
_elementPool: [],
7256

7357
_searchElementByTarget:function (arr, target) {
@@ -82,7 +66,6 @@ cc.ActionManager = cc.Class.extend(/** @lends cc.ActionManager# */{
8266
this._hashTargets = {};
8367
this._arrayTargets = [];
8468
this._currentTarget = null;
85-
this._currentTargetSalvaged = false;
8669
},
8770

8871
_getElement: function (target, paused) {
@@ -99,8 +82,8 @@ cc.ActionManager = cc.Class.extend(/** @lends cc.ActionManager# */{
9982
element.actions.length = 0;
10083
element.actionIndex = 0;
10184
element.currentAction = null;
102-
element.currentActionSalvaged = false;
10385
element.paused = false;
86+
element.target = null;
10487
this._elementPool.push(element);
10588
},
10689

@@ -156,15 +139,8 @@ cc.ActionManager = cc.Class.extend(/** @lends cc.ActionManager# */{
156139
return;
157140
var element = this._hashTargets[target.__instanceId];
158141
if (element) {
159-
if (element.actions.indexOf(element.currentAction) !== -1 && !(element.currentActionSalvaged))
160-
element.currentActionSalvaged = true;
161-
162142
element.actions.length = 0;
163-
if (this._currentTarget === element && !forceDelete) {
164-
this._currentTargetSalvaged = true;
165-
} else {
166-
this._deleteHashElement(element);
167-
}
143+
this._deleteHashElement(element);
168144
}
169145
},
170146
/** Removes an action given an action reference.
@@ -181,6 +157,9 @@ cc.ActionManager = cc.Class.extend(/** @lends cc.ActionManager# */{
181157
for (var i = 0; i < element.actions.length; i++) {
182158
if (element.actions[i] === action) {
183159
element.actions.splice(i, 1);
160+
// update actionIndex in case we are in tick. looping over the actions
161+
if (element.actionIndex >= i)
162+
element.actionIndex--;
184163
break;
185164
}
186165
}
@@ -291,10 +270,10 @@ cc.ActionManager = cc.Class.extend(/** @lends cc.ActionManager# */{
291270
* @param {Array} targetsToResume
292271
*/
293272
resumeTargets:function(targetsToResume){
294-
if(!targetsToResume)
273+
if (!targetsToResume)
295274
return;
296275

297-
for(var i = 0 ; i< targetsToResume.length; i++){
276+
for (var i = 0; i< targetsToResume.length; i++) {
298277
if(targetsToResume[i])
299278
this.resumeTarget(targetsToResume[i]);
300279
}
@@ -311,21 +290,14 @@ cc.ActionManager = cc.Class.extend(/** @lends cc.ActionManager# */{
311290
_removeActionAtIndex:function (index, element) {
312291
var action = element.actions[index];
313292

314-
if ((action === element.currentAction) && (!element.currentActionSalvaged))
315-
element.currentActionSalvaged = true;
316-
317293
element.actions.splice(index, 1);
318294

319295
// update actionIndex in case we are in tick. looping over the actions
320296
if (element.actionIndex >= index)
321297
element.actionIndex--;
322298

323299
if (element.actions.length === 0) {
324-
if (this._currentTarget === element) {
325-
this._currentTargetSalvaged = true;
326-
} else {
327-
this._deleteHashElement(element);
328-
}
300+
this._deleteHashElement(element);
329301
}
330302
},
331303

@@ -356,25 +328,17 @@ cc.ActionManager = cc.Class.extend(/** @lends cc.ActionManager# */{
356328
for (var elt = 0; elt < locTargets.length; elt++) {
357329
this._currentTarget = locTargets[elt];
358330
locCurrTarget = this._currentTarget;
359-
//this._currentTargetSalvaged = false;
360-
if (!locCurrTarget.paused) {
331+
if (!locCurrTarget.paused && locCurrTarget.actions) {
361332
// The 'actions' CCMutableArray may change while inside this loop.
362-
for (locCurrTarget.actionIndex = 0;
363-
locCurrTarget.actionIndex < (locCurrTarget.actions ? locCurrTarget.actions.length : 0);
364-
locCurrTarget.actionIndex++) {
333+
for (locCurrTarget.actionIndex = 0; locCurrTarget.actionIndex < locCurrTarget.actions.length; locCurrTarget.actionIndex++) {
365334
locCurrTarget.currentAction = locCurrTarget.actions[locCurrTarget.actionIndex];
366335
if (!locCurrTarget.currentAction)
367336
continue;
368337

369-
locCurrTarget.currentActionSalvaged = false;
370338
//use for speed
371339
locCurrTarget.currentAction.step(dt * ( locCurrTarget.currentAction._speedMethod ? locCurrTarget.currentAction._speed : 1 ) );
372-
if (locCurrTarget.currentActionSalvaged) {
373-
// The currentAction told the node to remove it. To prevent the action from
374-
// accidentally deallocating itself before finishing its step, we retained
375-
// it. Now that step is done, it's safe to release it.
376-
locCurrTarget.currentAction = null;//release
377-
} else if (locCurrTarget.currentAction.isDone()) {
340+
341+
if (locCurrTarget.currentAction.isDone()) {
378342
locCurrTarget.currentAction.stop();
379343
var action = locCurrTarget.currentAction;
380344
// Make currentAction nil to prevent removeAction from salvaging it.
@@ -385,12 +349,8 @@ cc.ActionManager = cc.Class.extend(/** @lends cc.ActionManager# */{
385349
locCurrTarget.currentAction = null;
386350
}
387351
}
388-
389-
// elt, at this moment, is still valid
390-
// so it is safe to ask this here (issue #490)
391-
392352
// only delete currentTarget if no actions were scheduled during the cycle (issue #481)
393-
if (this._currentTargetSalvaged && locCurrTarget.actions.length === 0) {
353+
if (locCurrTarget.actions.length === 0) {
394354
this._deleteHashElement(locCurrTarget) && elt--;
395355
}
396356
}

0 commit comments

Comments
 (0)