cocos-enhance-kit/demo/assets/spine/skin/spine-skin.ts

73 lines
2.3 KiB
TypeScript
Raw Normal View History

2022-06-17 10:42:43 +00:00
const { ccclass, property } = cc._decorator;
@ccclass
export default class SpineSkin extends cc.Component {
@property(cc.Node)
addBoyBtn: cc.Node = null;
@property(cc.Node)
removeBoyBtn: cc.Node = null;
@property(cc.Node)
randomChangeBtn: cc.Node = null;
@property(cc.Node)
boy: cc.Node = null;
@property([cc.SpriteFrame])
heads: cc.SpriteFrame[] = [];
boys: cc.Node[] = [];
2022-07-05 10:10:58 +00:00
datas: sp.SkeletonData[] = [];
2022-06-17 10:42:43 +00:00
protected start(): void {
const boySpine = this.boy.getComponentInChildren(sp.Skeleton);
const newSkeletonData = boySpine.skeletonData.clone();
boySpine.skeletonData = newSkeletonData;
boySpine.animation = 'attack';
2022-07-05 10:10:58 +00:00
this.datas.push(newSkeletonData);
2022-06-17 10:42:43 +00:00
this.boys.push(this.boy);
this.addBoyBtn.on('click', () => {
const newBoy = cc.instantiate(this.boy);
const newBoySpine = newBoy.getComponentInChildren(sp.Skeleton);
2022-06-21 10:09:15 +00:00
newBoySpine.skeletonData = boySpine.skeletonData.clone();
newBoySpine.animation = 'attack';
2022-06-17 10:42:43 +00:00
this.boy.parent.addChild(newBoy);
newBoy.setPosition(this.boys[this.boys.length - 1].position);
newBoy.x += 100;
if (this.boys.length % 2 === 1) {
newBoy.getComponentInChildren(sp.Skeleton).setAnimationCacheMode(sp.Skeleton.AnimationCacheMode.PRIVATE_CACHE);
newBoy.getComponentInChildren(cc.Label).string = `Spine - Cache`;
}
2022-07-05 10:10:58 +00:00
this.datas.push(newBoySpine.skeletonData);
2022-06-17 10:42:43 +00:00
this.boys.push(newBoy);
});
this.removeBoyBtn.on('click', () => {
if (this.boys.length > 1) {
2022-07-05 10:10:58 +00:00
this.datas[this.datas.length - 1].destroy();
2022-06-17 10:42:43 +00:00
this.boys[this.boys.length - 1].destroy();
2022-07-05 10:10:58 +00:00
this.datas.length -= 1;
2022-06-17 10:42:43 +00:00
this.boys.length -= 1;
}
});
this.randomChangeBtn.on('click', () => {
const boy = this.boys[this.boys.length - 1].getComponentInChildren(sp.Skeleton);
2022-07-05 10:10:58 +00:00
boy.setRegionData('Head', 'Head', new sp.RegionData(this.heads[Math.floor(Math.random() * (this.heads.length))]));
2022-06-17 10:42:43 +00:00
});
}
2022-07-05 10:10:58 +00:00
protected onDestroy(): void {
for (const data of this.datas) {
data.destroy();
}
}
2022-06-17 10:42:43 +00:00
}