import { Vec2,Node, v2 } from "cc"; export namespace gi { //让node振动repeat次,振幅amplitude(水平、垂直两方向),频率frequency(ms),time < 0无限振动,time = 0停止振动 //例如:gi.shake(node,5,cc.v2(20,0))振动5次,gi.shake(node,-1)振动无限次,gi.shake(node,0)停止振动 export function shake(node: Node, repeat: number, amplitude?: any, frequency?: number) { let x = node.position.x; let y = node.position.y if (node['amplitude']) { x -= node['amplitude'].x; y -= node['amplitude'].y; node.setPosition(x,y); clearInterval(node['shakeHandle']); } if (repeat === 0) { delete node['shakeHandle']; delete node['amplitude']; delete node['frequency']; return; } repeat = ~~repeat; if (amplitude) { node['amplitude'] = amplitude; } else if (node['amplitude']) { node['amplitude'].x = -node['amplitude'].x; node['amplitude'].y = -node['amplitude'].y; } else return; node['frequency'] = frequency !== undefined ? Math.max(frequency, 10) : node['frequency'] ?? 50; x += node['amplitude'].x; y += node['amplitude'].y; node.setPosition(x,y); let step = v2(); if (repeat > 0) { repeat = Math.max(repeat - 1, 1); step = v2(-node['amplitude'].x / repeat, -node['amplitude'].y / repeat); } node['shakeHandle'] = setInterval(() => { x -= node['amplitude'].x; y -= node['amplitude'].y; node.setPosition(x,y); if (--repeat === 0) { clearInterval(node['shakeHandle']); delete node['shakeHandle']; delete node['amplitude']; delete node['frequency']; return; } node['amplitude'].x = -node['amplitude'].x - step.x; x += node['amplitude'].x; step.x = -step.x; node['amplitude'].y = -node['amplitude'].y - step.y; y += node['amplitude'].y; step.y = -step.y; node.setPosition(x,y); }, node['frequency']); } } window['gi'] = gi;