关于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 ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交流 ...
随机推荐
- android 屏幕适配问题
转自http://blog.sina.com.cn/s/blog_74c22b210100tn3o.html 如何将一个应用程序适配在不同的手机上,虽然这不算是一个技术问题,但是对于刚刚做屏幕的开发人 ...
- yield与send实现协程操作
yield与send实现协程操作 之前我们说过,在函数内部含有yield语句即称为生成器. 下面,我们来看看在函数内部含有yield语句达到的效果.首先,我们来看看以下代码: def foo(): w ...
- git配置gitignore
一.背景 ...
- tp表单的提交与验证
一.控制器如下 引用use app\index\model\User; //注意模型类名不能和控制器类名相同 public function index(){ return $this->fet ...
- 找到当前mysql group replication 环境的primary结点
一.起原: mysql group replication 有两种模式.第一种是single primary 也就是说单个primary .这个模式下只有这一个主可以写入: 第二种是multi pri ...
- maven的部署安装
首先上传apache-maven-3.3.9-bin.tar.gz tar -xfvz apache-maven-3.3.9-bin.tar.gz mv apache-maven-3.3.9 /dat ...
- 同一个界面内取微信的OPENID和调用微信的分享接口
步骤如下,1:判断URL是否有CODE参数传入,没有则拼接那个微信跳转连接,然后redirect2:有CODE传入,调用微信接口,根据code获取openid和access_token,注意这一步取到 ...
- android.content.res.TypedArray-深入理解android自定义属性(AttributeSet,TypedArray)
属性 自定义属性,首先要定义出来属性,我们新建一个attrs.xml: <?xml version="1.0" encoding="utf-8"?> ...
- Ubuntu下如何解压缩zip,tar,tar.gz,tar.bz2文件
转自:http://wangli-5665.diandian.com/post/2011-08-18/4039228 这么多年来,数据压缩对我们来说是非常有用的.无论是在邮件中发送的图片用的zip文件 ...
- rman备份,恢复
ackup database format 'd:\tt\%U'; rman> restore database;rman> recover database; ============= ...