ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

cocos creator基础-(十六)自定义的帧动画播放组件

2019-05-07 15:53:37  阅读:389  来源: 互联网

标签:动画 cocos sprite 自定义 creator default play 播放 optional


1: 掌握帧动画的原理;

2: 完成帧动画组件的编写;

3: 代码中使用帧动画组件;

 

通过拖拽图片进行播放,比引擎的制作方式方便,但动画不是很灵活

 

帧动画播放组件

 

1: creator播放帧动画需要通过动画编辑器去制作;

2: 为了方便控制和使用加入帧动画代码播放组件;

3: 属性设置:

  sprite_frames: 帧动画所用到的所有的帧;

  duration: 每帧的时间间隔;

  loop: 是否循环播放;

  play_onload: 是否加载组件的时候播放;

4: 接口设置:

  play_once(end_func); // 播放结束后的回掉函数;

  play_loop(); // 循环播放;


 

帧动画播放原理

 

1: 对的时间播放显示对的图片:

假设以三帧动画为例,时间间隔就是duration

 

组件实现代码

cc.Class({
    extends: cc.Component,

    
    properties: {
        // foo: {
        //    default: null,      // The default value will be used only when the component attaching
        //                           to a node for the first time
        //    url: cc.Texture2D,  // optional, default is typeof default
        //    serializable: true, // optional, default is true
        //    visible: true,      // optional, default is true
        //    displayName: 'Foo', // optional
        //    readonly: false,    // optional, default is false
        // },
        // ...
        // 帧动画的图片, 多张图片, 
        sprite_frames: {
            default: [],
            type: cc.SpriteFrame, 
        },

        duration: 0.1, // 帧的时间间隔

        loop: false, // 是否循环播放;

        play_onload: false, // 是否在加载的时候就开始播放;
    },

    // use this for initialization
    onl oad: function () {
        this.end_func = null;
        this.is_playing = false; // 加一个变量
        this.play_time = 0; // 播放的时间

        // 获得了精灵组件
        this.sprite = this.getComponent(cc.Sprite);
        if (!this.sprite) {
            this.sprite = this.addComponent(cc.Sprite);
        }
        // end

        if (this.play_onload) { // 如果在加载的时候开始播放
            if (this.loop) { // 循环播放
                this.play_loop();
            }
            else { // 播放一次
                this.play_once(null);
            }
        } 
    },

    play_loop: function() {
        if (this.sprite_frames.length <= 0) {
            return;
        }

        this.loop = true;
        this.end_func = null;

        this.is_playing = true; // 正在播放
        this.play_time = 0; // 播放的时间

        this.sprite.spriteFrame = this.sprite_frames[0];
    },

    // 需要播放结束以后的回掉, end_func
    play_once: function(end_func) {
        if (this.sprite_frames.length <= 0) {
            return;
        }
        
        this.end_func = end_func;
        this.loop = false;
        this.is_playing = true; // 正在播放
        this.play_time = 0; // 播放的时间

        this.sprite.spriteFrame = this.sprite_frames[0];
    },

    // called every frame, uncomment this function to activate update callback
    // 每次游戏刷新的时候
    update: function (dt) {
        if(!this.is_playing) {
            return;
        }

        this.play_time += dt; // 当前我们过去了这么多时间;
        var index = Math.floor(this.play_time / this.duration);

        // 非循环播放
        if (!this.loop) {
            if (index >= this.sprite_frames.length)  { // 如果超过了,播放结束
                this.is_playing = false;
                if (this.end_func) {
                    this.end_func();
                }
            }
            else {
                this.sprite.spriteFrame = this.sprite_frames[index]; // 修改当前时刻显示的正确图片;
            }
        }
        else { // 循环播放
            while(index >= this.sprite_frames.length) {
                index -= this.sprite_frames.length;
                this.play_time -= (this.sprite_frames.length * this.duration);
            }
            this.sprite.spriteFrame = this.sprite_frames[index];
        }
    },

});

使用组件的测试代码

需要播放动画的节点挂载组件脚本,设置好资源后,play

var frame_anim = require("frame_anim");

cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //    default: null,      // The default value will be used only when the component attaching
        //                           to a node for the first time
        //    url: cc.Texture2D,  // optional, default is typeof default
        //    serializable: true, // optional, default is true
        //    visible: true,      // optional, default is true
        //    displayName: 'Foo', // optional
        //    readonly: false,    // optional, default is false
        // },
        // ...

        anim: {
            type: frame_anim,
            default: null,
        } 
    },

    // use this for initialization
    onl oad: function () {

    },

    start: function() {
        
        /*this.anim.play_once(function() {
            console.log("anim end called");
        });*/
        this.anim.duration = 1;
        this.anim.play_loop();
    },
    // called every frame, uncomment this function to activate update callback
    // update: function (dt) {

    // },
});

 

标签:动画,cocos,sprite,自定义,creator,default,play,播放,optional
来源: https://blog.csdn.net/u010742414/article/details/89921540

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有