关于CocosCreator里面导入spine的使用流程

在CocosCreator中运行效果如下图所示:

准备流程:

spine中信息列表:


导出文件:


拖入对应骨骼,然后添加脚本:


脚本如下:

    cc.Class({
        extends: cc.Component,
        editor: {
            requireComponent: sp.Skeleton
        },
     
        properties: {
            mixTime: 0.2
        },
     
        onLoad() {
            var spine = this.spine = this.getComponent('sp.Skeleton');
            this._setMix('walk', 'run');//动画混合播放,走和跑平滑过渡,不显的太僵硬
            this._setMix('run', 'jump');
            this._setMix('walk', 'jump');
     
            // .setStartListener()用来设置开始播放动画的事件监听。
            spine.setStartListener(trackEntry => {
                var animationName = trackEntry.animation ? trackEntry.animation.name : "";
                cc.log("[track %s][animation %s] start.", trackEntry.trackIndex, animationName);
            });
            // .setInterruptListener()用来设置动画被打断的事件监听。
            spine.setInterruptListener(trackEntry => {
                var animationName = trackEntry.animation ? trackEntry.animation.name : "";
                cc.log("[track %s][animation %s] interrupt.", trackEntry.trackIndex, animationName);
            });
            // .setEndListener()用来设置动画播放完后的事件监听。
            spine.setEndListener(trackEntry => {
                var animationName = trackEntry.animation ? trackEntry.animation.name : "";
                cc.log("[track %s][animation %s] end.", trackEntry.trackIndex, animationName);
            });
            // .setDisposeListener()用来设置动画将被销毁的事件监听。
            spine.setDisposeListener(trackEntry => {
                var animationName = trackEntry.animation ? trackEntry.animation.name : "";
                cc.log("[track %s][animation %s] will be disposed.", trackEntry.trackIndex, animationName);
            });
            // .setCompleteListener用来设置动画播放一次循环结束后的事件监听。
            spine.setCompleteListener((trackEntry, loopCount) => {
                var animationName = trackEntry.animation ? trackEntry.animation.name : "";
                if (animationName === 'attack') { //如果当前为射击状态,立马清除
                    // .clearTrack(对应状态数字)清除出指定 track 的动画状态。
                    this.spine.clearTrack(1);
                }
                cc.log("[track %s][animation %s] complete: %s", trackEntry.trackIndex, animationName, loopCount);
            });
            // .setEventListener()用来设置动画播放过程中帧事件的监听。
            spine.setEventListener((trackEntry, event) => {
                var animationName = trackEntry.animation ? trackEntry.animation.name : "";
                cc.log("[track %s][animation %s] event: %s, %s, %s, %s", trackEntry.trackIndex, animationName, event.data.name, event.intValue, event.floatValue, event.stringValue);
            });
     
            this._hasStop = false;
     
            // var self = this;
            // cc.eventManager.addListener({
            //     event: cc.EventListener.TOUCH_ALL_AT_ONCE,
            //     onTouchesBegan () {
            //         self.toggleTimeScale();
            //     }
            // }, this.node);
        },
     
        // OPTIONS
     
        toggleDebugSlots() {
            // .debugSlots是否显示 slot 的 debug 信息。
            this.spine.debugSlots = !this.spine.debugSlots;
        },
     
        toggleDebugBones() {
            // .debugBones是否显示 bone 的 debug 信息。
            this.spine.debugBones = !this.spine.debugBones;
        },
     
        toggleTimeScale() {
            // .timeScale当前骨骼中所有动画的时间缩放率。
            if (this.spine.timeScale === 1.0) {
                this.spine.timeScale = 0.3;
            } else {
                this.spine.timeScale = 1.0;
            }
        },
     
        // ANIMATIONS
     
        stop() {
            // .clearTrack(对应状态数字)清除出指定 track 的动画状态。
            this.spine.clearTrack(0);
            this._hasStop = true;
        },
     
        walk() {
            // .setAnimation(trackIndex,name,loop)设置当前动画。队列中的任何的动画将被清除。返回一个 sp.spine.TrackEntry 对象。
            this.spine.setAnimation(0, 'walk', true);
            this._hasStop = false;
        },
     
        run() {
            // .setAnimation(trackIndex,name,loop)设置当前动画。队列中的任何的动画将被清除。返回一个 sp.spine.TrackEntry 对象。
            this.spine.setAnimation(0, 'run', true);
            this._hasStop = false;
        },
     
        jump() {
            // .animation当前播放的动画名称。
            var oldAnim = this.spine.animation;
            // .setAnimation(trackIndex,name,loop)设置当前动画。队列中的任何的动画将被清除。返回一个 sp.spine.TrackEntry 对象。
            this.spine.setAnimation(0, 'jump', false);
            if (oldAnim && !this._hasStop) {
                this.spine.addAnimation(0, oldAnim === 'run' ? 'run' : 'walk', true, 0);
            }
        },
     
        crouch() {
            // .setAnimation(trackIndex,name,loop)设置当前动画。队列中的任何的动画将被清除。返回一个 sp.spine.TrackEntry 对象。
            this.spine.setAnimation(0, 'crouch', true);
            this._hasStop = false;
        },
     
        head_turn() {
            // .setAnimation(trackIndex,name,loop)设置当前动画。队列中的任何的动画将被清除。返回一个 sp.spine.TrackEntry 对象。
            this.spine.setAnimation(0, 'head-turn', true);
            this._hasStop = false;
        },
     
        fall() {
            // .setAnimation(trackIndex,name,loop)设置当前动画。队列中的任何的动画将被清除。返回一个 sp.spine.TrackEntry 对象。
            this.spine.setAnimation(0, 'fall', true);
            this._hasStop = false;
        },
     
        idle() {
            // .setAnimation(trackIndex,name,loop)设置当前动画。队列中的任何的动画将被清除。返回一个 sp.spine.TrackEntry 对象。
            this.spine.setAnimation(0, 'idle', true);
            this._hasStop = false;
        },
     
        // crouch() {
        //     // .animation当前播放的动画名称。
        //     var oldAnim = this.spine.animation;
        //     // .setAnimation(trackIndex,name,loop)设置当前动画。队列中的任何的动画将被清除。返回一个 sp.spine.TrackEntry 对象。
        //     this.spine.setAnimation(0, 'crouch', false);
        //     if (oldAnim && !this._hasStop) {
        //         this.spine.addAnimation(0, oldAnim === 'run' ? 'run' : 'walk', true, 0);
        //     }
        // },
     
        attack() {
            // .setAnimation(trackIndex,name,loop)设置当前动画。队列中的任何的动画将被清除。返回一个 sp.spine.TrackEntry 对象。
            this.spine.setAnimation(1, 'attack', false);
        },
     
        _setMix(anim1, anim2) {
            // .setMix()为所有关键帧设定混合及混合时间(从当前值开始差值)。
            this.spine.setMix(anim1, anim2, this.mixTime);
            this.spine.setMix(anim2, anim1, this.mixTime);
        }
    });
————————————————
版权声明:本文为CSDN博主「MaximilianLiu」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/MaximilianLiu/article/details/79356242

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值