准备的过程

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中打包图集和使用方法的更多相关文章

  1. Unity3D - 使用TexturePacker打包图集以及NGUI对旋转sprites的支持

    作者:EnigmaJJ 博客地址:http://www.cnblogs.com/twjcnblog/ 在Unity中使用NGUI时,为了减少draw call,我们会将美术用到的小图打成一张图集,如图 ...

  2. 如何在cocos2d项目中enable ARC

    如何在cocos2d项目中enable ARC 基本思想就是不支持ARC的代码用和支持ARC的分开,通过xcode中设置编译选项,让支持和不支持ARC的代码共存. cocos2d是ios app开发中 ...

  3. DEDE在图集列表中调出图集的所有图片[首页也适用]

    在include/common.func.php 中添加以下函数代码 代码如下:   // 在图集列表中调出图集的所有图片 function Getimgs($aid, $imgwith = 220, ...

  4. 如何在Cocos2D游戏中实现A*寻路算法(六)

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交流 ...

  5. 如何在Cocos2D游戏中实现A*寻路算法(一)

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交流 ...

  6. InstallShield中打包ArcEnineRuntime

    InstallShield中打包ArcEnineRuntime 最近研究了一阵应用程序的打包,几天下来也算颇有收获.普通的.net程序打包相对简单一点,不过ArcEngine的应用程序还涉及到Engi ...

  7. ionic在iOS中打包失败

    在ios中打包失败,遇上这样的错误 解决办法,查看index.html的权限是否是只读状态,如果是,改成可读可写,再次打包重试,成功!

  8. 【Electron】在 WSL2 中 打包 electron Linux 版本

    [Electron]在 WSL2 中 打包 electron Linux 版本. 安装 WSL 我使用的是 Ubuntu 20.04.4 LTS 的版本. 安装 WSL 文档地址:https://do ...

  9. 如何在Cocos2D游戏中实现A*寻路算法(四)

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交流 ...

随机推荐

  1. ArcMap导入数据到ArcSDE报000597或者000224的错误

    这两天碰到不同用户提出的不同的问题,可是分析之后发现导致该问题的解决办法是同一个原因. -------------------------------------------------------- ...

  2. 通向码农的道路(enet开源翻译计划 二)

    QQ 324186207群 enet交流技术,主要是为了研究tcp内部执行机制.欢迎大家增加探讨.小弟水平有限,翻译难免有误. . http://enet.bespin.org 解析enet 双向链表 ...

  3. mac与windows上部署使用Redis

    windows下Redis安装 在Redis的官网下载页上有各种各样的版本,由于redis官网不支持windows,但是我们伟大的windows家族还是召唤了一群小伙伴开发了win版的redis.要在 ...

  4. Redis(二):Redis的九大应用场景

    毫无疑问,Redis开创了一种新的数据存储思路,使用Redis,我们不用在面对功能单调的数据库时,把精力放在如何把大象放进冰箱这样的问题上,而是利用Redis灵活多变的数据结构和数据操作,为不同的大象 ...

  5. Excel中不常用的一些公式用法

    INDIRECT函数 http://baike.baidu.com/view/3222185.htm 用于使用单元格内容拼凑公式的情况. 1.采用  [工作表名]!单元格名  的形式读取内容: 2.所 ...

  6. ELK的索引的坑——Kibana的图形化(Tile Map)

    如果想通过ELK展示地图, 需要将索引名称修改为:logstash*的格式 否则location字段不会修改成geo_point的形式. 详情参考:http://blog.csdn.net/yangg ...

  7. Hadoop副本数配置

    一个文件,上传到hdfs上时指定的是几个副本就是几个.修改了副本数(dfs.replications),对已经上传了的文件也不会起作用.当然可以在上传文件的同时指定创建的副本数hadoop dfs - ...

  8. 在WEB开发的时候导入各种jar包

    使用eclipse导入很简单 右击你的project,选择properties,然后选择java build path,接着选择libraries,点击add external jars即可 如果你还 ...

  9. LeetCode:Decode Ways 解题报告

    Decode WaysA message containing letters from A-Z is being encoded to numbers using the following map ...

  10. LeetCode: Spiral Matrix 解题报告

    Spiral MatrixGiven a matrix of m x n elements (m rows, n columns), return all elements of the matrix ...