Cocos Creator 3.x之Spine和Animation
  RlWeLU85QNwT 2023年11月02日 32 0

一, 前言

Cocos Creator 3.7.3

二,Spine

import {Component, Node, sp, isValid, v3} from 'cc';

export class SpineView extends Component {
    private spineAnim: sp.Skeleton;
    private readonly EVENT_NAME: Array<string> = ["dice1", "dice2", "dice3", "dice4", "dice5", "dice6"];
    private readonly BONE_NAME: Array<string> = ["dice1", "dice2", "dice3", "dice4", "dice5", "dice6"];
    private readonly OPEN_EVENT: string = "open";
    private readonly ANIM_NAME: string = "animation";

    private boneArr: sp.spine.Bone[];
    private images: Node[];

    protected onLoad(): void {
        this.spineAnim = this.node.getChildByName("Spine").getComponent(sp.Skeleton);
        for (let i: number = 0; i < this.EVENT_NAME.length; i++) {
            this.node.getChildByName(`${i}`).active = false;
        }
    }

    protected onEnable(): void {
        this.listener(true);
    }

    protected onDisable(): void {
        this.listener(false);
    }

    private listener(isAdd: boolean): void {
        if (!isValid(this.node)) return;
        if (isAdd) {
            this.spineAnim.setCompleteListener(this.onAnimCompleteHandler.bind(this));
            this.spineAnim.setEventListener(this.onEventHandler.bind(this));
        } else {
            this.spineAnim.setCompleteListener(null);
            this.spineAnim.setEventListener(null);
        }
    }

    private onAnimCompleteHandler(): void {
        if (!isValid(this.node)) return;
        console.log(`Spine播放完毕!`);
    }

    private onEventHandler(trackEntry: sp.spine.TrackEntry, event: sp.spine.Event): void {
        if (!isValid(this.node)) return;
        if (!event.data) return;
        let index: number = this.EVENT_NAME.indexOf(event.data.name); //查找到触发的event
        if (index < 0) {
            if (event.data.name == this.OPEN_EVENT) {
                console.log("spine open");
            }
        } else {
            this.images[index] = this.node.getChildByName(`${index}`);
        }
    }

    /**
     * 播放Spine动画
     */
    public play(): void {
        this.setBoneArr();
        this.images = [];
        this.spineAnim.timeScale = 1;
        let track = this.spineAnim.setAnimation(0, this.ANIM_NAME, false);
        track.trackTime = track.animationStart;
    }

    private setBoneArr(): void {
        this.boneArr = [];
        for (let i: number = 0; i < this.BONE_NAME.length; i++) {
            this.boneArr[i] = this.spineAnim.findBone(this.BONE_NAME[i]);
        }
    }

    private updateDice(diceOne: Node, bone: sp.spine.Bone): void {
        diceOne.setPosition(v3(bone.worldX, bone.worldY, 0));
        diceOne.setScale(bone.getWorldScaleX(), bone.getWorldScaleY());
    }

    protected update(dt: number): void {
        if (this.images == null || this.images.length == 0) return;
        for (let i: number = 0; i < this.BONE_NAME.length; i++) {
            if (!this.images[i]) continue;
            if (!this.images[i].active) {
                this.images[i].active = true;
            }
            this.updateDice(this.images[i], this.boneArr[i]);
        }
    }

    protected onDestroy(): void {
        this.boneArr = null;
        this.images = null;
    }
}

三,Animation

import {Component, Animation, AnimationState, isValid} from 'cc';

export class AnimView extends Component {
    private anim: Animation;
    private animState: AnimationState;

    protected onLoad(): void {
        this.anim = this.node.getChildByName("Anim").getComponent(Animation);
    }


    protected onEnable(): void {
        this.listener(true);
    }

    protected onDisable(): void {
        this.listener(false);
    }

    private listener(isAdd: boolean): void {
        if (!isValid(this.node)) return;
        if (isAdd) {
            this.anim.on(Animation.EventType.FINISHED, this.onAnimComplete, this);
        } else {
            this.anim.off(Animation.EventType.FINISHED, this.onAnimComplete, this);
        }
    }

    private onAnimComplete(event: string, date: AnimationState): void {
        if (!isValid(this.node)) return;
        switch (date.name) {
            case "anim":
                console.log(`Anim 播放完毕!`);
                this.animState = this.anim.getState("anim");
                if (this.animState) {
                    console.log(`complete ${this.animState.isPlaying}`);
                }
                break;
        }
    }

    public play(): void {

        this.scheduleOnce(() => {
            if (!isValid(this.node)) return;
            console.log(`Anim 开始播放!!!!!!`);
            this.anim.play("anim");
        }, 6);
    }


    protected onDestroy(): void {
        this.anim = null;
        this.animState = null;
    }
}
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

RlWeLU85QNwT