Merge pull request #578 from jareguo/master

should be able to deactivate self during onLoad
This commit is contained in:
Jare Guo
2016-04-11 10:04:55 +08:00
8 changed files with 290 additions and 273 deletions
+46 -10
View File
@@ -30,6 +30,7 @@ var JS = cc.js;
var Flags = cc.Object.Flags;
var Destroying = Flags.Destroying;
var DontDestroy = Flags.DontDestroy;
var Activating = Flags.Activating;
//var RegisteredInEditor = Flags.RegisteredInEditor;
/**
@@ -847,6 +848,20 @@ var Node = cc.Class({
},
_onActivatedInHierarchy: function (newActive) {
var cancelActivation = false;
if (this._objFlags & Activating) {
if (newActive) {
cc.error('Node "%s" is already activating', this.name);
return;
}
else {
cancelActivation = true;
}
}
else if (newActive) {
this._objFlags |= Activating;
}
this._activeInHierarchy = newActive;
// component maybe added during onEnable, and the onEnable of new component is already called
@@ -854,42 +869,63 @@ var Node = cc.Class({
var originCount = this._components.length;
for (var c = 0; c < originCount; ++c) {
var component = this._components[c];
if ( !(component instanceof cc.Component) ) {
if (component instanceof cc.Component) {
component.__onNodeActivated(newActive);
if (newActive && !this._activeInHierarchy) {
// deactivated during activating
this._objFlags &= ~Activating;
return;
}
}
else {
if (CC_EDITOR) {
cc.error('Sorry, the component of "%s" which with an index of %s is corrupted! It has been removed.', this.name, c);
cc.error('Sorry, the component of "%s" which with an index of %s is corrupted! It has been removed.',
this.name, c);
console.log('Corrupted component value:', component);
}
if (component) {
this._removeComponent(component);
}
else {
this._components.splice(c, 1);
JS.array.removeAt(this._components, c);
}
--c;
--originCount;
}
else {
component.__onNodeActivated(newActive);
}
}
// activate children recursively
for (var i = 0, len = this.childrenCount; i < len; ++i) {
for (var i = 0, len = this._children.length; i < len; ++i) {
var child = this._children[i];
if (child._active) {
child._onActivatedInHierarchy(newActive);
if (newActive && !this._activeInHierarchy) {
// deactivated during activating
this._objFlags &= ~Activating;
return;
}
}
}
// activate or desactivate ActionManager, EventManager
// Activate
if (cancelActivation) {
this._objFlags &= ~Activating;
return;
}
// ActionManager, EventManager
if (newActive) {
// activate
cc.director.getActionManager().resumeTarget(this);
cc.eventManager.resumeTarget(this);
}
// Desactivate
else {
// deactivate
cc.director.getActionManager().pauseTarget(this);
cc.eventManager.pauseTarget(this);
}
//
this._objFlags &= ~Activating;
},
_onHierarchyChanged: function (oldParent) {
+19 -73
View File
@@ -75,6 +75,11 @@ function callOnEnable (self, enable) {
}
}
var deactivatedDuringOnEnable = !self.node._activeInHierarchy;
if (deactivatedDuringOnEnable) {
return;
}
cc.director.getScheduler().resumeTarget(self);
_registerEvent(self, true);
@@ -520,75 +525,6 @@ var Component = cc.Class({
return this.node.getComponentsInChildren(typeOrClassName);
},
///**
// * Invokes the method on this component after a specified delay.
// * The method will be invoked even if this component is disabled, but will not invoked if this component is
// * destroyed.
// *
// * @method invoke
// * @param {function|string} functionOrMethodName
// * @param {number} [delay=0] - The number of seconds that the function call should be delayed by. If omitted, it defaults to 0. The actual delay may be longer.
// * @return {number} - Will returns a new InvokeID if the functionOrMethodName is type function. InvokeID is the numerical ID of the invoke, which can be used later with cancelInvoke().
// * @example {@link examples/Fire/Component/invoke.js }
// */
//invoke: createInvoker(Timer.setTimeout, Timer.setTimeoutWithKey, 'invoke'),
//
///**
// * Invokes the method on this component repeatedly, with a fixed time delay between each call.
// * The method will be invoked even if this component is disabled, but will not invoked if this component is
// * destroyed.
// *
// * @method repeat
// * @param {function|string} functionOrMethodName
// * @param {number} [delay=0] - The number of seconds that the function call should wait before each call to the method. If omitted, it defaults to 0. The actual delay may be longer.
// * @return {number} - Will returns a new RepeatID if the method is type function. RepeatID is the numerical ID of the repeat, which can be used later with cancelRepeat().
// * @example {@link examples/Fire/Component/repeat.js}
// */
//repeat: createInvoker(Timer.setInterval, Timer.setIntervalWithKey, 'repeat'),
//
///**
// * Cancels previous invoke calls with methodName or InvokeID on this component.
// * When using methodName, all calls with the same methodName will be canceled.
// * InvokeID is the identifier of the invoke action you want to cancel, as returned by invoke().
// *
// * @method cancelInvoke
// * @param {string|number} methodNameOrInvokeId
// * @example {@link examples/Fire/Component/cancelInvoke.js}
// */
//cancelInvoke: function (methodNameOrInvokeId) {
// if (typeof methodNameOrInvokeId === 'string') {
// var key = this.id + '.' + methodNameOrInvokeId;
// Timer.clearTimeoutByKey(key);
// }
// else {
// Timer.clearTimeout(methodNameOrInvokeId);
// }
//},
//
///**
// * Cancels previous repeat calls with methodName or RepeatID on this component.
// * When using methodName, all calls with the same methodName will be canceled.
// * RepeatID is the identifier of the repeat action you want to cancel, as returned by repeat().
// *
// * @method cancelRepeat
// * @param {string|number} methodNameOrRepeatId
// * @example {@link examples/Fire/Component/cancelRepeat.js}
// */
//cancelRepeat: function (methodNameOrRepeatId) {
// if (typeof methodNameOrRepeatId === 'string') {
// var key = this.id + '.' + methodNameOrRepeatId;
// Timer.clearIntervalByKey(key);
// }
// else {
// Timer.clearInterval(methodNameOrRepeatId);
// }
//},
//
//isInvoking: function (methodName) {
// var key = this.id + '.' + methodName;
// return Timer.hasTimeoutKey(key);
//},
// VIRTUAL
/**
@@ -665,7 +601,7 @@ var Component = cc.Class({
},
__onNodeActivated: CC_EDITOR ? function (active) {
if (!(this._objFlags & IsOnLoadStarted) &&
if (active && !(this._objFlags & IsOnLoadStarted) &&
(cc.engine._isPlaying || this.constructor._executeInEditMode)) {
this._objFlags |= IsOnLoadStarted;
@@ -688,20 +624,30 @@ var Component = cc.Class({
_Scene.AssetsWatcher.start(this);
}
}
if (this._enabled) {
if (active) {
var deactivatedOnLoading = !this.node._activeInHierarchy;
if (deactivatedOnLoading) {
return;
}
}
callOnEnable(this, active);
}
} : function (active) {
if (!(this._objFlags & IsOnLoadStarted)) {
if (active && !(this._objFlags & IsOnLoadStarted)) {
this._objFlags |= IsOnLoadStarted;
if (this.onLoad) {
this.onLoad();
}
this._objFlags |= IsOnLoadCalled;
}
if (this._enabled) {
if (active) {
var deactivatedOnLoading = !this.node._activeInHierarchy;
if (deactivatedOnLoading) {
return;
}
}
callOnEnable(this, active);
}
},
+4 -2
View File
@@ -5,10 +5,11 @@ var JS = require('./js');
var Destroyed = 1 << 0;
var ToDestroy = 1 << 1;
var DontSave = 1 << 2;
var EditorOnly = 1 << 3;
var EditorOnly = 1 << 3;
var Dirty = 1 << 4;
var DontDestroy = 1 << 5;
var Destroying = 1 << 6;
var Activating = 1 << 7;
//var RegisteredInEditor = 1 << 8;
var HideInGame = 1 << 9;
var HideInEditor = 1 << 10;
@@ -26,7 +27,7 @@ var IsPositionLocked = 1 << 20;
var Hide = HideInGame | HideInEditor;
// should not clone or serialize these flags
var PersistentMask = ~(ToDestroy | Dirty | Destroying | DontDestroy |
var PersistentMask = ~(ToDestroy | Dirty | Destroying | DontDestroy | Activating |
IsOnEnableCalled | IsOnLoadStarted | IsOnLoadCalled | IsOnStartCalled
/*RegisteredInEditor*/);
@@ -93,6 +94,7 @@ CCObject.Flags = {
// FLAGS FOR ENGINE
Destroying: Destroying,
Activating: Activating,
/**
* !#en
+11 -33
View File
@@ -1222,22 +1222,16 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{
cc.assert(child, cc._LogInfos.Node.addChild_3);
cc.assert(child._parent === null, "child already added. It can't be added again");
this._addChildHelper(child, localZOrder, tag, name, setTag);
},
// invokes the parent setter
child.parent = this;
_addChildHelper: function(child, localZOrder, tag, name, setTag){
this._insertChild(child, localZOrder);
child.zIndex = localZOrder;
if (setTag)
child.setTag(tag);
else
child.setName(name);
},
_insertChild: function (child, z) {
child.parent = this;
child.zIndex = z;
},
// composition: REMOVE
/**
@@ -1282,14 +1276,14 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{
* node.removeChild(newNode, false);
*/
removeChild: function (child, cleanup) {
// explicit nil handling
if (this._children.length === 0)
return;
if (cleanup === undefined)
cleanup = true;
if (this._children.indexOf(child) > -1)
this._detachChild(child, cleanup);
if (this._children.indexOf(child) > -1) {
// If you don't do cleanup, the child's actions will not get removed and the
if (cleanup || cleanup === undefined) {
child.cleanup();
}
// invoke the parent setter
child.parent = null;
}
},
/**
@@ -1354,22 +1348,6 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{
this._children.length = 0;
},
_detachChild: function (child, doCleanup) {
// IMPORTANT:
// - 1st do onExit
// - 2nd cleanup
//if (this._running) {
// child.onExitTransitionDidStart();
// child.onExit();
//}
// If you don't do cleanup, the child's actions will not get removed and the
if (doCleanup)
child.cleanup();
child.parent = null;
},
setNodeDirty: function(){
this._sgNode.setNodeDirty();
},
+4
View File
@@ -61,6 +61,10 @@ cc.Enum = function (obj) {
var reverseKey = '' + val;
if (key !== reverseKey) {
if (enumType.hasOwnProperty(reverseKey) && CC_EDITOR) {
cc.error('%s already defined in Enum.', reverseKey);
continue;
}
Object.defineProperty(enumType, reverseKey, {
value: key,
enumerable: false
+3
View File
@@ -389,6 +389,9 @@ if (CC_DEV) {
running: 'activeInHierarchy, active',
_realOpacity: '_opacity, _opacity',
_realColor: '_color, _color',
_insertChild: 'addChild',
_addChildHelper: 'addChild',
_detachChild: 'removeChild',
getZOrder: 'getLocalZOrder',
setZOrder: 'setLocalZOrder',
getOrderOfArrival: 'getSiblingIndex',
-154
View File
@@ -66,157 +66,3 @@ test('should not cause an infinite loops if re-activated in onLoad', function ()
// var comp = parent.addComponent(Comp);
// comp.stopTest();
//});
//asyncTest('invoke using name', function () {
// var cb = new Callback().setDisabledMessage('method should not invokes in this frame');
// var cb2 = new Callback().disable('method should not being called after destroyed');
//
// var MyComp = Fire.Class({
// extends: Component,
// onLoad: function () {
// this.invoke('myCallback', 0.001);
// },
// myCallback: cb,
// callback2: cb2
// });
// cc.executeInEditMode(MyComp);
//
// var ent = new cc.Node();
// var comp = ent.addComponent(MyComp);
//
// cb.enable();
// setTimeout(function () {
// cb.once('method should being invoked');
//
// comp.invoke('callback2', 0);
// comp.destroy();
// FO._deferredDestroy();
//
// setTimeout(function () {
// start();
// }, 1);
// }, 0.001 * 1000);
//});
//
//asyncTest('cancel invoke using name', 0, function () {
// var cb1 = new Callback().disable('method 1 should not invoked if canceled');
//
// var MyComp = Fire.Class({
// extends: Component,
// myCallback1: cb1,
// });
// cc.executeInEditMode(MyComp);
// var ent = new cc.Node();
// var comp = ent.addComponent(MyComp);
//
// comp.invoke('myCallback1', 0.001);
// comp.invoke('myCallback1', 0.001);
//
// comp.cancelInvoke('myCallback1');
//
// setTimeout(start, 0.001 * 1000);
//});
//
//asyncTest('invoke using function', function () {
// var cb = new Callback().setDisabledMessage('method should not invokes in this frame');
// var cb2 = new Callback().disable('method should not being called after destroyed');
//
// var MyComp = Fire.Class({
// extends: Component,
// onLoad: function () {
// this.invoke(this.myCallback, 0.001);
// },
// myCallback: cb,
// callback2: cb2
// });
// cc.executeInEditMode(MyComp);
//
// var ent = new cc.Node();
// var comp = ent.addComponent(MyComp);
//
// cb.enable();
// setTimeout(function () {
// cb.once('method should being invoked');
//
// comp.invoke(comp.callback2, 0);
// comp.destroy();
// FO._deferredDestroy();
//
// setTimeout(function () {
// start();
// }, 1);
// }, 0.001 * 1000);
//});
//
//asyncTest('cancel invoke using id', 0, function () {
// var cb1 = new Callback().disable('method 1 should not invoked if canceled');
//
// var MyComp = Fire.Class({
// extends: Component,
// myCallback1: cb1,
// });
// cc.executeInEditMode(MyComp);
// var ent = new cc.Node();
// var comp = ent.addComponent(MyComp);
//
// var id1 = comp.invoke(comp.myCallback1, 0.001);
// comp.cancelInvoke(id1);
//
// setTimeout(start, 0.001 * 1000);
//});
//
//asyncTest('repeat using name', function () {
// var cb = new Callback(function () {
// if (cb.calledCount > 1) {
// ok(true, 'method should being invoked repeatedly');
// cb.disable();
// comp.cancelRepeat('myCallback');
// clearTimeout(stopId);
// start();
// }
// }).enable();
//
// var MyComp = Fire.Class({
// extends: Component,
// myCallback: cb,
// });
// cc.executeInEditMode(MyComp);
//
// var ent = new cc.Node();
// var comp = ent.addComponent(MyComp);
//
// comp.repeat('myCallback', 0);
//
// var stopId = setTimeout(function () {
// ok(false, 'Timeout: method should being invoked repeatedly');
// start();
// }, 100);
//});
//
//asyncTest('repeat using function', function () {
// var cb = new Callback(function () {
// if (cb.calledCount > 1) {
// ok(true, 'method should being invoked repeatedly');
// cb.disable();
// comp.cancelRepeat(repeatId);
// clearTimeout(stopId);
// start();
// }
// }).enable();
//
// var MyComp = Fire.Class({
// extends: Component,
// myCallback: cb,
// });
// cc.executeInEditMode(MyComp);
//
// var ent = new cc.Node();
// var comp = ent.addComponent(MyComp);
//
// var repeatId = comp.repeat(cb, 0);
//
// var stopId = setTimeout(function () {
// ok(false, 'Timeout: method should being invoked repeatedly');
// start();
// }, 100);
//});
+203 -1
View File
@@ -260,7 +260,7 @@ test('life cycle logic for component', function () {
this._assert(CallbackTester.OnEnable);
comp.notExpect(CallbackTester.OnEnable, 'onEnable should only be called once');
this.enabled = true;
}
};
comp.expect(CallbackTester.OnEnable, 'onEnable should be called once after entity active');
obj.active = true;
@@ -315,6 +315,208 @@ test('activation logic for component in hierarchy', function () {
comp.stopTest();
});
(function () {
var StillInvokeRestCompsOnSameNode = false;
var StillInvokeOnEnableOnSameComp = false;
if (StillInvokeRestCompsOnSameNode) {
strictEqual(StillInvokeOnEnableOnSameComp, true);
}
function createNormalComp (node) {
var comp = node.addComponent(cc.Component);
comp.onLoad = new Callback().enable();
comp.onEnable = new Callback(function () {
this.onDisable.enable();
}).enable();
comp.onDisable = new Callback().disable('onDisable should called after onEnable');
comp.update = new Callback().disable('all update should not be called (deactivated)');
return comp;
}
function createDisabledComp (node, name) {
var comp = node.addComponent(cc.Component);
comp.onLoad = new Callback().disable('onLoad of ' + name + ' component should not be called (deactivated)');
comp.onEnable = new Callback().disable('onEnable of ' + name + ' component should not be called (deactivated)');
comp.update = new Callback().disable('update of ' + name + ' component should not be called (deactivated)');
return comp;
}
function compShouldBeActivated (comp, name) {
comp.onLoad.once('onLoad of ' + name + ' component should be called').disable();
comp.onEnable.once('onEnable of ' + name + ' component should be called').disable();
comp.onDisable.once('onDisable of ' + name + ' component should be called').disable();
}
test('could deactivate self node in onLoad', function () {
var node = new cc.Node();
var previousComp = createNormalComp(node);
var testComp = node.addComponent(cc.Component);
testComp.onLoad = function () {
this.node.active = false;
};
if (StillInvokeOnEnableOnSameComp) {
testComp.onEnable = new Callback(function () {
this.onDisable.enable();
}).enable();
testComp.onDisable = new Callback().disable('onDisable should called after onEnable');
}
else {
testComp.onEnable = new Callback().disable(
'onEnable of testing component should not be called (deactivated)');
testComp.onDisable = new Callback().disable(
'onDisable of testing component should not be called (deactivated)');
}
testComp.update = new Callback().disable('update of testing component should not be called (deactivated)');
var restComp;
if (StillInvokeRestCompsOnSameNode) {
restComp = createNormalComp(node);
}
else {
restComp = createDisabledComp(node, 'rest');
}
var child = new cc.Node();
createDisabledComp(child, 'child');
cc.director.getScene().addChild(node);
strictEqual(node.active, false, 'node should be deactivated');
compShouldBeActivated(previousComp, 'previous');
if (StillInvokeOnEnableOnSameComp) {
testComp.onEnable.once('onEnable of test component should still be called').disable();
testComp.onDisable.once('onDisable of test component should still be called').disable();
}
if (StillInvokeRestCompsOnSameNode) {
compShouldBeActivated(restComp, 'rest');
}
cc.game.step();
});
test('could deactivate parent in onLoad', function () {
strictEqual(StillInvokeRestCompsOnSameNode, false, 'test cases not implemented if "StillInvokeRestCompsOnSameNode"');
strictEqual(StillInvokeOnEnableOnSameComp, false, 'test cases not implemented if "StillInvokeOnEnableOnSameComp"');
var parent = new cc.Node();
var prevNode = new cc.Node();
var node = new cc.Node();
var nextNode = new cc.Node();
parent.addChild(prevNode);
parent.addChild(node);
parent.addChild(nextNode);
var child = new cc.Node();
node.addChild(child);
// init parent
var compOfParent = createNormalComp(parent);
// init prevNode
var compOfPrevNode = createNormalComp(prevNode);
// init nextNode
var compOfNextNode = createDisabledComp(nextNode, 'next node\'s');
// init node
var previousComp = createNormalComp(node);
var testComp = node.addComponent(cc.Component);
testComp.onLoad = function () {
// deactivate parent
parent.active = false;
};
if (StillInvokeOnEnableOnSameComp) {
testComp.onEnable = new Callback(function () {
this.onDisable.enable();
}).enable();
testComp.onDisable = new Callback().disable('onDisable should called after onEnable');
}
else {
testComp.onEnable = new Callback().disable(
'onEnable of testing component should not be called (deactivated)');
testComp.onDisable = new Callback().disable(
'onDisable of testing component should not be called (deactivated)');
}
testComp.update = new Callback().disable('update of testing component should not be called (deactivated)');
var restComp;
if (StillInvokeRestCompsOnSameNode) {
restComp = createNormalComp(node);
}
else {
restComp = createDisabledComp(node, 'rest');
}
// init child
createDisabledComp(child, 'child');
// ACTIVATE
cc.director.getScene().addChild(parent);
// test
strictEqual(parent.active, false, 'parent should be deactivated');
strictEqual(node.active, true, 'node should be deactivated');
compShouldBeActivated(compOfParent, 'parent node\'s');
compShouldBeActivated(compOfPrevNode, 'previous node\'s');
compShouldBeActivated(previousComp, 'previous');
if (StillInvokeOnEnableOnSameComp) {
testComp.onEnable.once('onEnable of test component should still be called').disable();
testComp.onDisable.once('onDisable of test component should still be called').disable();
}
if (StillInvokeRestCompsOnSameNode) {
compShouldBeActivated(restComp, 'rest');
}
cc.game.step();
});
test('could deactivate self node in onEnable', function () {
var node = new cc.Node();
var previousComp = createNormalComp(node);
var testComp = node.addComponent(cc.Component);
testComp.onEnable = new Callback(function () {
this.node.active = false;
}).enable();
testComp.update = new Callback().disable('update of testing component should not be called (deactivated)');
var restComp;
if (StillInvokeRestCompsOnSameNode) {
restComp = createNormalComp(node);
}
else {
restComp = createDisabledComp(node, 'rest');
}
var child = new cc.Node();
createDisabledComp(child, 'child');
cc.director.getScene().addChild(node);
strictEqual(node.active, false, 'node should be deactivated');
compShouldBeActivated(previousComp, 'previous');
testComp.onEnable.once('onEnable of test component should still be called').disable();
if (StillInvokeRestCompsOnSameNode) {
compShouldBeActivated(restComp, 'rest');
}
cc.game.step();
});
})();
test('destroy', function () {
var parent = new cc.Node();
var child = new cc.Node();