CocosCreator-屏幕震动

it2025-12-14  7

脚本挂在Camera身上

cc.Class({ extends: cc.Component, properties: { _state: "idle" }, // LIFE-CYCLE CALLBACKS: // onLoad () {}, /**设置全部 */ setAll(x, y, times, duration) { this.amplitudeX = x; this.amplitudeY = y; this.duration = duration; this.times = times; }, shaker(type = "out") { let node = this.node; this.shakeObj = { node: node, originPos: node.position, offset: cc.v2(20, 20), times: 4, duration: 0.4, startTime: Date.now(), amplitudeModifier: "out" } this.shakeObj.amplitudeModifier = type; this._state = "shaker"; this.curTime = 0; }, start() { }, update(dt) { if (this._state == "shaker") { let shakeObj = this.shakeObj; this.node.position = cc.Vec2.ZERO; this.curTime += dt; var aX = shakeObj.offset.x; var aY = shakeObj.offset.y; var n = shakeObj.times; var duration = shakeObj.duration; var range = n * 2 * Math.PI; var am = shakeObj.amplitudeModifier; if (this.curTime < duration) { var factor = 1; var percent = this.curTime / duration; // console.log("百分比:::", percent); var angle = range * percent; var xo = aX * Math.cos(angle); var yo = aY * Math.sin(angle); if (am === "in") { factor = percent; } else if (am === "out") { factor = 1 - percent; } else if (am === "inOut") { factor = 2 * (percent < 0.5 ? percent : 1 - percent); } xo *= factor; yo *= factor; let p = shakeObj.originPos.add(cc.v2(xo, yo)); // console.log("pppp", p); shakeObj.node.position = p; } else { this._state = "idle"; this.curTime = 0; shakeObj.node.position = shakeObj.originPos; } } }, });
最新回复(0)