zoukankan      html  css  js  c++  java
  • cocos js 自定义抖动动作

    自定义抖动动作,记录备查。

    一:直接继承自cc.ActionInterval,这种方式目前只能在WEB环境下运行,测试3.5版本JSB环境会报错。

     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);

    二、使用定时器方式,可同时用于WEB和JSB环境

     1 /**
     2  * 自定义抖动效果,用于WEB和JSB环境
     3  * @param {float}duration 抖动时间
     4  * @param {number}shakeStrengthX X轴抖动幅度
     5  * @param {number}shakeStrengthY Y轴抖动幅度
     6  * @param {cc.Node}shakeNode 抖动节点
     7  * @param {string}key 唯一key(WEB环境)
     8  * @example:
     9  * var shakeAction = new Shake2(0.5,20,20,node,"shakeKey");
    10  * shakeAction.shake();
    11  */
    12 var Shake2 = cc.Class.extend({
    13     //抖动时间
    14     duration:0,
    15     //已抖动时间
    16     dtCost:0,
    17     //X轴抖动范围
    18     shakeStrengthX:0,
    19     shakeStrengthY:0,
    20     //抖动节点
    21     shakeNode:null,
    22     //抖动节点初始位置
    23     nodeInitialPos:null,
    24     //定时器绑定回调
    25     bindCallback:null,
    26     //定时器唯一键(WEB)
    27     key:null,
    28     ctor:function(duration,shakeStrengthX,shakeStrengthY,shakeNode,key){
    29         this.duration = duration;
    30         this.shakeStrengthX = shakeStrengthX;
    31         this.shakeStrengthY = shakeStrengthY;
    32         this.shakeNode = shakeNode;
    33         this.nodeInitialPos = shakeNode.getPosition();
    34         this.key = key;
    35     },
    36     shake:function(){
    37         this.bindCallback = this.doSchedule.bind(this);
    38         this.shakeNode.unschedule(this.bindCallback);
    39         if(cc.sys.isNative){
    40             this.shakeNode.schedule(this.bindCallback,0,cc.REPEAT_FOREVER,0);
    41         }else{
    42             this.shakeNode.schedule(this.bindCallback,0,cc.REPEAT_FOREVER,0,this.key);
    43         }
    44         
    45     },
    46     doSchedule:function(dt){
    47         var dt2=dt*50;
    48         var randX=this.getRandomStrength(-this.shakeStrengthX,this.shakeStrengthX)*dt2;
    49         var randY=this.getRandomStrength(-this.shakeStrengthY,this.shakeStrengthY)*dt2;
    50 //        cc.log("randX:"+randX+",randY="+randY);
    51         this.shakeNode.setPosition(cc.pAdd(this.nodeInitialPos,cc.p(randX,randY)));
    52         this.dtCost+=dt;
    53         if(this.dtCost>=this.duration){
    54             this.shakeNode.unschedule(this.bindCallback);
    55             this.shakeNode.setPosition(this.nodeInitialPos);
    56         }
    57     },
    58     //获取两个数间的随机值
    59     getRandomStrength:function(min,max){
    60         return Math.random()*(max-min+1)+min;
    61     },
    62 });

    使用方式:

    1 var shakeAction = new Shake2(0.5,20,20,node,"shakeKey");
    2 shakeAction.shake();

    参考文章:http://blog.csdn.net/teng_ontheway/article/details/25307889

    本文地址:http://www.cnblogs.com/wangjiajun/p/4670036.html

  • 相关阅读:
    如何提取一个转录本的3'UTR区域的序列
    如何研究某个gene的ceRNA 网络
    ceRNA 调控机制
    利用circpedia 数据库探究circRNA的可变剪切
    R语言低级绘图函数-symbols
    R语言低级绘图函数-grid
    R语言低级绘图函数-axis
    R语言低级绘图函数-title
    R语言低级绘图函数-points
    二叉树和二叉查找树之间的区别
  • 原文地址:https://www.cnblogs.com/wangjiajun/p/4670036.html
Copyright © 2011-2022 走看看