一:直接继承自cc.ActionInterval,这种方式目前只能在WEB环境下运行,测试3.5版本JSB环境会报错。
/**
* 自定义抖动动作
*/
var Shake = cc.ActionInterval.extend({
//节点初始位置
nodeInitialPos:null,
//X轴抖动幅度
nodeShakeStrengthX:0,
//Y轴抖动幅度
nodeShakeStrengthY:0,
//抖动时间
duration:0,
ctor:function(duration,shakeStrengthX,shakeStrengthY){
cc.ActionInterval.prototype.ctor.call(this);
this.duration = duration;
this.initWithDuration(duration,shakeStrengthX,shakeStrengthY);
},
//获取两个数间的随机值
getRandomStrength:function(min,max){
return Math.random()*(max-min+1)+min;
},
update:function(dt){
var randX=this.getRandomStrength(-this.nodeShakeStrengthX,this.nodeShakeStrengthX)*dt;
var randY=this.getRandomStrength(-this.nodeShakeStrengthY,this.nodeShakeStrengthY)*dt;
// cc.log("randX:"+randX+",randY="+randY);
this.target.setPosition(cc.pAdd(this.nodeInitialPos,cc.p(randX,randY)));
},
initWithDuration:function(duration,shakeStrengthX,shakeStrengthY){
if (cc.ActionInterval.prototype.initWithDuration.call(this, duration)) {
this.nodeShakeStrengthX=shakeStrengthX;
this.nodeShakeStrengthY=shakeStrengthY=='undefined'?shakeStrengthX:shakeStrengthY;
return true;
}
return false;
},
startWithTarget:function(target){
cc.ActionInterval.prototype.startWithTarget.call(this, target);
this.nodeInitialPos=target.getPosition();
},
stop:function(){
this.target.setPosition(this.nodeInitialPos);
}
});
/**
* 自定义抖动动作
* @param {float}duration 抖动时间
* @param {number}shakeStrengthX X轴抖动幅度
* @param {number}shakeStrengthY Y轴抖动幅度
* @returns {Shake}
*/
cc.shake = function(duration,shakeStrengthX,shakeStrengthY){
return new Shake(duration,shakeStrengthX,shakeStrengthY);
};
二、使用定时器方式,可同时用于WEB和JSB环境
/**
* 自定义抖动效果,用于WEB和JSB环境
* @param {float}duration 抖动时间
* @param {number}shakeStrengthX X轴抖动幅度
* @param {number}shakeStrengthY Y轴抖动幅度
* @param {cc.Node}shakeNode 抖动节点
* @param {string}key 唯一key(WEB环境)
* @example:
* var shakeAction = new Shake2(0.5,20,20,node,"shakeKey");
* shakeAction.shake();
*/
var Shake2 = cc.Class.extend({
//抖动时间
duration:0,
//已抖动时间
dtCost:0,
//X轴抖动范围
shakeStrengthX:0,
shakeStrengthY:0,
//抖动节点
shakeNode:null,
//抖动节点初始位置
nodeInitialPos:null,
//定时器绑定回调
bindCallback:null,
//定时器唯一键(WEB)
key:null,
ctor:function(duration,shakeStrengthX,shakeStrengthY,shakeNode,key){
this.duration = duration;
this.shakeStrengthX = shakeStrengthX;
this.shakeStrengthY = shakeStrengthY;
this.shakeNode = shakeNode;
this.nodeInitialPos = shakeNode.getPosition();
this.key = key;
},
shake:function(){
this.bindCallback = this.doSchedule.bind(this);
this.shakeNode.unschedule(this.bindCallback);
if(cc.sys.isNative){
this.shakeNode.schedule(this.bindCallback,0,cc.REPEAT_FOREVER,0);
}else{
this.shakeNode.schedule(this.bindCallback,0,cc.REPEAT_FOREVER,0,this.key);
}
},
doSchedule:function(dt){
var dt2=dt*50;
var randX=this.getRandomStrength(-this.shakeStrengthX,this.shakeStrengthX)*dt2;
var randY=this.getRandomStrength(-this.shakeStrengthY,this.shakeStrengthY)*dt2;
// cc.log("randX:"+randX+",randY="+randY);
this.shakeNode.setPosition(cc.pAdd(this.nodeInitialPos,cc.p(randX,randY)));
this.dtCost+=dt;
if(this.dtCost>=this.duration){
this.shakeNode.unschedule(this.bindCallback);
this.shakeNode.setPosition(this.nodeInitialPos);
}
},
//获取两个数间的随机值
getRandomStrength:function(min,max){
return Math.random()*(max-min+1)+min;
},
});
来源https://www.cnblogs.com/wangjiajun/p/4670036.html