Custom jitter action, record for reference
1 /**2 * Custom jitter action3 */4 varShake =cc. Actioninterval.extend ({5 //node initial position6Nodeinitialpos:NULL,7 //x-axis jitter amplitude8nodeshakestrengthx:0,9 //y-axis jitter amplitudeTennodeshakestrengthy:0, One //jitter Time Aduration:0, -ctorfunction(duration,shakestrengthx,shakestrengthy) { -Cc. ActionInterval.prototype.ctor.call ( This); the This. Duration =duration; - This. Initwithduration (duration,shakestrengthx,shakestrengthy); - - }, + //get a random value between two numbers -Getrandomstrength:function(Min,max) { + returnMath.random () * (max-min+1) +min; A }, atUpdatefunction(DT) { - varrandx= This. Getrandomstrength (- This. NODESHAKESTRENGTHX, This. nodeshakestrengthx) *DT; - varrandy= This. Getrandomstrength (- This. Nodeshakestrengthy, This. nodeshakestrengthy) *DT; - //Cc.log ("Randx:" +randx+ ", randy=" +randy); - This. Target.setposition (Cc.padd ( This. NODEINITIALPOS,CC.P (Randx,randy))); - }, inInitwithduration:function(duration,shakestrengthx,shakestrengthy) { - if(CC. ActionInterval.prototype.initWithDuration.call ( This, duration)) { to This. nodeshakestrengthx=Shakestrengthx; + This. nodeshakestrengthy=shakestrengthy== ' undefined '?shakestrengthx:shakestrengthy; - return true; the } * return false; $ },Panax NotoginsengStartwithtarget:function(target) { -Cc. ActionInterval.prototype.startWithTarget.call ( This, target); the This. nodeinitialpos=target.getposition (); + }, AStopfunction(){ the This. Target.setposition ( This. Nodeinitialpos); + } - }); $ /** $ * Custom jitter action - * @param {float}duration jitter time - * @param {number}shakestrengthx x axis jitter amplitude the * @param {number}shakestrengthy y-axis jitter amplitude - * @returns {Shake}Wuyi */ theCc.shake =function(duration,shakestrengthx,shakestrengthy) { - return NewShake (duration,shakestrengthx,shakestrengthy); Wu};
How to use:
1 var action = Cc.shake (0.4,20,20); 2 xx.runaction (action);
Reference article: http://blog.csdn.net/teng_ontheway/article/details/25307889
This address: http://www.cnblogs.com/wangjiajun/p/4670036.html
Cocos JS Custom jitter Action