Cocos实例4:使用Spine动画的渲染模式以及更改颜色的案例

使用Spine动画的渲染模式以及更改颜色的案例

关于Spine动画的渲染模式和颜色属性描述可参考:CocosCtreator知识点5:2D渲染组件Spine Skeleton详解

源码:

import { _decorator, Color, Component, Label, Node, sp } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('Test')
export class Test extends Component {
    @property({ type: Label })
    runMode: Label = null!;
    @property({ type: Label })
    tip: Label = null!;
    start() {

    }

    onChangeModel(){
        const skelComp = this.node.getComponent(sp.Skeleton);
        if (skelComp!.isAnimationCached()) {
            skelComp!.setAnimationCacheMode(sp.AnimationCacheMode.REALTIME);
            this.runMode.string = 'model:realtime';
            try {
                let ep = skelComp!.setAnimation(0,"walk",false);
                skelComp!.setTrackCompleteListener(ep,()=>{
                    console.log('walk event complete');
                    this.tip.string = 'walk event complete';
                })
            } catch (error) {
                console.log(error);
                this.tip.string = error;
            }
        } else {
            skelComp!.setAnimationCacheMode(sp.AnimationCacheMode.PRIVATE_CACHE);
            this.runMode.string = 'model:cache';

            try {
                let ep = skelComp!.setAnimation(0,"walk",false);
                skelComp!.setTrackCompleteListener(ep,()=>{
                    console.log('walk event complete');
                    this.tip.string = 'walk event complete';
                })
            } catch (error) {
                console.log(error);
                this.tip.string = error;
            }
        }
        
    }
    private colorCount = 0;
    onChangeColor(){
        const skelComp = this.node.getComponent(sp.Skeleton);
        if (this.colorCount === 0) {
            skelComp!.color = new Color(255, 255, 0, 255);
        } else if (this.colorCount === 1) {
            skelComp!.color = new Color(0, 0, 255, 150);
        } else {
            skelComp!.color = new Color(179, 245, 170, 255);
        }
        this.colorCount++;
        this.colorCount = this.colorCount % 3;
    }
    update(deltaTime: number) {
        
    }
}


效果:
在这里插入图片描述
在这里插入图片描述

  • 6
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值