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;