关于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 ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交流 ...
随机推荐
- Spring Bean的生命周期详解(转)
Spring作为当前Java最流行.最强大的轻量级框架,受到了程序员的热烈欢迎.准确的了解Spring Bean的生命周期是非常必要的.我们通常使用ApplicationContext作为Spring ...
- 使用.net的跟踪诊断来记录wcf消息
首先在项目的config文件中定义以下结点: <system.diagnostics> <sources> <source name="System.Servi ...
- Hypothesis Testing
Hypothesis Testing What's Hypothesis Testing(假设检验) Hypothesis testing is the statistical assessment ...
- 公共的Json操作C#类
using System; using System.Data; using System.Text; using System.Collections.Generic; using System.R ...
- 移动开发UI库
参考链接:http://www.cnblogs.com/edobnet/archive/2012/08/17/2643573.html 自己总结: jquery 的移动开发UI库 http://jq ...
- 服务器搭建1 安装mysql数据库
一,安装mysql-service (1)检查系统中是否已经安装mysql 在终端里面输入 sudo netstat -tap | grep mysql 若没有反映,没有显示已安装结果,则没有安装.若 ...
- C++ 11 std::function std::bind使用
cocos new 出新的项目之后,仔细阅读代码,才发现了一句3.0区别于2.0的代码: auto closeItem = MenuItemImage::create( "CloseNorm ...
- appium安卓自动化的 常用driver方法封装
appium安卓自动化的 常用driver方法封装 做安卓自动化的时候,很多方法写起来会造成代码冗余,把这部分封装起来 ,添加到androidUI工具类里,随时可调用 都放在这个类下面: @Compo ...
- 获取Django中model字段名 字段的verbose_name
obj._meta.fields 为关键 obj为model类 推荐使用函数 from django.apps import apps def getmodelfield(appname,modeln ...
- Redis Keys 命令 - 查找所有符合给定模式( pattern)的 key
Redis Keys 命令用于查找所有符合给定模式 pattern 的 key .. 语法 redis KEYS 命令基本语法如下: redis 127.0.0.1:6379> KEYS PAT ...