自定义抖动动作,记录备查
1 /** 2 * 自定义抖动动作 3 */ 4 var Shake = cc.ActionInterval.extend({ 5 //节点初始位置 6 nodeInitialPos:null, 7 //X轴抖动幅度 8 nodeShakeStrengthX:0, 9 //Y轴抖动幅度 10 nodeShakeStrengthY:0, 11 //抖动时间 12 duration:0, 13 ctor:function(duration,shakeStrengthX,shakeStrengthY){ 14 cc.ActionInterval.prototype.ctor.call(this); 15 this.duration = duration; 16 this.initWithDuration(duration,shakeStrengthX,shakeStrengthY); 17 18 }, 19 //获取两个数间的随机值 20 getRandomStrength:function(min,max){ 21 return Math.random()*(max-min+1)+min; 22 }, 23 update:function(dt){ 24 var randX=this.getRandomStrength(-this.nodeShakeStrengthX,this.nodeShakeStrengthX)*dt; 25 var randY=this.getRandomStrength(-this.nodeShakeStrengthY,this.nodeShakeStrengthY)*dt; 26 // cc.log("randX:"+randX+",randY="+randY); 27 this.target.setPosition(cc.pAdd(this.nodeInitialPos,cc.p(randX,randY))); 28 }, 29 initWithDuration:function(duration,shakeStrengthX,shakeStrengthY){ 30 if (cc.ActionInterval.prototype.initWithDuration.call(this, duration)) { 31 this.nodeShakeStrengthX=shakeStrengthX; 32 this.nodeShakeStrengthY=shakeStrengthY==‘undefined‘?shakeStrengthX:shakeStrengthY; 33 return true; 34 } 35 return false; 36 }, 37 startWithTarget:function(target){ 38 cc.ActionInterval.prototype.startWithTarget.call(this, target); 39 this.nodeInitialPos=target.getPosition(); 40 }, 41 stop:function(){ 42 this.target.setPosition(this.nodeInitialPos); 43 } 44 }); 45 /** 46 * 自定义抖动动作 47 * @param {float}duration 抖动时间 48 * @param {number}shakeStrengthX X轴抖动幅度 49 * @param {number}shakeStrengthY Y轴抖动幅度 50 * @returns {Shake} 51 */ 52 cc.shake = function(duration,shakeStrengthX,shakeStrengthY){ 53 return new Shake(duration,shakeStrengthX,shakeStrengthY); 54 };
使用方式:
1 var action = cc.shake(0.4,20,20); 2 xx.runAction(action);
参考文章:http://blog.csdn.net/teng_ontheway/article/details/25307889
本文地址:http://www.cnblogs.com/wangjiajun/p/4670036.html
时间: 2024-10-10 12:28:59