12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- import { _decorator, CCFloat, CCInteger, Component, easing, Enum, tween, Vec3 } from 'cc';
- const { ccclass, property } = _decorator;
- enum AnimationType {
- BOUNCE = 0, //上下弹跳
- BREATHE = 1 //呼吸动画
- }
- @ccclass('BounceAnimation')
- export class BounceAnimation extends Component {
- @property({ type: Enum(AnimationType), tooltip: '动画类型' })
- animationType: AnimationType = AnimationType.BOUNCE;
- //弹跳动画参数
- @property({ type: CCInteger, tooltip: '缓动高度', visible() { return this.animationType === AnimationType.BOUNCE } })
- bounceHeight: number = 20;
- @property({ type: CCInteger, tooltip: '缓动时间(秒)', visible() { return this.animationType === AnimationType.BOUNCE } })
- duration: number = 0.5;
- //呼吸动画参数
- @property({ type: CCFloat, tooltip: '缩放幅度', visible() { return this.animationType === AnimationType.BREATHE } })
- scaleSize: number = 0.05;
- @property({ type: CCInteger, tooltip: '呼吸周期(秒)', visible() { return this.animationType === AnimationType.BREATHE } })
- breatheDuration: number = 1;
- private originalPosition: Vec3 = new Vec3();
- private originalScale: Vec3 = new Vec3(); // 新增原始缩放值记录
- start() {
- this.originalPosition = this.node.position.clone();
- this.originalScale = this.node.scale.clone(); // 记录原始缩放值
- this.startAnimation();
- }
- /**
- * 开始执行动画
- */
- public startAnimation() {
- const btn_play = this.node;
- if(this.animationType === AnimationType.BOUNCE) {
- tween(btn_play)
- .delay(Math.random())
- .to(this.duration,{position: new Vec3(this.originalPosition.x, this.originalPosition.y + this.bounceHeight, this.originalPosition.z)}, { easing: easing.linear }) // 向上缓动
- .to(this.duration,{position: new Vec3(this.originalPosition.x, this.originalPosition.y - this.bounceHeight, this.originalPosition.z)}, { easing: easing.linear }) // 向下缓动
- .to(this.duration,{position: new Vec3(this.originalPosition.x, this.originalPosition.y, this.originalPosition.z)}, { easing: easing.linear }) // 向下缓动
- .union()
- .repeatForever() // 无限循环
- .start(); // 启动动画
- } else if(this.animationType === AnimationType.BREATHE) {
- tween(btn_play)
- .sequence(
- tween().to(this.breatheDuration, {
- scale: new Vec3(
- this.originalScale.x + this.scaleSize,
- this.originalScale.y + this.scaleSize,
- 1
- )
- }),
- tween().to(this.breatheDuration, {
- scale: this.originalScale
- })
- )
- .repeatForever()
- .start();
- }
- }
- }
|