转自:http://codingnow.cn/cocos2d-x/795.html

精灵是游戏中十分重要的组成部分,随处可见,如:游戏背景、NPC、人物、道具等。在cocos2d-x引擎中,只要是用图片展示的,基本上需要使用精灵类。

1. 首先来了解一下跟精灵相关的几个类:
(1) CCTexture2D
可以把它看成一个纹理,它是cocos2d-x渲染图形的重要参数,用来贴图,因为cocos2d-x使用opengl es绘制2d图形的,它的尺寸是2的n次方。一般通过以下方式获得:

CCTexture2D* cache = CCTextureCache::sharedTextureCache()->addImage("hero.png");

(2) CCSprite
这个就是精灵类,是CCNode的子类,它的内部封装了CCTexture2D(纹理),可以通过下面几种方式初始化精灵对象。

//CCTexture2D表示精灵包含的图片,范围是整张图片
static CCSprite* spriteWithTexture(CCTexture2D *pTexture);
//CCRect表示图片的指定范围,即从图片的指定矩形区域裁剪
static CCSprite* spriteWithTexture(CCTexture2D *pTexture, const CCRect& rect);
//CCSpriteFrame表示精灵的某一帧,大多数情况下精灵本身的图片有多帧。它内部封装了CCTexture2D和CCRect,可以从一个大图片取出一部分作为一帧。
static CCSprite* spriteWithSpriteFrame(CCSpriteFrame *pSpriteFrame);
//pszSpriteFrameName表示帧的名字,根据帧名从内存中取出CCSpriteFrame
static CCSprite* spriteWithSpriteFrameName(const char *pszSpriteFrameName);
//pszFileName表示本地图片文件名
static CCSprite* spriteWithFile(const char *pszFileName);
static CCSprite* spriteWithFile(const char *pszFileName, const CCRect& rect);
static CCSprite* spriteWithBatchNode(CCSpriteBatchNode *batchNode, const CCRect& rect);

下面是两种比较常用的初始化精灵的方式:

CCSprite* sprite = CCSprite::spriteWithFile("hero.png");
/** 或者 **/
CCTexture2D* cache = CCTextureCache::sharedTextureCache()->addImage("hero.png");
CCSprite* sprite = CCSprite::spriteWithTexture(cache);

(3) CCTextureCache
它相当于CCTexture2D的容器,是内存池,用来缓存CCTexture2D对象的,它内部有一个字典CCMutableDictionary m_pTextures,key为图片的名称,值是CCTexture2D。当调用它的addImage函数添加图片时,会先根据图片名称去内存中查找是否已存在,是则直接取出返回。下面是addImage部分源码:

CCTexture2D * CCTextureCache::addImage(const char * path)
{
CCTexture2D * texture = NULL;
std::string pathKey = path;
CCFileUtils::ccRemoveHDSuffixFromFile(pathKey); pathKey = CCFileUtils::fullPathFromRelativePath(pathKey.c_str());
texture = m_pTextures->objectForKey(pathKey); std::string fullpath = pathKey; // (CCFileUtils::fullPathFromRelativePath(path));
if( ! texture )
{
/** .... */
} return texture;
}

如果需要一次加载多张图片的时候,可以先把图片加载到CCTextureCache中,这样使用图片的时候速度就会很快了。
(4) CCSpriteBatchNode
它是批处理绘制精灵,主要是用来提高精灵的绘制效率的,需要绘制的精灵数量越多,效果越明显。因为cocos2d-x采用opengl es绘制图片的,opengl es绘制每个精灵都会执行:open-draw-close流程。而CCSpriteBatchNode是把多个精灵放到一个纹理上,绘制的时候直接统一绘制该texture,不需要单独绘制子节点,这样opengl es绘制的时候变成了:open-draw()-draw()…-draw()-close(),节省了多次open-close的时间。CCSpriteBatchNode内部封装了一个CCTextureAtlas(纹理图集,它内部封装了一个CCTexture2D)和一个CCArray(用来存储CCSpriteBatchNode的子节点:单个精灵)。注意:因为绘制的时候只open-close一次,所以CCSpriteBatchNode对象的所有子节点都必须和它是用同一个texture(同一张图片),类似下面这样的图片,4个贝壳都在同一纹理上:

在addChild的时候会检查子节点纹理的名称跟CCSpriteBatchNode的是不是一样,如果不一样就会出错,源码:

