cocos js 自訂抖動動作

來源:互聯網
上載者:User

標籤:

自訂抖動動作,記錄備查

 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

cocos js 自訂抖動動作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.