AnimationUtils.ts
2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import FrameAnimation from "../components/FrameAnimation";
import { ScheduleUtils } from "./ScheduleUtils";
export namespace AnimationUtils {
export function playFrameAnimation(node: cc.Node, loop: boolean, name: string, callback?: (loopCount?: number) => void) {
let anims = node.getComponents(FrameAnimation)
if (!anims || anims.length == 0) {
return
}
let findOne: FrameAnimation = anims[0]
for (let i = 0; i < anims.length; i++) {
const anim = anims[i];
if (anim.tag == name) {
findOne = anim
} else {
anim.enabled = false
}
}
findOne.enabled = true;
findOne.play(callback, loop);
}
export function playFrameAnimationOnce(node: cc.Node, name: string, callback?: (loopCount?: number) => void) {
playFrameAnimation(node, false, name, callback);
}
export function playFrameAnimationLoop(node: cc.Node, name: string) {
playFrameAnimation(node, true, name);
}
export function animateProgress(progressBar: cc.ProgressBar, progress: number, speed = 1, label?: cc.Label): Promise<void> {
return new Promise((resolve) => {
let oldProgress = progressBar.progress
if (oldProgress == progress) {
resolve();
return;
}
if (progress > 1) progress = 1;
if (progress < 0) progress = 0;
let delta = progress - oldProgress;
if ((<any>progressBar).updateFunc) ScheduleUtils.unscheduleUpdate((<any>progressBar).updateFunc);
const update = (dt: number) => {
oldProgress += delta * dt * speed;
if ((delta > 0 && oldProgress > progress) || (delta < 0 && oldProgress < progress)) {
oldProgress = progress;
delete (<any>progressBar).updateFunc;
ScheduleUtils.unscheduleUpdate(update);
resolve();
}
progressBar.progress = oldProgress;
if (label) label.string = `${Math.round(oldProgress * 1000) / 10}%`;
}
(<any>progressBar).updateFunc = update;
ScheduleUtils.scheduleUpdate(update);
});
}
//异步动画执行API. 方便链式调用.
export async function runAction(node: cc.Node | cc.Component, action: cc.FiniteTimeAction | cc.FiniteTimeAction[]): Promise<any> {
return new Promise<any>(resolve => {
if (!action || !node) {
console.info("runAction param is null:", node, action)
resolve();
return;
}
let nd = node instanceof cc.Component ? node.node : node
nd.runAction(cc.sequence(action, cc.callFunc(function () {
resolve()
})))
})
}
}