96 lines
3.1 KiB
TypeScript
Raw Normal View History

import AnimatorCustomization from "../../animator/AnimatorCustomization";
import { AnimationPlayer } from "../../animator/core/AnimatorBase";
const { ccclass, property } = cc._decorator;
@ccclass
export default class CustomizationScene extends cc.Component implements AnimationPlayer {
@property(cc.Node) Cube: cc.Node = null;
private _animator: AnimatorCustomization = null;
private _curTween: cc.Tween = null;
private _call: () => void = null;
private _traget: any = null;
protected onLoad() {
this._animator = this.Cube.getComponent(AnimatorCustomization);
// 自定义动画播放必须要将实现了AnimationPlayer接口的对象传入
this._animator.onInit(this);
this.node.on(cc.Node.EventType.TOUCH_START, () => { this._animator.setTrigger('next'); }, this);
}
private move(dur: number, pos: cc.Vec3, loop: boolean) {
if (loop) {
this._curTween = cc.tween(this.Cube)
.repeatForever(
cc.tween()
.to(dur, { position: pos })
.call(() => {
this._call.call(this._traget);
})
)
.start();
} else {
this._curTween = cc.tween(this.Cube)
.to(dur, { position: pos })
.call(() => {
this._call.call(this._traget);
})
.start();
}
}
/**
* - AnimationPlayer
*
*/
public setFinishedCallback(callback: () => void, target: any): void {
this._call = callback;
this._traget = target;
}
/**
* - AnimationPlayer
*
*/
public playAnimation(animName: string, loop: boolean): void {
this._curTween && this._curTween.stop();
if (animName === 'idle') {
if (loop) {
this._curTween = cc.tween(this.Cube)
.repeatForever(
cc.tween()
.to(1, { scale: 2 })
.to(1, { scale: 0.5 })
.call(() => {
this._call.call(this._traget);
})
)
.start();
} else {
this._curTween = cc.tween(this.Cube)
.to(1, { scale: 2 })
.to(1, { scale: 0.5 })
.call(() => {
this._call.call(this._traget);
})
.start();
}
} else if (animName === 'move1') {
this.move(2, cc.v3(500, 500, 0), loop);
} else if (animName === 'move2') {
this.move(4, cc.v3(0, 0, 0), loop);
}
}
/**
* - AnimationPlayer
*
*/
public scaleTime(scale: number): void {
}
}