54 lines
1.3 KiB
JavaScript
54 lines
1.3 KiB
JavaScript
"use strict";
|
|
cc._RF.push(module, '61affD5SO9Ipo0fGmIeTFTZ', 'Shake');
|
|
// Script/tool/Shake.js
|
|
|
|
"use strict";
|
|
|
|
/**
|
|
* 自定义抖动效果
|
|
*/
|
|
var Shake = cc.Class({
|
|
"extends": cc.Component,
|
|
properties: {
|
|
//抖动时间
|
|
duration: 0,
|
|
//X轴抖动范围
|
|
shakeX: 0,
|
|
shakeY: 0
|
|
},
|
|
shake: function shake(callback) {
|
|
if (this.shaking) {
|
|
return;
|
|
}
|
|
|
|
this.callback = callback;
|
|
this.shaking = true;
|
|
this.dtCost = 0;
|
|
this.nodeInitialPos = this.node.getPosition();
|
|
this.unschedule(this.doSchedule);
|
|
this.schedule(this.doSchedule, 0, cc.macro.REPEAT_FOREVER, 0);
|
|
},
|
|
doSchedule: function doSchedule(dt) {
|
|
var dt2 = dt * 50;
|
|
var randX = this.getRandomStrength(-this.shakeX, this.shakeX) * dt2;
|
|
var randY = this.getRandomStrength(-this.shakeY, this.shakeY) * dt2;
|
|
this.node.setPosition(cc.pAdd(this.nodeInitialPos, cc.v2(randX, randY)));
|
|
this.dtCost += dt;
|
|
|
|
if (this.dtCost >= this.duration) {
|
|
this.unschedule(this.doSchedule);
|
|
this.node.setPosition(this.nodeInitialPos);
|
|
this.shaking = false;
|
|
|
|
if (this.callback) {
|
|
this.callback();
|
|
}
|
|
}
|
|
},
|
|
//获取两个数间的随机值
|
|
getRandomStrength: function getRandomStrength(min, max) {
|
|
return Math.random() * (max - min + 1) + min;
|
|
}
|
|
});
|
|
|
|
cc._RF.pop(); |