From c18c728fbdcabd1de883a2c2212481104b143bc5 Mon Sep 17 00:00:00 2001 From: pandamicro Date: Sun, 28 Feb 2016 20:44:50 +0800 Subject: [PATCH] Fix properties synchronize issue in action (JSB) --- cocos2d/actions/CCActionInstant.js | 4 +- cocos2d/core/CCNode.js | 1 + jsb/index.js | 1 + jsb/jsb-action.js | 262 +++++++++++++++++++++++++++++ jsb/jsb-etc.js | 174 ------------------- 5 files changed, 266 insertions(+), 176 deletions(-) create mode 100644 jsb/jsb-action.js diff --git a/cocos2d/actions/CCActionInstant.js b/cocos2d/actions/CCActionInstant.js index 48fe4485da..d083417421 100644 --- a/cocos2d/actions/CCActionInstant.js +++ b/cocos2d/actions/CCActionInstant.js @@ -251,7 +251,7 @@ cc.FlipX = cc.ActionInstant.extend({ }, update:function (dt) { - this.target.flippedX = this._flippedX; + this.target.scaleX = Math.abs(this.target.scaleX) * (this._flippedX ? 1 : -1); }, reverse:function () { @@ -309,7 +309,7 @@ cc.FlipY = cc.ActionInstant.extend({ }, update:function (dt) { - this.target.flippedY = this._flippedY; + this.target.scaleY = Math.abs(this.target.scaleY) * (this._flippedX ? 1 : -1); }, reverse:function () { diff --git a/cocos2d/core/CCNode.js b/cocos2d/core/CCNode.js index cbc39862b6..f33d2b7f96 100644 --- a/cocos2d/core/CCNode.js +++ b/cocos2d/core/CCNode.js @@ -1044,6 +1044,7 @@ var Node = cc.Class({ if (cc.sys.isNative) { this._retainAction(action); + this._sgNode._owner = this; } cc.director.getActionManager().addAction(action, this, false); return action; diff --git a/jsb/index.js b/jsb/index.js index feea696cad..1ebf0d3b25 100644 --- a/jsb/index.js +++ b/jsb/index.js @@ -21,6 +21,7 @@ require('./jsb-editbox'); require('./jsb-particle'); require('./jsb-enums'); require('./jsb-event'); +require('./jsb-action'); require('./jsb-etc'); // Check version diff --git a/jsb/jsb-action.js b/jsb/jsb-action.js new file mode 100644 index 0000000000..9f69bf348b --- /dev/null +++ b/jsb/jsb-action.js @@ -0,0 +1,262 @@ +// Independent Action from retain/release +var actionArr = [ + 'ActionEase', + 'EaseExponentialIn', + 'EaseExponentialOut', + 'EaseExponentialInOut', + 'EaseSineIn', + 'EaseSineOut', + 'EaseSineInOut', + 'EaseBounce', + 'EaseBounceIn', + 'EaseBounceOut', + 'EaseBounceInOut', + 'EaseBackIn', + 'EaseBackOut', + 'EaseBackInOut', + 'EaseRateAction', + 'EaseIn', + 'EaseElastic', + 'EaseElasticIn', + 'EaseElasticOut', + 'EaseElasticInOut', + 'RemoveSelf', + 'FlipX', + 'FlipY', + 'Place', + 'CallFunc', + 'DelayTime', + 'Sequence', + 'Spawn', + 'Speed', + 'Repeat', + 'RepeatForever', + 'Follow', + 'TargetedAction', + 'Animate', + 'OrbitCamera', + 'GridAction', + 'ProgressTo', + 'ProgressFromTo', + 'ActionInterval', + 'RotateTo', + 'RotateBy', + 'MoveBy', + 'MoveTo', + 'SkewTo', + 'SkewBy', + 'JumpTo', + 'JumpBy', + 'ScaleTo', + 'ScaleBy', + 'Blink', + 'FadeTo', + 'FadeIn', + 'FadeOut', + 'TintTo', + 'TintBy', +]; + +function setCtorReplacer (proto) { + var ctor = proto._ctor; + proto._ctor = function () { + ctor.apply(this, arguments); + this.retain(); + this._retained = true; + }; +} +function setAliasReplacer (name, type) { + var aliasName = name[0].toLowerCase() + name.substr(1); + cc[aliasName] = function () { + var action = type.create.apply(this, arguments); + action.retain(); + action._retained = true; + return action; + }; +} + +for (var i = 0; i < actionArr.length; ++i) { + var name = actionArr[i]; + var type = cc[name]; + if (!type) + continue; + var proto = type.prototype; + setCtorReplacer(proto); + setAliasReplacer(name, type); +} + +function setChainFuncReplacer (proto, name) { + var oldFunc = proto[name]; + proto[name] = function () { + if (this._retained) { + this.release(); + this._retained = false; + } + var newAction = oldFunc.apply(this, arguments); + newAction.retain(); + newAction._retained = true; + return newAction; + }; +} + +setChainFuncReplacer(cc.ActionInterval.prototype, 'repeat'); +setChainFuncReplacer(cc.ActionInterval.prototype, 'repeatForever'); +setChainFuncReplacer(cc.ActionInterval.prototype, 'easing'); + +var jsbRunAction = cc.Node.prototype.runAction; +cc.Node.prototype.runAction = function (action) { + jsbRunAction.call(this, action); + if (action._retained) { + action.release(); + action._retained = false; + } +}; + +function getSGTarget (target) { + if (target instanceof cc.Component) { + target = target.node._sgNode; + } + if (target instanceof cc.Node) { + target = target._sgNode; + } + return target; +} +var jsbAddAction = cc.ActionManager.prototype.addAction; +cc.ActionManager.prototype.addAction = function (action, target, paused) { + target = getSGTarget(target); + jsbAddAction.call(this, action, target, paused); + if (action._retained) { + action.release(); + action._retained = false; + } +}; + +function actionMgrFuncReplacer (funcName, targetPos) { + var proto = cc.ActionManager.prototype; + var oldFunc = proto[funcName]; + proto[funcName] = function () { + arguments[targetPos] = getSGTarget(arguments[targetPos]); + return oldFunc.apply(this, arguments); + }; +} + +var targetRelatedFuncs = [ + ['removeAllActionsFromTarget', 0], + ['removeActionByTag', 1], + ['getActionByTag', 1], + ['numberOfRunningActionsInTarget', 0], + ['pauseTarget', 0], + ['resumeTarget', 0] +]; + +for (var i = 0; i < targetRelatedFuncs.length; ++i) { + actionMgrFuncReplacer.apply(null, targetRelatedFuncs[i]); +} + +cc.ActionManager.prototype.resumeTargets = function (targetsToResume) { + if (!targetsToResume) + return; + + for (var i = 0; i< targetsToResume.length; i++) { + if (targetsToResume[i]) + this.resumeTarget(targetsToResume[i]); + } +}; + +cc.ActionManager.prototype.pauseTargets = function (targetsToPause) { + if (!targetsToPause) + return; + + for (var i = 0; i< targetsToPause.length; i++) { + if (targetsToPause[i]) + this.pauseTarget(targetsToPause[i]); + } +}; + + + +function syncPositionUpdate (dt) { + this._jsbUpdate(dt); + var target = this.getTarget(); + if (target._owner) { + target._owner.x = target.getPositionX(); + target._owner.y = target.getPositionY(); + } +} + +function syncRotationUpdate (dt) { + this._jsbUpdate(dt); + var target = this.getTarget(); + if (target._owner) { + target._owner.rotation = target.getRotation(); + } +} + +function syncScaleUpdate (dt) { + this._jsbUpdate(dt); + var target = this.getTarget(); + if (target._owner) { + target._owner.scaleX = target.getScaleX(); + target._owner.scaleY = target.getScaleY(); + } +} + +function syncRemoveSelfUpdate (dt) { + this._jsbUpdate(dt); + var target = this.getTarget(); + if (target._owner) { + target._owner.removeFromParent(); + } +} + +function syncSkewUpdate (dt) { + this._jsbUpdate(dt); + var target = this.getTarget(); + if (target._owner) { + target._owner.skewX = target.getSkewX(); + target._owner.skewY = target.getSkewY(); + } +} + +// function syncOpacityUpdate (dt) { +// this._jsbUpdate(dt); +// var target = this.getTarget(); +// if (target._owner) { +// target._owner.opacity = target.getOpacity(); +// } +// } + +function syncColorUpdate (dt) { + this._jsbUpdate(dt); + var target = this.getTarget(); + if (target._owner) { + target._owner.color = target.getColor(); + } +} + +var actionUpdate = { + 'MoveTo' : syncPositionUpdate, + 'MoveBy' : syncPositionUpdate, + 'JumpTo' : syncPositionUpdate, + 'JumpBy' : syncPositionUpdate, + 'Place' : syncPositionUpdate, + 'RotateTo' : syncRotationUpdate, + 'RotateBy' : syncRotationUpdate, + 'ScaleTo' : syncScaleUpdate, + 'ScaleBy' : syncScaleUpdate, + 'RemoveSelf' : syncRemoveSelfUpdate, + 'SkewTo' : syncSkewUpdate, + 'SkewBy' : syncSkewUpdate, + // 'Blink' : syncOpacityUpdate, + // 'FadeTo' : syncOpacityUpdate, + // 'FadeIn' : syncOpacityUpdate, + // 'FadeOut' : syncOpacityUpdate, + 'TintTo' : syncColorUpdate, + 'TintBy' : syncColorUpdate +}; + +for (var key in actionUpdate) { + var action = cc[key]; + action.prototype._jsbUpdate = action.prototype.update; + action.prototype.update = actionUpdate[key]; +} \ No newline at end of file diff --git a/jsb/jsb-etc.js b/jsb/jsb-etc.js index 1cf8e96edd..a7d89ea353 100644 --- a/jsb/jsb-etc.js +++ b/jsb/jsb-etc.js @@ -80,180 +80,6 @@ cc.Scheduler.prototype.unschedule = function (callback, target) { this._unschedule(target, callback); }; -// Independent Action from retain/release -var actionArr = [ - 'ActionEase', - 'EaseExponentialIn', - 'EaseExponentialOut', - 'EaseExponentialInOut', - 'EaseSineIn', - 'EaseSineOut', - 'EaseSineInOut', - 'EaseBounce', - 'EaseBounceIn', - 'EaseBounceOut', - 'EaseBounceInOut', - 'EaseBackIn', - 'EaseBackOut', - 'EaseBackInOut', - 'EaseRateAction', - 'EaseIn', - 'EaseElastic', - 'EaseElasticIn', - 'EaseElasticOut', - 'EaseElasticInOut', - 'RemoveSelf', - 'FlipX', - 'FlipY', - 'Place', - 'CallFunc', - 'DelayTime', - 'Sequence', - 'Spawn', - 'Speed', - 'Repeat', - 'RepeatForever', - 'Follow', - 'TargetedAction', - 'Animate', - 'OrbitCamera', - 'GridAction', - 'ProgressTo', - 'ProgressFromTo', - 'ActionInterval', - 'RotateTo', - 'RotateBy', - 'MoveBy', - 'MoveTo', - 'SkewTo', - 'SkewBy', - 'JumpTo', - 'JumpBy', - 'ScaleTo', - 'ScaleBy', - 'Blink', - 'FadeTo', - 'FadeIn', - 'FadeOut', - 'TintTo', - 'TintBy', -]; - -function setCtorReplacer (proto) { - var ctor = proto._ctor; - proto._ctor = function () { - ctor.apply(this, arguments); - this.retain(); - this._retained = true; - }; -} -function setAliasReplacer (name, type) { - var aliasName = name[0].toLowerCase() + name.substr(1); - cc[aliasName] = function () { - var action = type.create.apply(this, arguments); - action.retain(); - action._retained = true; - return action; - }; -} - -for (var i = 0; i < actionArr.length; ++i) { - var name = actionArr[i]; - var type = cc[name]; - if (!type) - continue; - var proto = type.prototype; - setCtorReplacer(proto); - setAliasReplacer(name, type); -} - -function setChainFuncReplacer (proto, name) { - var oldFunc = proto[name]; - proto[name] = function () { - if (this._retained) { - this.release(); - this._retained = false; - } - var newAction = oldFunc.apply(this, arguments); - newAction.retain(); - newAction._retained = true; - return newAction; - }; -} - -setChainFuncReplacer(cc.ActionInterval.prototype, 'repeat'); -setChainFuncReplacer(cc.ActionInterval.prototype, 'repeatForever'); -setChainFuncReplacer(cc.ActionInterval.prototype, 'easing'); - -var jsbRunAction = cc.Node.prototype.runAction; -cc.Node.prototype.runAction = function (action) { - jsbRunAction.call(this, action); - if (action._retained) { - action.release(); - action._retained = false; - } -}; - -function getSGTarget (target) { - if (target instanceof cc.Component) { - target = target.node._sgNode; - } - if (target instanceof cc.Node) { - target = target._sgNode; - } - return target; -} -var jsbAddAction = cc.ActionManager.prototype.addAction; -cc.ActionManager.prototype.addAction = function (action, target, paused) { - target = getSGTarget(target); - jsbAddAction.call(this, action, target, paused); - if (action._retained) { - action.release(); - action._retained = false; - } -}; - -function actionMgrFuncReplacer (funcName, targetPos) { - var proto = cc.ActionManager.prototype; - var oldFunc = proto[funcName]; - proto[funcName] = function () { - arguments[targetPos] = getSGTarget(arguments[targetPos]); - return oldFunc.apply(this, arguments); - }; -} - -var targetRelatedFuncs = [ - ['removeAllActionsFromTarget', 0], - ['removeActionByTag', 1], - ['getActionByTag', 1], - ['numberOfRunningActionsInTarget', 0], - ['pauseTarget', 0], - ['resumeTarget', 0] -]; - -for (var i = 0; i < targetRelatedFuncs.length; ++i) { - actionMgrFuncReplacer.apply(null, targetRelatedFuncs[i]); -} - -cc.ActionManager.prototype.resumeTargets = function (targetsToResume) { - if (!targetsToResume) - return; - - for (var i = 0; i< targetsToResume.length; i++) { - if (targetsToResume[i]) - this.resumeTarget(targetsToResume[i]); - } -}; - -cc.ActionManager.prototype.pauseTargets = function (targetsToPause) { - if (!targetsToPause) - return; - - for (var i = 0; i< targetsToPause.length; i++) { - if (targetsToPause[i]) - this.pauseTarget(targetsToPause[i]); - } -}; // ccsg window._ccsg = {