void CCSpriteBatchNode::addChild(CCNode *child, int zOrder, int tag)
{
/** ... */
// check CCSprite is using the same texture id
CCAssert(pSprite->getTexture()->getName() == m_pobTextureAtlas->getTexture()->getName(), ""); /** ... */
}

下面是使用CCSpriteBatchNode的使用代码示例:

CCSpriteBatchNode* BatchNode1 = CCSpriteBatchNode::batchNodeWithFile("Images/grossini_dance_atlas.png", );
addChild(BatchNode1, , kTagSpriteBatchNode); CCSpriteBatchNode* BatchNode = (CCSpriteBatchNode*) getChildByTag( kTagSpriteBatchNode );
int idx = CCRANDOM_0_1() * / ;
int x = (idx%) * ;
int y = (idx/) * ; CCSprite* sprite = CCSprite::spriteWithTexture(BatchNode->getTexture(), CCRectMake(x,y,,));
BatchNode->addChild(sprite); sprite->setPosition( ccp( p.x, p.y) );

(5) CCSpriteFrameCache
它是管理CCSpriteFrame的内存池,跟CCTextureCache功能一样,不过跟CCTextureCache不同的是,如果内存池中不存在要查找的帧,它会提示找不到,而不会去本地加载图片。它的内部封装了一个字典:CCDictionary *m_pSpriteFrames,key为帧的名称。CCSpriteFrameCache一般用来处理plist文件(这个文件指定了每个独立的精灵在这张“大图”里面的位置和大小),该文件对应一张包含多个精灵的大图,plist文件可以使用TexturePacker制作。如下图所示:

下面是使用CCSpriteFrameCache的使用代码示例:

CCSpriteFrameCache* cache = CCSpriteFrameCache::sharedSpriteFrameCache();
cache->addSpriteFramesWithFile("animations/grossini.plist", "animations/grossini.png");
m_pSprite1 = CCSprite::spriteWithSpriteFrameName("grossini_dance_01.png");
m_pSprite1->setPosition( ccp( s.width/-, s.height/) );

只要plist文件跟对应的png图片在同一目录下,且名字相同,则

  addSpriteFramesWithFile(“animations/grossini.plist”, “animations/grossini.png”)可以改成

  addSpriteFramesWithFile(“animations/grossini.plist”);

2. CCSpriteBatchNode和CCSpriteFrameCache结合使用
必须保证CCSpriteFrameCache和CCSpriteBatchNode加载的是同一纹理贴图。

CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("animations/ghosts.plist", "animations/ghosts.png");

CCSpriteBatchNode *aParent = CCSpriteBatchNode::batchNodeWithFile("animations/ghosts.png");
addChild(aParent, , kTagSprite1); CCSprite *pFather = CCSprite::spriteWithSpriteFrameName("father.gif");
pFather->setPosition(ccp( s.width/, s.height/));
aParent->addChild(pFather, , kTagSprite2);

CCSprite API

CCSprite*spret=CCSprite::spriteWithFile("Icon.png");

spret->setPosition( ccp(size.width/, size.height/) );
this->addChild(spret);
CCSprite*spret=CCSprite::spriteWithFile("Icon.png",CCRectMake(, , , ));//加载图片的一个区域
spret->setScale();//设置CCSprite的缩放比例
spret->setScaleX();//以x,y轴缩放
spret->setScaleY();
spret->setRotation();//旋转angle) 其中angle为角度不是弧度。正数为顺时针旋转,负数为逆时针旋转。
spret->setSkewX();
spret->setSkewY();//原图片坐标XY轴倾斜 (图片会拉扯)
spret->setOpacity();//范围0-255,0完全透明,255完全不透明。
spret->setIsVisible(false);//true代表可见false代表不可见
spret->setFlipX(true);//翻转
spret->autorelease();

