关于Cocos2d-x中打包图集和使用方法
准备的过程
1.打开TextruePacker软件
2.把游戏中要使用的图片拖到TextruePacker里面,TextruePacker会自动帮我们排序,让所有小图变成一个大图
3.点击Publish-会输出两个文件
MyTexture.plist //里面记录了所有小图在大图中的位置和属性,cocos可以根据这些信息在MyTexture.png大图中找到所需要的小图
MyTexture.png //一张容纳了所有小图的大图
4.在GameScene.cpp的init方法里面写
//加载plist文件到一个精灵帧缓存中
SpriteFrameCache::getInstance()->addSpriteFramesWithFile("MyTexture.plist");
开始使用
1.在对象类的init方法中使用,根据小图的名字在精灵帧缓存的大图中找到小图并且赋予Star对象纹理图案,这个和setTexture("star.png");是一样的效果,但是setTexture("star.png");每次都要去原文件里面找,而initWithSpriteFrameName("star.png");是直接从缓存中取,效率更高,速度更快。初始化的时候才能用这个。
bool Star::init(){
Sprite::init();
//setTexture("star.png");
initWithSpriteFrameName("star.png");
}
2.创建精灵节点的时候使用
//创建一颗子弹
auto bullet = Sprite::createWithSpriteFrameName("bullet1.png");
3.设置精灵节点的时候使用(最常用)
myBedHero->setSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("h_1.png"));
4.在帧动画中使用,SpriteFrameCache::getInstance()是获得这个缓存精灵帧对象(类似一个大图)
//创建行走的帧动画
Animation * animation = Animation::create();
//animation->addSpriteFrameWithFile("s_1.png");
animation->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("s_1.png"));
//animation->addSpriteFrameWithFile("s_2.png");
animation->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("s_2.png"));
//animation->addSpriteFrameWithFile("s_3.png");
animation->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("s_3.png"));
//animation->addSpriteFrameWithFile("s_4.png");
animation->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("s_4.png"));
//animation->addSpriteFrameWithFile("s_5.png");
animation->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("s_5.png"));
//animation->addSpriteFrameWithFile("s_6.png");
animation->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("s_6.png"));
animation->setDelayPerUnit(0.1f);
animation->setRestoreOriginalFrame(true);
//设置动画型的动作
auto animate = Animate::create(animation);
myHero->runAction(RepeatForever::create(animate));
5.播放动画时使用,setSpriteFrame();感觉和setTextrue();差不多
//播放受伤动画
SpriteFrame *hurt = nullptr;
SpriteFrame *old = nullptr;
switch (m_planeType)
{
case Enemy2:
hurt = SpriteFrameCache::getInstance()->getSpriteFrameByName("enemy2_hit.png");
old = SpriteFrameCache::getInstance()->getSpriteFrameByName("enemy2.png");
break;
case Enemy3:
hurt = SpriteFrameCache::getInstance()->getSpriteFrameByName("enemy3_hit.png");
old = SpriteFrameCache::getInstance()->getSpriteFrameByName("enemy3_n1.png");
break;
case Enemy4:
hurt = SpriteFrameCache::getInstance()->getSpriteFrameByName("enemy3_hit.png");
old = SpriteFrameCache::getInstance()->getSpriteFrameByName("enemy3_n2.png");
break;
} auto setHurtImg = CallFunc::create([this, hurt](){
this->setSpriteFrame(hurt);
}); auto setOldImg = CallFunc::create([this, old](){
this->setSpriteFrame(old);
}); auto hurtAction = Sequence::create(setHurtImg, DelayTime::create(0.2), setOldImg, nullptr); this->stopAllActions();
this->runAction(hurtAction);
6.放进精灵帧数组中
Vector<SpriteFrame*> m_blowframes; //存放爆炸纹理精灵帧,.h文件里面定义
if (planetype == EnemyPlaneType::Enemy1)
{
m_blowframes.pushBack(SpriteFrameCache::getInstance()->getSpriteFrameByName("enemy1_down1.png"));
m_blowframes.pushBack(SpriteFrameCache::getInstance()->getSpriteFrameByName("enemy1_down2.png"));
m_blowframes.pushBack(SpriteFrameCache::getInstance()->getSpriteFrameByName("enemy1_down3.png"));
m_blowframes.pushBack(SpriteFrameCache::getInstance()->getSpriteFrameByName("enemy1_down4.png"));
}
关于Cocos2d-x中打包图集和使用方法的更多相关文章
- Unity3D - 使用TexturePacker打包图集以及NGUI对旋转sprites的支持
作者:EnigmaJJ 博客地址:http://www.cnblogs.com/twjcnblog/ 在Unity中使用NGUI时,为了减少draw call,我们会将美术用到的小图打成一张图集,如图 ...
- 如何在cocos2d项目中enable ARC
如何在cocos2d项目中enable ARC 基本思想就是不支持ARC的代码用和支持ARC的分开,通过xcode中设置编译选项,让支持和不支持ARC的代码共存. cocos2d是ios app开发中 ...
- DEDE在图集列表中调出图集的所有图片[首页也适用]
在include/common.func.php 中添加以下函数代码 代码如下: // 在图集列表中调出图集的所有图片 function Getimgs($aid, $imgwith = 220, ...
- 如何在Cocos2D游戏中实现A*寻路算法(六)
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交流 ...
- 如何在Cocos2D游戏中实现A*寻路算法(一)
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交流 ...
- InstallShield中打包ArcEnineRuntime
InstallShield中打包ArcEnineRuntime 最近研究了一阵应用程序的打包,几天下来也算颇有收获.普通的.net程序打包相对简单一点,不过ArcEngine的应用程序还涉及到Engi ...
- ionic在iOS中打包失败
在ios中打包失败,遇上这样的错误 解决办法,查看index.html的权限是否是只读状态,如果是,改成可读可写,再次打包重试,成功!
- 【Electron】在 WSL2 中 打包 electron Linux 版本
[Electron]在 WSL2 中 打包 electron Linux 版本. 安装 WSL 我使用的是 Ubuntu 20.04.4 LTS 的版本. 安装 WSL 文档地址:https://do ...
- 如何在Cocos2D游戏中实现A*寻路算法(四)
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交流 ...
随机推荐
- Linux内核(13) - 子系统的初始化之以PCI子系统为例
由Kconfig这张地图的分布来看,PCI这块儿的代码应该分布在两个地方,drivers/pci和arch/i386/pci,两岸三地都属于一个中国,不管是drivers/pci那儿的,还是arch/ ...
- struts2.xml中所有constant详解--大全
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-/ ...
- mysql查询-不存在记录时赋对应的数据
使用mysql数据库,执行查询的时候,有时候就不存在记录,但是正好在不存在记录的时候又需要给赋予相应的查询结果字段,代码实现如下: select IFNULL(('), '1970-01-01 00: ...
- Java:多线程,线程池,用Executors静态工厂生成常用线程池
一: newSingleThreadExecutor 创建一个单线程的线程池,以无界队列方式运行.这个线程池只有一个线程在工作(如果这个唯一的线程因为异常结束,那么会有一个新的线程来替代它.)此线程池 ...
- 基于Unity3d 引擎的Android游戏优化
原文地址:http://blog.csdn.net/jixuguo/article/details/9018669 近期项目进入收尾阶段,之前对项目做了非常多优化,mesh合并 .降低DrawCall ...
- 通达OA 在工作流中直接查询表单内容的开发尝试(图文)
一个朋友提出要在工作里直接查询表单内容的需求,原来他们把工作流当做业务系统来用.也算把工作流用到极致了.为了实现像软件里直接的查询功能,他想在办理工作流的时候直接能查询到表单里面的内容. 通过研究通达 ...
- WCF入门学习2-控制台做为宿主
Step1.创建一个WCF类库和一个控制台程序. Step2.引用WCF项目. Step3.添加这两个引用. //本段代码出处 无废话WCF入门教程系列 Uri baseAddress = new U ...
- 如何在 block 中修改外部变量
转自:http://www.cnblogs.com/easonoutlook/archive/2012/08/22/2650070.html block 的目的是为了支持并行编程,对于普通的 loca ...
- 解决MAC下ctags -R无效的问题
MAC下自带了ctags,与我们常用的是不同的. 我们需要去重新下载一个ctags并重新安装 1.去http://ctags.sourceforge.net/下载Ctags的最新版本源代码 2.tar ...
- 不能将参数1从“const char []”转换为“LPCTSTR
今天在使用vs2008+MFC时候,使用editControl的replacesel(“”)发生报错.如下::不能将参数1从“const char []”转换为“LPCTSTR” 其解决方案就是, 在 ...