Cocos2d-x3.2 Animate帧动画

简介:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//GameScene.h
 
#include "cocos2d.h"
 
class  GameScene :  public  cocos2d::Layer
{
public :
     static  cocos2d::Scene* createScene();
     
     virtual  bool  init();
     
     void  menuCallback(cocos2d::Ref* pSender);
     
     CREATE_FUNC(GameScene);
};




?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
//GameScene.cpp
 
#include "GameScene.h"
 
USING_NS_CC;
 
cocos2d::Scene* GameScene::createScene()
{
     auto  scene = Scene::create();    //创建一个场景
     auto  layer = GameScene::create();    //创建一个图层
     scene->addChild(layer);
     return  scene;
}
 
//初始化当前的图层
bool  GameScene::init()
{
     if (!Layer::init())       //初始化父类
         return  false ;
     
     //获取屏幕大小
     Size visibleSize = Director::getInstance()->getVisibleSize();
     //auto size = Director::getInstance()->getWinSize();
 
     //帧动画的创建
     //方式一,通过多张图片来创建
     auto  sprite1 = Sprite::create( "grossini_dance_05.png" );
     sprite1->setPosition(Vec2(visibleSize.width*0.3, visibleSize.height/2));
     this ->addChild(sprite1);
     
     //创建帧动画序列,名词形式
     auto  animation = Animation::create();
     for  ( int  i=5; i<11; i++)
     {
         char  szName[100] = {0};
         sprintf (szName,  "grossini_dance_%02d.png" , i);
         animation->addSpriteFrameWithFile(szName);
     }
     //设置帧动画属性
     animation->setDelayPerUnit(2.0f / 6);       //每一帧停留的时间,2秒时间完成6幅图片显示,切记要写成2.0f形式!
     animation->setRestoreOriginalFrame( true );        //播放完后回到第一帧
     
     auto  animate = Animate::create(animation);
     sprite1->runAction(RepeatForever::create(animate));
     
     
     //方式二,通过一张集合的图片来创建
     //创建2D纹理
     auto  texture = Director::getInstance()->getTextureCache()->addImage( "dragon_animation.png" );
     //建立图片帧
     auto  frame0 = SpriteFrame::createWithTexture(texture, Rect(132*0, 132*0, 132, 132));
     auto  frame1 = SpriteFrame::createWithTexture(texture, Rect(132*1, 132*0, 132, 132));
     auto  frame2 = SpriteFrame::createWithTexture(texture, Rect(132*2, 132*0, 132, 132));
     auto  frame3 = SpriteFrame::createWithTexture(texture, Rect(132*3, 132*0, 132, 132));
     auto  frame4 = SpriteFrame::createWithTexture(texture, Rect(132*0, 132*1, 132, 132));
     auto  frame5 = SpriteFrame::createWithTexture(texture, Rect(132*1, 132*1, 132, 132));
     
     auto  sprite2 = Sprite::createWithSpriteFrame(frame0);
     sprite2->setPosition(Vec2(visibleSize.width/2, visibleSize.height/2));
     this ->addChild(sprite2);
     
     //保存图片帧
     //Vector<cocos2d::AnimationFrame *> array;
     Vector<cocos2d::SpriteFrame *> array;
     array.pushBack(frame0);
     array.pushBack(frame1);
     array.pushBack(frame2);
     array.pushBack(frame3);
     array.pushBack(frame4);
     array.pushBack(frame5);
     
     auto  animation2 = Animation::createWithSpriteFrames(array, 0.2f);    //此处createWithSpriteFrames()函数确实每帧间隔时间参数,需自行加上去!!!
     sprite2->runAction(RepeatForever::create(Animate::create(animation2)));
     
     
     //方式三,通过.plist 文件来创建
     auto  cache = SpriteFrameCache::getInstance();
     cache->addSpriteFramesWithFile( "animate.plist" );
     
     auto  sprite3 = Sprite::createWithSpriteFrameName( "grossini_dance_05.png" );
     sprite3->setPosition(Vec2(visibleSize.width*0.7, visibleSize.height/2));
     this ->addChild(sprite3);
     
     Vector<cocos2d::SpriteFrame *> arr;
     char  str[100] = {0};
     for  ( int  i=1; i<15; i++)
     {
         sprintf (str,  "grossini_dance_%02d.png" , i);
         auto  frame_2 = cache->SpriteFrameCache::getInstance()->getSpriteFrameByName(str);    //3.2版本有改变
         arr.pushBack(frame_2);
     }
     
     auto  animation3 = Animation::createWithSpriteFrames(arr, 0.2f);      //此处也不要忘记加上时间间隔参数
     sprite3->runAction(RepeatForever::create(Animate::create(animation3)));
     
     return  true ;
}