cocos2d-x Sprite的更多相关文章

  1. ! cocos2d 同一个sprite的触控问题

    如果对一个A sprite添加触控,然后在一个场景中创建四个A的实例,那么1234逐个添加的话,只有最后一个会被点击到.其他的将不会响应.

  2. cocos基础教程(5)数据结构介绍之cocos2d::Map<K,V>

    1.概述 cocos2d::Map<K,V> 是一个内部使用了 std::unordered_map的关联容器模版. std::unordered_map 是一个存储了由key-value ...

  3. cocos2d制作动态光晕效果基础——blendFunc

    转自:http://www.2cto.com/kf/201207/144191.html 最近的项目要求动态光晕的效果. 何谓动态光晕?之前不知道别人怎么称呼这个效果, 不过在我看来,“动态光晕”这个 ...

  4. cocos2d 制作动态光晕效果基础 —— blendFunc

    转自:http://blog.csdn.net/yang3wei/article/details/7795764 最近的项目要求动态光晕的效果. 何谓动态光晕?之前不知道别人怎么称呼这个效果, 不过在 ...

  5. cocos2d中两种移动的算法

    在对cocos2d的sprite处理移动的过程中,通常用到的两种移动的算法: 假设这个CCNode是直接放在CCLayer上的 距离差法: CGPoint curTouchPosUI = [touch ...

  6. Cocos2d-x3.0模版容器具体解释之二:cocos2d::Map&lt;K,V&gt;

    1.概述: 版本号: v3.0 beta 语言: C++ 定义在 "COCOS2DX_ROOT/cocos/base" 路径下的 "CCMap.h" 的头文件里 ...

  7. 五毛的cocos2d-x学习笔记03-控件

    VS2013快捷键:注释,Ctrl+K+C:取消注释Ctrl+K+U.都是单行.要实现多行注释与取消注释,就选中多行.run方法调用了AppDelegate的applicationDidFinishL ...

  8. cocos2d-x项目101次相遇: Scenes , Director, Layers, Sprites

    cocos2d-x 101次相遇 / 文件夹  1   安装和环境搭建 -xcode  2   Scenes , Director, Layers, Sprites 3   建立图片菜单  4   在 ...

  9. iPhone Tutorials

    http://www.raywenderlich.com/tutorials This site contains a ton of fun written tutorials – so many t ...

  10. Cocos2d 中的Sprite大小调整问题

    以前用UIImageView,比如  UIImageView *view = [[UIImageViewalloc] initWithImage:[UIImageimageNamed:@"b ...

随机推荐

  1. SharePoint2013切换帐户登录菜单

    SharePoint2013帐户姓名显示的地方没有切换帐户的菜单,这个功能对于终端用户是可有可无的,但对于sharepoint管理员或sharepoint开发人员来讲,却是一个很重要的菜单,由于经常要 ...

  2. Servlet个人总结

    netstat -an ——查看端口占用情况 netstat -an ——查看是谁占用了哪个端口 端口被占用之后可以关闭端口占用程序或者在conf/server.xml修改本身使用端口 javac - ...

  3. Windows 8 自带定时关机的4种实现方法

    问题描述:前几天发布了一篇文章[ Windows 7/8 自带定时关机命令 ],文章中的用到的命令我在Windows 7都运行成功,但没有在Windows 8 上进行测试,因为我认为Windows 8 ...

  4. chrome禁用某个网站js脚本的执行

      1 首先打开谷歌浏览器.如下 2 点击右上角,打开菜单进入[设置] 3 打开后,第一个界面是没有这个的,要滚动到最后点击[显示高级设置...] 4 展开第二页后,点击[隐私设置]->[内容设 ...

  5. poj2376

    最少区间覆盖问题: 首先我们想到将r排序,则以得出dp方程 f[i]=1 (l[i]=1) =min{f[j]}+1 (r[j]+1>=l[i]) 最后ans是min{f[j]} (r[j]&g ...

  6. 通过移动的Mas接口发送短信

    1. 首先,需要移动公司提供的用户名.密码.服务ID.接口Url等信息. 2. 将短信信息整理成XML格式的字符串,再转为byte数组,通过POST的方式,将短信发往Mas接口.需要引用"M ...

  7. PASCALmath库

    noi上是让用,noip让用么?貌似不让— — 反正是好东西.在FP中,Math库为我们提供了丰富的数学函数.以下介绍在OI中可能会用到的Math库中一些函数.过程. 使用方法:在程序头用Uses语句 ...

  8. RPi 2B 中文语言包

    /************************************************************************* * RPi 2B 中文语言包 * 声明: * 本文 ...

  9. 剑指Offer:二进制中1的个数

    题目:输入一个整数,输出该数二进制表示中1的个数. // 二进制中1的个数 #include <stdio.h> int wrong_count_1_bits(int n) // 错误解法 ...

  10. 多线程程序设计学习(8)Thread-Per-Message

    Thread-Per-Message[这个工作交给你模式]一:Thread-Per-Message的参与者--->Client(委托人)--->host(中介开线程)--->hepl ...