消消方块阵换皮表情
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
3.5 KiB

1 week ago
//************************
// Create By 流云
// Time: 2021.01.18
// Desc: 帧动画组件
//************************
import { Component, director, game, Sprite, SpriteFrame, _decorator } from "cc";
const { ccclass, property, executeInEditMode } = _decorator;
/**
*
* 使
* 1.
* 2.Frames数组中
* 3.PlayOnload为true
* 4.Duration为每帧的时间0.1
* 5.IsAllsyn为true
* 6.Loop为true
* 7.play方法来播放动画stop方法来停止动画
* 8.Frames数组来动态改变帧动画
*/
@ccclass
@executeInEditMode
export default class ZClips extends Component {
@property(SpriteFrame)
public Frames: SpriteFrame[] = [];
@property
public PlayOnload = false;
@property
public Duration = 0.1;
@property
private IsAllsyn = false;//保持同步
/*10
10play10IsAllsyn设置
true10ZClips都会计算得到同一个
Frames的索引10
*/
@property
private Loop = false;
@property
private clear = false;
private _sp: Sprite;
private _playing = false;
private _total_time = 0;
private _default_sp = null;
onLoad() {
this._sp = this.node.getComponent(Sprite);
if (!this._sp)
this._sp = this.node.addComponent(Sprite);
else {
this._default_sp = this._sp.spriteFrame;
}
if (this.PlayOnload) {
this.play(this.Loop, this.Duration, this.IsAllsyn);
}
}
protected onEnable(): void {
this.play(this.Loop, this.Duration, this.IsAllsyn);
}
update(dt) {
let self = this;
if (self._playing && self.Frames.length > 0) {
self._total_time += dt;
let index = Math.floor(self._total_time / self.Duration) % self.Frames.length;
if (self.IsAllsyn)
index = Math.floor(game.totalTime / 1000 / self.Duration) % self.Frames.length;
if (self.Loop || index < self.Frames.length) {
self._sp.spriteFrame = self.Frames[index];
}
if (index >= self.Frames.length - 1 && !self.Loop)
self.stop();
}
}
/**
*
* @param loop 使
* @param duration
* @param isAllsyn
* @returns
*/
play(loop = null, duration = null, isAllsyn = null) {
if (this._playing) return;
this._total_time = 0;
this._playing = true;
if (isAllsyn != null) this.IsAllsyn = isAllsyn;
if (loop != null) this.Loop = loop;
if (duration != null) this.Duration = duration;
}
stop() {
this._playing = false;
if (this._default_sp) {
this._sp.spriteFrame = this._default_sp;
return;
}
if (this.clear) this._sp.spriteFrame = null;
}
}