cocoscreator基础-(十六)自定义的帧动画播放组件(需要优化)(代码片段)

orxx orxx     2023-03-09     351

关键词:

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) 

    // ,
);

 

cocoscreator(1)——帧动画

...首先要将图片处理成带位置信息的大图形式,以便能够被cocoscreator所用。其次要创建一个承载动画的节点,并绑定Animation组件。编辑动画,包括运行速度,帧事件,运行模式(顺序、循环...)。预览。动画资源就对应于之前animat... 查看详情

自定义 UITableViewCell 中的 contentView 宽度错误?

...tSubviews,因为我需要计算几个子视图的帧。我所有计算的基础是内容视图的宽度。这就是我开始实现layoutSubviews的样子:-( 查看详情

如何使用十六进制更改自定义数字格式颜色?

】如何使用十六进制更改自定义数字格式颜色?【英文标题】:HowcanIchangecustomnumberformattingcolorsusingHex?【发布时间】:2020-03-2003:37:58【问题描述】:我想更改自定义数字格式的颜色-“%change”。这是使用标准颜色的电子表格示例... 查看详情

cocoscreator自定义事件机制

项目中一直在使用cocoscreator作为开发工具。之前使用的是cocos2d-x,用惯了notificationCenter,用了creator,发现没有这个东西,只能用js默认的on和emit用来设置事件监听和发送事件,但是,系统自带的on和emit有一... 查看详情

cocoscreator自定义事件机制

项目中一直在使用cocoscreator作为开发工具。之前使用的是cocos2d-x,用惯了notificationCenter,用了creator,发现没有这个东西,只能用js默认的on和emit用来设置事件监听和发送事件,但是,系统自带的on和emit有一... 查看详情

cocoscreator基础-(十三)cc.loader使用(代码片段)

1:掌握cc.loader加载本地资源;2:掌握cc.loader加载远程资源;3:掌握资源释放的机制与autorelease;4:掌握手动释放资源; cc.Loader 1:有三个默认的Pipeline:  (1)assetLoader:主要用于加载资源,加载asset类型资源,和释放这些资源;  (2)down... 查看详情

qt系列文章之三十六(自定义qtdesigner插件)

文章目录前言创建QtDesignerWidget插件项目插件项目各文件的功能实现MyBatteryPlugin类MyBatteryDesignerPlugin.pro的内容内置项目mybattery.pri组件类MyBattery的定义插件的编译与安装使用自定义插件使用MSVC编译器输出中文的问题运行效果图demo... 查看详情

qt系列文章之三十六(自定义qtdesigner插件)

文章目录前言创建QtDesignerWidget插件项目插件项目各文件的功能实现MyBatteryPlugin类MyBatteryDesignerPlugin.pro的内容内置项目mybattery.pri组件类MyBattery的定义插件的编译与安装使用自定义插件使用MSVC编译器输出中文的问题运行效果图demo... 查看详情

cocoscreator动作系统(代码片段)

废话没有,直接上代码,有注释可以看的,cc.Class(extends:cc.Component,properties://1player:default:null,type:cc.Node,tooltip:"thisisatips"//属性检查器内的自定义提示信息,onLoad:function()//////////使用动作系统/////////////////////////// 查看详情

Tailwind 自定义表单:如何添加自己的颜色(十六进制值)

】Tailwind自定义表单:如何添加自己的颜色(十六进制值)【英文标题】:Tailwindcustomforms:howtoaddyourowncolor(hexvalue)【发布时间】:2021-02-0222:14:33【问题描述】:这个网站:https://tailwindcss-custom-forms.netlify.app/给出了这个例子tailwind.co... 查看详情

如何使用不透明度的任何颜色的十六进制颜色制作自定义矩形形状?

】如何使用不透明度的任何颜色的十六进制颜色制作自定义矩形形状?【英文标题】:Howtomakecustomrectangleshapewithhexcolorofanycolorwithopacity?【发布时间】:2022-01-0608:01:51【问题描述】:有没有可能用任何十六进制颜色绘制一个矩形形... 查看详情

cocoscreator游戏开发基础入门

在CocosCreator游戏开发中,有几个非常重要的基础知识大家必须掌握,就是场景、场景树、节点Node、组件Component。一、什么是场景和场景树一个游戏中可以有多个场景(例如登录场景、修改密码场景、游戏主场景等等),在游戏... 查看详情

十六周四次课

十六周四次课19.12添加自定义监控项目19.13/19.14配置邮件告警19.15测试告警19.16不发邮件的问题处理19.12添加自定义监控项目添加自定义监控项目需求:监控某台web的80端口连接数,并出图两步:1)zabbix监控中心创建监控项目;2)... 查看详情

cocoscreator基础1,小白自学日常。(代码片段)

...错则改之,无则加勉。接触ccc的第一天。我首先去看了下CocosCreator用户手册(http://docs.cocos.com/creator/manual/zh/),然后跟着做了一个摘星星的小游戏。第一天就是简单的复制粘贴,简单了解了一下操作界面和运行原理。第二天我... 查看详情

cocoscreator基础-spine骨骼动画组件使用(代码片段)

cocoscreator基础-(十一)spine骨骼动画组件使用1:掌握sp.Skeleton组件的使用;spine骨骼动画工具1:骨骼动画:把动画打散,通过工具,调骨骼的运动等来形成动画2:spine是一个非常流行的2D骨骼动画制作工具3:spine动画美术人员导出3个文件:... 查看详情

自定义控件三部曲之绘图篇(十六)——给控件添加阴影效果与发光效果

前言:要么出击,要么出局,命运女神总会眷顾拼劲全力的一方相关文章:《Android自定义控件三部曲文章索引》:http://blog.csdn.net/harvic880925/article/details/50995268这节我们将学到如下内容:传统地给按钮添加阴影的方法如何给已有... 查看详情

cocoscreator3.0基础——常见操作(代码片段)

文章持续记录开发中遇到常用的功能节点平移缩放旋转@ccclass('CubeScale')exportclassCubeScaleextendsComponenti=0j=0update(deltaTime:number)//平移this.node.translate(newVec3(0.01,0,0))//缩放this.node.setScale(this 查看详情

cocoscreator3.0基础——常见操作(代码片段)

文章持续记录开发中遇到常用的功能节点平移缩放旋转基本操作@ccclass('CubeScale')exportclassCubeScaleextendsComponenti=0j=0update(deltaTime:number)//平移this.node.translate(newVec3(0.01,0,0))//缩放this.node.setScale( 查看详情