备注:注意spriteWithSpriteFrames()函数缺省时间间隔参数,需自行添加!同时,不要忘了把资源文件加载到Resources文件夹下

目录
相关文章
|
12天前
|
人工智能 JSON 机器人
让龙虾成为你的“公众号分身” | 阿里云服务器玩Openclaw
本文带你零成本玩转OpenClaw:学生认证白嫖6个月阿里云服务器,手把手配置飞书机器人、接入免费/高性价比AI模型(NVIDIA/通义),并打造微信公众号“全自动分身”——实时抓热榜、AI选题拆解、一键发布草稿,5分钟完成热点→文章全流程!
11438 122
让龙虾成为你的“公众号分身” | 阿里云服务器玩Openclaw
|
2天前
|
人工智能 JSON 监控
Claude Code 源码泄露:一份价值亿元的 AI 工程公开课
我以为顶级 AI 产品的护城河是模型。读完这 51.2 万行泄露的源码,我发现自己错了。
3358 8
|
1天前
|
人工智能 数据可视化 安全
王炸组合!阿里云 OpenClaw X 飞书 CLI,开启 Agent 基建狂潮!(附带免费使用6个月服务器)
本文详解如何用阿里云Lighthouse一键部署OpenClaw,结合飞书CLI等工具,让AI真正“动手”——自动群发、生成科研日报、整理知识库。核心理念:未来软件应为AI而生,CLI即AI的“手脚”,实现高效、安全、可控的智能自动化。
1314 2
王炸组合!阿里云 OpenClaw X 飞书 CLI,开启 Agent 基建狂潮!(附带免费使用6个月服务器)
|
12天前
|
人工智能 IDE API
2026年国内 Codex 安装教程和使用教程:GPT-5.4 完整指南
Codex已进化为AI编程智能体,不仅能补全代码,更能理解项目、自动重构、执行任务。本文详解国内安装、GPT-5.4接入、cc-switch中转配置及实战开发流程,助你从零掌握“描述需求→AI实现”的新一代工程范式。(239字)
7408 139
|
2天前
|
云安全 供应链 安全
Axios投毒事件:阿里云安全复盘分析与关键防护建议
阿里云云安全中心和云防火墙第一时间响应
1141 0
|
3天前
|
人工智能 自然语言处理 数据挖掘
零基础30分钟搞定 Claude Code,这一步90%的人直接跳过了
本文直击Claude Code使用痛点,提供零基础30分钟上手指南:强调必须配置“工作上下文”(about-me.md+anti-ai-style.md)、采用Cowork/Code模式、建立标准文件结构、用提问式提示词驱动AI理解→规划→执行。附可复制模板与真实项目启动法,助你将Claude从聊天工具升级为高效执行系统。
|
2天前
|
人工智能 定位技术
Claude Code源码泄露:8大隐藏功能曝光
2026年3月,Anthropic因配置失误致Claude Code超51万行源码泄露,意外促成“被动开源”。代码中藏有8大未发布功能,揭示其向“超级智能体”演进的完整蓝图,引发AI编程领域震动。(239字)
2128 9
|
10天前
|
人工智能 并行计算 Linux
本地私有化AI助手搭建指南:Ollama+Qwen3.5-27B+OpenClaw阿里云/本地部署流程
本文提供的全流程方案,从Ollama安装、Qwen3.5-27B部署,到OpenClaw全平台安装与模型对接,再到RTX 4090专属优化,覆盖了搭建过程的每一个关键环节,所有代码命令可直接复制执行。使用过程中,建议优先使用本地模型保障隐私,按需切换云端模型补充功能,同时注重显卡温度与显存占用监控,确保系统稳定运行。
2535 9