/** * 摇动组件脚本,通过定时改变Sprite的旋转角度,实现摇动效果 * 添加方法:Sprite.addComponent(ShakeScript); * 移除方法:(Sprite.getComponent(ShakeScript) as ShakeScript).destroy(); */ export default class ShakeScript extends Laya.Script { //旋转角度循环数组 public static angleList = [ 0, 3, 5, 7, 5, 3, 0, -3, -5, -7, -5, -3, ]; /** * 每帧更新时执行,通过对帧序号取模,按次序拿到旋转角度的值 */ onUpdate() { let index = Laya.timer.currFrame % ShakeScript.angleList.length; (this.owner as Laya.Sprite).rotation = ShakeScript.angleList[index]; } }