diff --git a/cocos2d/core/CCNode.js b/cocos2d/core/CCNode.js
index b3c8eb382b..a94dab83b1 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;
@@ -616,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) {
@@ -835,9 +855,188 @@ var Node = cc.Class({
}
},
+ isRunning: function () {
+ 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
+ * @method runAction
+ * @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 .
+ * @method stopAllActions
+ */
+ 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.
+ * @method stopAction
+ * @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.
+ * @method stopActionByTag
+ * @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.
+ * @method getActionByTag
+ * @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.
Schedules a custom selector.
+ * If the selector is already scheduled, then the interval parameter will be updated without scheduling it again.
+ * 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).
+ *
unschedule all scheduled callback functions: custom callback functions, and the 'update' callback function.
+ * Actions are not affected by this method.
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..527c1b63c6 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,18 +519,43 @@ 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; }; +// 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) {