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

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
    onLoad: 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
    onLoad: 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) {

    // },
});

原文地址:https://www.cnblogs.com/orxx/p/10453565.html

时间: 2024-08-25 08:50:01

cocos creator基础-(十六)自定义的帧动画播放组件(需要优化)的相关文章

Bootstrap&lt;基础十六&gt; 导航元素

Bootstrap 提供的用于定义导航元素的一些选项.它们使用相同的标记和基类 .nav.Bootstrap 也提供了一个用于共享标记和状态的帮助器类.改变修饰的 class,可以在不同的样式间进行切换. 表格导航或标签 创建一个标签式的导航菜单: 以一个带有 class .nav 的无序列表开始. 添加 class .nav-tabs. 下面的实例演示了这点: <!DOCTYPE html> <html> <head> <title>Bootstrap 实

cocos creator基础-(六)cc.Sprite使用

1: 了解cc.Sprite的使用;2: 了解cc.Sprite的大小模式;3: 学会使用九宫格,使用九宫格节省美术资源;4: 学会个性化的时间精度条的使用;5: 代码里面来更换图片; cc.Sprite 1: 游戏中显示一个图片,通常我们把这个叫做”精灵” sprite2: cocos creator如果需要显示一个图片,那么需要在节点上挂一个精灵组件,为这个组件指定要显示的图片(SpriteFrame)3: 显示一个图片的步骤: (1) 创建一个节点; (2) 添加一个组件; (3) 要显示

cocos creator 基础1,小白自学日常。

<!--作为一个初学者,难免有遗漏和不妥之处,还请不吝赐教,不胜感激.错则改之,无则加勉. 接触ccc的第一天.我首先去看了下Cocos Creator 用户手册(http://docs.cocos.com/creator/manual/zh/),然后跟着做了一个摘星星的小游戏. 第一天就是简单的复制粘贴,简单了解了一下操作界面和运行原理.第二天我找了下前辈们的教程,找了一个基础加实战的教程(http://forum.cocos.com/t/topic/40659),这里对作者表示感谢. 作为小

cocos creator基础-(二十三)android环境搭建、h5/android 打包发布

1: 了解h5打包发布,要注意的事项; 2: 完成android环境配置与creator 项目android打包; 3: 使用 eclips打开项目或android-studio; h5打包发布 1:引擎模块裁剪,减少引擎体积; 2: resources目录的特性,减少setting.js体积; 3: 订制启动的logo,与样式; 4: resources特性 a:creator里面会根据场景的依赖来打包的我们的资源, 资源没有用的,将不会被打包进来; b:有时候我们需要代码加载我们的资源, c

【Android的从零单排开发日记】之入门篇(十六)——Android的动画效果

      什么是动画,动画的本质是通过连续不断地显示若干图像来产生“动”起来的效果.比如说一个移动的动画,就是在一定的时间段内,以恰当的速率(起码要12帧/秒以上,才会让人产生动起来的错觉)每隔若干时间在屏幕上更新一次位置.游戏中的动画效果也是由此而来.同样还有其他属性变更所引起的动画效果,从数学的角度来看,包括:(1)平移(2)旋转(3)缩放(4)透明度.当然这些属性可以组合起来使用,来达到更绚丽的画面.但是不论什么样的组合方式,我们都可以统一用Matirx运算来实现,从技术实现的角度来讲,

cocos creator基础-(二十四)cc.Director与资源加载策略

1: 了解creator场景切换; 2: 了解director基本的一些接口; 3: 理解资源加载的策略; cc.Director对象 1:游戏里面控制管理整个游戏全局对象,包括了场景切换等,为cc.Director对象; 2:导演对象全局只有一个cc.director,大写的为类, 小写的cc.director为全局的导演对象; 3: cc.director来获取导演对象实例; 4: 游戏中各种管理对象都可以通过cc.director获取,比如物理引擎管理,Action管理, 碰撞检测管理等;

cocos creator基础-(二十)物理引擎碰撞检测

1: 理解物体类型和分类,配置碰撞矩阵;2: 编写碰撞响应函数,监听碰撞事件;3: 学会了解Sensor来做触发器,只触发碰撞不改变运动; 物体类型与碰撞矩阵 1: 添加物体类型: Add Layer, 每个类型对应一个名字group与groupIndex2: 创建物体的时候要选择一个类型;3: 配置碰撞矩阵,决定哪些物体类型碰撞; 碰撞事件监听 1: 刚体组件开启碰撞监听;2: 当有碰撞发生的时候,遍历刚体所在的节点所挂的所有的组件,看组件是否实现了碰撞检测函数,如果是,那么调用;3: 在需要

cocos creator基础-(二十五)FileUtils本地文件读写

1: 掌握jsb.fileUtils的基本使用; FileUtils API   CCFileUtils.h 1:jsb是javascript bind的代表,整个C/C++ 导出的绑定都在这个jsb里面,jsb 支持native,不支持h5(浏览器上无法运行jsb is not defined ); 2: FileUtils是本地文件读写的一个工具类,全局只有一个实例; 3: jsb.fileUtils来获取文件读写工具类的实例; 4: jsb.fileUtils.isDirectoryExi

cocos creator基础-(二十九)动画编辑器编辑地图路径

思路 1.利用动画编辑器,设置一个路径,多个路径就编辑多个动画 2.用特定的代码对动画进行处理,获取到路径坐标,大佬已经写好代码,不用自己重复造轮子了(微元法求曲线长度) 获得动画路径的贝塞尔曲线方程 求得每一段贝塞尔曲线的长度 每隔一小段打一个点 最终生成一个路径 3.编写寻路脚本,挂载到物体上,让沿着路径移动 动画编辑 脚本挂载 // gen_map_path.js 动画路径转换坐标的代码 已经升级到2.x cc.Class({ extends: cc.Component, properti