CocosCreator 帧动画 组件脚本

在节点上加上这个脚本,然后将每阵的spriteFrame拖入即可

cc.Class({
    extends: cc.Component,

    properties: {
        frames:{
            type: cc.SpriteFrame,
            default:[]
        },

        duration: 0.1,
        isloop: false,
        play_onload: false,
    },

    onLoad () {
        this.sprite = this.getComponent(cc.Sprite);
        if(!this.sprite){
            this.addComponent(cc.Sprite);
        }
    },

    start () {
        this.no_frame = false;
        if (this.frames.length <= 0) {
            this.no_frame = true;
            return;
        }

        this.sprite.spriteFrame = this.frames[0]; 

        this.playing = false;
        this.end_func = null;
        this.total_time = 0;

        if(this.play_onload)
        {
            if (this.isloop !== true){
                this.play_once();
            }
            else{
                this.play_loop();
            }
        }
    },

    play_once(end_func){
        this.playing = true;
        this.end_func = end_func;
    },

    play_loop(){
        this.isloop = true;
        this.playing = true;
    },

    stop_anim(){
        this.isloop = false;
        this.playing = false;
        this.total_time = 0;
    },

    update (dt) { // 改变每一帧的显示
        if (this.no_frame === true || this.playing !== true){ 
            return;
        }
        
        this.total_time += dt;
        var index = Math.floor(this.total_time / this.duration);

        if (this.isloop !== true){ 
            if (index >= this.frames.length){ // 播放完了
                this.sprite.spriteFrame = this.frames[index - 1]; 
                this.total_time = 0;
                this.playing = false;
                return;
            }

            this.sprite.spriteFrame = this.frames[index]; 
        }
        else{
            if (index >= this.frames.length){ // 播放完了
                index = index - 1; 
                this.total_time = 0;
            }
         
            this.sprite.spriteFrame = this.frames[index]; 
        }
    },
});

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值