From 3b45183b481857be6ce3fec08d20a10a1e38828d Mon Sep 17 00:00:00 2001
From: pandamicro
Date: Thu, 24 Dec 2015 01:38:19 +0800
Subject: [PATCH 1/4] Improve cc.Node for eventManager support and fix for JSB
---
cocos2d/core/CCNode.js | 23 ++++++++++++-
cocos2d/core/event-manager/CCEventListener.js | 14 ++------
jsb_polyfill.js | 34 +++++++++++++++++--
3 files changed, 55 insertions(+), 16 deletions(-)
diff --git a/cocos2d/core/CCNode.js b/cocos2d/core/CCNode.js
index b3c8eb382b..32af4574f5 100644
--- a/cocos2d/core/CCNode.js
+++ b/cocos2d/core/CCNode.js
@@ -242,6 +242,9 @@ var Node = cc.Class({
this._name = typeof name !== 'undefined' ? name : 'New Node';
this._activeInHierarchy = false;
+ // Support for Scheduler, ActionManager and EventManager
+ this.__instanceId = this._id || cc.ClassManager.getNewInstanceId();
+
// cache component
this._widget = null;
@@ -837,7 +840,23 @@ var Node = cc.Class({
});
-Node.EventType = EventType;
+// In JSB, when inner sg node being replaced, the system event listeners will be cleared.
+// We need a mechanisme to guarentee the persistence of system event listeners.
+if (cc.sys.isNative) {
+ cc.js.getset(Node.prototype, '_sgNode', function () {
+ return this.__sgNode;
+ }, function (value) {
+ this.__sgNode = value;
+ if (this._touchListener) {
+ cc.eventManager.removeListener(this._touchListener);
+ cc.eventManager.addListener(this._touchListener, this);
+ }
+ if (this._mouseListener) {
+ cc.eventManager.removeListener(this._mouseListener);
+ cc.eventManager.addListener(this._mouseListener, this);
+ }
+ }, true);
+}
/**
* @event position-changed
@@ -861,4 +880,6 @@ Node.EventType = EventType;
* @event opacity-changed
*/
+Node.EventType = EventType;
+
cc.Node = module.exports = Node;
diff --git a/cocos2d/core/event-manager/CCEventListener.js b/cocos2d/core/event-manager/CCEventListener.js
index d790b3b305..28f2c9f00a 100644
--- a/cocos2d/core/event-manager/CCEventListener.js
+++ b/cocos2d/core/event-manager/CCEventListener.js
@@ -146,18 +146,8 @@ cc.EventListener = cc._Class.extend(/** @lends cc.EventListener# */{
* @private
*/
_setSceneGraphPriority: function (node) {
- if (cc.Node && node instanceof cc.Node) {
- this._target = node;
- this._node = node._sgNode;
- }
- else if (cc.Component && node instanceof cc.Component) {
- this._target = node;
- this._node = node.node._sgNode;
- }
- else {
- this._target = node;
- this._node = node;
- }
+ this._target = node;
+ this._node = node;
},
/**
diff --git a/jsb_polyfill.js b/jsb_polyfill.js
index f015551140..4f3e3a2ad5 100644
--- a/jsb_polyfill.js
+++ b/jsb_polyfill.js
@@ -499,6 +499,12 @@ cc.Label.prototype.setOverflow = function () {};
cc.Label.prototype.enableWrapText = function () {};
cc.Label.prototype.setLineHeight = function () {};
+// cc.Event#getCurrentTarget
+cc.Event.prototype._getCurrentTarget = cc.Event.prototype.getCurrentTarget;
+cc.Event.prototype.getCurrentTarget = function () {
+ return this._currentTarget || this._getCurrentTarget();
+};
+
// cc.eventManager.addListener
cc.eventManager.addListener = function(listener, nodeOrPriority) {
if(!(listener instanceof cc.EventListener)) {
@@ -513,13 +519,35 @@ cc.eventManager.addListener = function(listener, nodeOrPriority) {
cc.eventManager.addEventListenerWithFixedPriority(listener, nodeOrPriority);
} else {
+ var node = nodeOrPriority;
if (nodeOrPriority instanceof cc.Component) {
- nodeOrPriority = nodeOrPriority.node._sgNode;
+ node = nodeOrPriority.node._sgNode;
}
if (nodeOrPriority instanceof cc.Node) {
- nodeOrPriority = nodeOrPriority._sgNode;
+ node = nodeOrPriority._sgNode;
}
- cc.eventManager.addEventListenerWithSceneGraphPriority(listener, nodeOrPriority);
+ // rebind target
+ if (node !== nodeOrPriority) {
+ var keys = Object.keys(listener);
+ // Overwrite all functions
+ for (var i = 0; i < keys.length; ++i) {
+ var key = keys[i];
+ var value = listener[key];
+ if (typeof value === 'function') {
+ // var _value = value;
+ listener[key] = function (event1, event2) {
+ // event must be the last argument, and arguments count could be 1 or 2
+ var event = event2 || event1;
+ // Replace event's _currentTarget
+ if (event) {
+ event._currentTarget = nodeOrPriority;
+ }
+ value.call(this, event1, event2);
+ };
+ }
+ }
+ }
+ cc.eventManager.addEventListenerWithSceneGraphPriority(listener, node);
}
return listener;
From f6a2cb84dea7fceb0689d0c2eea7cc21e0b1ac5c Mon Sep 17 00:00:00 2001
From: pandamicro
Date: Thu, 24 Dec 2015 01:39:25 +0800
Subject: [PATCH 2/4] cc.Node support Scheduler
---
cocos2d/core/CCNode.js | 84 +++++++++++++++++++++++++++++++++++++
cocos2d/core/CCScheduler.js | 4 +-
jsb_polyfill.js | 3 ++
3 files changed, 89 insertions(+), 2 deletions(-)
diff --git a/cocos2d/core/CCNode.js b/cocos2d/core/CCNode.js
index 32af4574f5..b721cdfdc6 100644
--- a/cocos2d/core/CCNode.js
+++ b/cocos2d/core/CCNode.js
@@ -838,6 +838,90 @@ var Node = cc.Class({
}
},
+ isRunning: function () {
+ return this.active;
+ },
+
+// Scheduler
+
+ /**
+ * Schedules a custom selector.
+ * If the selector is already scheduled, then the interval parameter will be updated without scheduling it again.
+ * @function
+ * @param {function} callback The callback function
+ * @param {Number} [interval=0] Tick interval in seconds. 0 means tick every frame. If interval = 0, it's recommended to use scheduleUpdate() instead.
+ * @param {Number} [repeat=cc.REPEAT_FOREVER] The selector will be executed (repeat + 1) times, you can use kCCRepeatForever for tick infinitely.
+ * @param {Number} [delay=0] The amount of time that the first tick will wait before execution.
+ */
+ schedule: function (callback, interval, repeat, delay) {
+ cc.assert(callback, cc._LogInfos.Node.schedule);
+ cc.assert(interval >= 0, cc._LogInfos.Node.schedule_2);
+
+ var key = this.__instanceId;
+ interval = interval || 0;
+ repeat = isNaN(repeat) ? cc.REPEAT_FOREVER : repeat;
+ delay = delay || 0;
+
+ cc.director.getScheduler().schedule(callback, this, interval, repeat, delay, !this.active, key);
+ },
+
+ /**
+ *
+ * schedules the "update" callback function with a custom priority.
+ * This callback function will be called every frame.
+ * Scheduled callback functions with a lower priority will be called before the ones that have a higher value.
+ * Only one "update" callback function could be scheduled per node (You can't have 2 'update' callback functions).
+ *
+ * @function
+ * @param {Number} [priority=0] The priority of the update callback
+ */
+ scheduleUpdate: function (priority) {
+ priority = priority || 0;
+ cc.director.getScheduler().scheduleUpdate(this, priority, !this.active);
+ },
+
+ /**
+ * Schedules a callback function that runs only once, with a delay of 0 or larger
+ * @function
+ * @see cc.Node#schedule
+ * @param {function} callback A function wrapped as a selector
+ * @param {Number} [delay=0] The amount of time that the first tick will wait before execution.
+ */
+ scheduleOnce: function (callback, delay) {
+ this.schedule(callback, 0, 0, delay);
+ },
+
+ /**
+ * Unschedules a custom callback function.
+ * @function
+ * @see cc.Node#schedule
+ * @param {function} callback_fn A function wrapped as a selector
+ */
+ unschedule: function (callback_fn) {
+ if (!callback_fn)
+ return;
+
+ cc.director.getScheduler().unschedule(callback_fn, this);
+ },
+
+ /**
+ * Unschedules the "update" method.
+ * @function
+ * @see cc.Node#scheduleUpdate
+ */
+ unscheduleUpdate: function () {
+ cc.director.getScheduler().unscheduleUpdate(this);
+ },
+
+ /**
+ * unschedule all scheduled callback functions: custom callback functions, and the 'update' callback function.
+ * Actions are not affected by this method.
+ * @function
+ */
+ unscheduleAllCallbacks: function () {
+ cc.director.getScheduler().unscheduleAllForTarget(this);
+ },
+
});
// In JSB, when inner sg node being replaced, the system event listeners will be cleared.
diff --git a/cocos2d/core/CCScheduler.js b/cocos2d/core/CCScheduler.js
index 85c5fe9038..617f110805 100644
--- a/cocos2d/core/CCScheduler.js
+++ b/cocos2d/core/CCScheduler.js
@@ -277,8 +277,8 @@ cc.TimerTargetCallback = cc.Timer.extend({
});
var getTargetId = function (target) {
- return target.uuid || target.__instanceId;
-}
+ return target.__instanceId || target.uuid;
+};
/**
*
diff --git a/jsb_polyfill.js b/jsb_polyfill.js
index 4f3e3a2ad5..527c1b63c6 100644
--- a/jsb_polyfill.js
+++ b/jsb_polyfill.js
@@ -553,6 +553,9 @@ cc.eventManager.addListener = function(listener, nodeOrPriority) {
return listener;
};
+// cc.Scheduler
+cc.Scheduler.prototype.scheduleUpdate = cc.Scheduler.prototype.scheduleUpdateForTarget;
+
// cc.Scale9Sprite
cc.Scale9Sprite.prototype._setBlendFunc = cc.Scale9Sprite.prototype.setBlendFunc;
cc.Scale9Sprite.prototype.setBlendFunc = function(blendFunc, dst) {
From 91dfbcaf6180b89f4c916abaf68ce7b84bdd3b98 Mon Sep 17 00:00:00 2001
From: pandamicro
Date: Thu, 24 Dec 2015 01:39:45 +0800
Subject: [PATCH 3/4] cc.Node Support Action
---
cocos2d/core/CCNode.js | 96 ++++++++++++++++++++++++++++++++++++++++++
1 file changed, 96 insertions(+)
diff --git a/cocos2d/core/CCNode.js b/cocos2d/core/CCNode.js
index b721cdfdc6..55842e45d6 100644
--- a/cocos2d/core/CCNode.js
+++ b/cocos2d/core/CCNode.js
@@ -619,6 +619,23 @@ var Node = cc.Class({
child._onActivatedInHierarchy(newActive);
}
}
+ // activate or desactivate ActionManager, Scheduler, EventManager
+ var target = this;
+ if (cc.sys.isNative) {
+ target = this._sgNode;
+ }
+ // Activate
+ if (newActive) {
+ cc.director.getScheduler().resumeTarget(this);
+ cc.director.getActionManager().resumeTarget(target);
+ cc.eventManager.resumeTarget(target);
+ }
+ // Desactivate
+ else {
+ cc.director.getScheduler().pauseTarget(this);
+ cc.director.getActionManager().pauseTarget(target);
+ cc.eventManager.pauseTarget(target);
+ }
},
_onHierarchyChanged: function (oldParent) {
@@ -842,6 +859,85 @@ var Node = cc.Class({
return this.active;
},
+// ACTIONS
+ /**
+ * Executes an action, and returns the action that is executed.
+ * The node becomes the action's target. Refer to cc.Action's getTarget()
+ * Calling runAction while the node is not active won't have any effect
+ * @function
+ * @param {cc.Action} action
+ * @return {cc.Action} An Action pointer
+ */
+ runAction: function (action) {
+ if (!this.active)
+ return;
+ cc.assert(action, cc._LogInfos.Node.runAction);
+
+ var target = cc.sys.isNative ? this._sgNode : this;
+ cc.director.getActionManager().addAction(action, target, false);
+ return action;
+ },
+
+ /**
+ * Stops and removes all actions from the running action list .
+ * @function
+ */
+ stopAllActions: function () {
+ var target = cc.sys.isNative ? this._sgNode : this;
+ cc.director.getActionManager().removeAllActionsFromTarget(target);
+ },
+
+ /**
+ * Stops and removes an action from the running action list.
+ * @function
+ * @param {cc.Action} action An action object to be removed.
+ */
+ stopAction: function (action) {
+ cc.director.getActionManager().removeAction(action);
+ },
+
+ /**
+ * Removes an action from the running action list by its tag.
+ * @function
+ * @param {Number} tag A tag that indicates the action to be removed.
+ */
+ stopActionByTag: function (tag) {
+ if (tag === cc.ACTION_TAG_INVALID) {
+ cc.log(cc._LogInfos.Node.stopActionByTag);
+ return;
+ }
+ var target = cc.sys.isNative ? this._sgNode : this;
+ cc.director.getActionManager().removeActionByTag(tag, target);
+ },
+
+ /**
+ * Returns an action from the running action list by its tag.
+ * @function
+ * @see cc.Action#getTag and cc.Action#setTag
+ * @param {Number} tag
+ * @return {cc.Action} The action object with the given tag.
+ */
+ getActionByTag: function (tag) {
+ if (tag === cc.ACTION_TAG_INVALID) {
+ cc.log(cc._LogInfos.Node.getActionByTag);
+ return null;
+ }
+ var target = cc.sys.isNative ? this._sgNode : this;
+ cc.director.getActionManager().getActionByTag(tag, target);
+ },
+
+ /** Returns the numbers of actions that are running plus the ones that are schedule to run (actions in actionsToAdd and actions arrays).
+ * Composable actions are counted as 1 action. Example:
+ * If you are running 1 Sequence of 7 actions, it will return 1.
+ * If you are running 7 Sequences of 2 actions, it will return 7.
+ * @function
+ * @return {Number} The number of actions that are running plus the ones that are schedule to run
+ */
+ getNumberOfRunningActions: function () {
+ var target = cc.sys.isNative ? this._sgNode : this;
+ cc.director.getActionManager().numberOfRunningActionsInTarget(target);
+ },
+
// Scheduler
/**
From c27fa489e4db271ba920400c8c0332df49e75c5c Mon Sep 17 00:00:00 2001
From: pandamicro
Date: Thu, 24 Dec 2015 11:50:30 +0800
Subject: [PATCH 4/4] Updated documentation
---
cocos2d/core/CCNode.js | 24 ++++++++++++------------
1 file changed, 12 insertions(+), 12 deletions(-)
diff --git a/cocos2d/core/CCNode.js b/cocos2d/core/CCNode.js
index 55842e45d6..a94dab83b1 100644
--- a/cocos2d/core/CCNode.js
+++ b/cocos2d/core/CCNode.js
@@ -864,7 +864,7 @@ var Node = cc.Class({
* Executes an action, and returns the action that is executed.
* The node becomes the action's target. Refer to cc.Action's getTarget()
* Calling runAction while the node is not active won't have any effect
- * @function
+ * @method runAction
* @param {cc.Action} action
* @return {cc.Action} An Action pointer
*/
@@ -880,7 +880,7 @@ var Node = cc.Class({
/**
* Stops and removes all actions from the running action list .
- * @function
+ * @method stopAllActions
*/
stopAllActions: function () {
var target = cc.sys.isNative ? this._sgNode : this;
@@ -889,7 +889,7 @@ var Node = cc.Class({
/**
* Stops and removes an action from the running action list.
- * @function
+ * @method stopAction
* @param {cc.Action} action An action object to be removed.
*/
stopAction: function (action) {
@@ -898,7 +898,7 @@ var Node = cc.Class({
/**
* Removes an action from the running action list by its tag.
- * @function
+ * @method stopActionByTag
* @param {Number} tag A tag that indicates the action to be removed.
*/
stopActionByTag: function (tag) {
@@ -912,7 +912,7 @@ var Node = cc.Class({
/**
* Returns an action from the running action list by its tag.
- * @function
+ * @method getActionByTag
* @see cc.Action#getTag and cc.Action#setTag
* @param {Number} tag
* @return {cc.Action} The action object with the given tag.
@@ -930,7 +930,7 @@ var Node = cc.Class({
* Composable actions are counted as 1 action. Example:
* If you are running 1 Sequence of 7 actions, it will return 1.
* If you are running 7 Sequences of 2 actions, it will return 7.
- * @function
+ * @method getNumberOfRunningActions
* @return {Number} The number of actions that are running plus the ones that are schedule to run
*/
getNumberOfRunningActions: function () {
@@ -943,7 +943,7 @@ var Node = cc.Class({
/**
* Schedules a custom selector.
* If the selector is already scheduled, then the interval parameter will be updated without scheduling it again.
- * @function
+ * @method schedule
* @param {function} callback The callback function
* @param {Number} [interval=0] Tick interval in seconds. 0 means tick every frame. If interval = 0, it's recommended to use scheduleUpdate() instead.
* @param {Number} [repeat=cc.REPEAT_FOREVER] The selector will be executed (repeat + 1) times, you can use kCCRepeatForever for tick infinitely.
@@ -968,7 +968,7 @@ var Node = cc.Class({
* Scheduled callback functions with a lower priority will be called before the ones that have a higher value.
* Only one "update" callback function could be scheduled per node (You can't have 2 'update' callback functions).
*
- * @function
+ * @method scheduleUpdate
* @param {Number} [priority=0] The priority of the update callback
*/
scheduleUpdate: function (priority) {
@@ -978,7 +978,7 @@ var Node = cc.Class({
/**
* Schedules a callback function that runs only once, with a delay of 0 or larger
- * @function
+ * @method scheduleOnce
* @see cc.Node#schedule
* @param {function} callback A function wrapped as a selector
* @param {Number} [delay=0] The amount of time that the first tick will wait before execution.
@@ -989,7 +989,7 @@ var Node = cc.Class({
/**
* Unschedules a custom callback function.
- * @function
+ * @method unschedule
* @see cc.Node#schedule
* @param {function} callback_fn A function wrapped as a selector
*/
@@ -1002,7 +1002,7 @@ var Node = cc.Class({
/**
* Unschedules the "update" method.
- * @function
+ * @method unscheduleUpdate
* @see cc.Node#scheduleUpdate
*/
unscheduleUpdate: function () {
@@ -1012,7 +1012,7 @@ var Node = cc.Class({
/**
* unschedule all scheduled callback functions: custom callback functions, and the 'update' callback function.
* Actions are not affected by this method.
- * @function
+ * @method unscheduleAllCallbacks
*/
unscheduleAllCallbacks: function () {
cc.director.getScheduler().unscheduleAllForTarget(this);