cocos2d-x3.1.1 step by step 学习笔记6 Sprite精灵类

来源:互联网 发布:企业网络管理培训 编辑:程序博客网 时间:2024/06/10 01:38

#include <CCSprite.h>

Inheritance diagram for Sprite:
 
static Sprite * create () Creates an empty sprite without texture. More... static Sprite * create (const std::string &filename) Creates a sprite with an image filename. More... static Sprite * create (const std::string &filename, constRect &rect) Creates a sprite with an image filename and a rect. More... static Sprite * createWithTexture (Texture2D *texture) Creates a sprite with a Texture2D object. More... static Sprite * createWithTexture (Texture2D *texture, const Rect &rect, bool rotated=false) Creates a sprite with a texture and a rect. More... static Sprite * createWithSpriteFrame (SpriteFrame *spriteFrame) Creates a sprite with an sprite frame. More... static Sprite * createWithSpriteFrameName (const std::string &spriteFrameName) Creates a sprite with an sprite frame name. More...
在Cocos2d-x中对图片的封装是通过CCImage来完成的,该类实现了对于不同类型图片文件的读取、解析、像素信息保存。由于在引擎内部的封装,
一般情况下不需要修改类,由于Cocos2d-x使用了OpenGL的图形库,因此在图片显示在屏幕的过程中是通过在3D空间的某个平面通过纹理贴图的方式
完成的图片显示,在Cocos2d-x中通过Textuere2D来实现。
处理纹理常用的类有:
Image //调用libpng linjpg来读取图片像素信息
Texture2d//封装纹理对象
TextureCache//一组纹理对象,通过Dictionary管理,通过字符key读取
RenderTexture//创建动态纹理
Sprite//实现纹理显示
SpriteFrame//SpriteBatchNode包含纹理的部分块对象,作为动画的每一帧
SpriteBatchNode//批处理节点纹理
SPriteFrameCache//一组纹理对象,通过Dictionary来管理,通过字符key获取
Animation//实现一组SPrite播放的Action
 
1:根据图片文件创建SPrite
在Cocos2d-x中通过图片文件来创建一个Sprite图层之后,整个图片大小就会作为图层的大小:
Sprite * sprite = Sprite::create("Image/123.png");
还可以:
auto tree = Sprite::create("tree1.png","Rect(604,38.485,548)"); //Rect:原点坐标+宽+高
 
2:根据Texture2D纹理对象创建Sprite:
有点时候,为了让图片资源更小,会根据一张图片创建不同的Sprite,这样需要首先通过TextureCache加载图片到图片纹理的缓冲
,然后从缓冲获取这张图片的Texture2D对象,根据这个对象来创建Sprite:
static Sprite* createWithTexture(Texture2D * texture)  

Creates a sprite with a Texture2D object.

 

Texture2D * texture;

texture = Director::getinstance()->getTextureCache()->addImage("tree1.png");

auto sprite = Sprite::createWithTexture(texture);

 

3:精灵的性能优化:

3.1:使用纹理图集:

纹理图集(Texture Atlas )也称为 精灵表(Sprite Sheet):

优点:

减少文件读取次数,读取一张图片要比一堆小文件要快

减少OpenGl ES绘制调用并且加速渲染

减少内存消耗,OpenGL ES 1.1 仅仅能够使用2的n次幂的大小图片(即宽度或者高度是2 4 8.。。64)

如果采用小图片,即使没这么大也会分配大于它实际的空间,运用纹理集减少了内存碎片

全面支持Zwoptex和TxeturePacker,所以创建和使用纹理是非常方便的

 

缓冲:

纹理缓冲TextureCache,使用纹理缓冲可以创建纹理对象

精灵帧缓冲SpriteFrameCache。能够从精灵变中创建精灵缓冲,然后再从精灵缓冲中获得精灵对象,反复使用精灵对象的时候,

使用精灵帧缓冲可以节省内存;

动画缓冲(AnimationCache)。 动画缓冲主要用于精灵动画,精灵动画中的每一帧是从动画缓冲中获取的。

SpriteFrameCache *frameCache = SpriteFrameCache::getInstance();
 frameCache->addSpriteFramesWithFile("SpirteSheet.plist");  //创建精灵表

    auto mountain1 = Sprite::createWithSpriteFrameName("mountain1.png"); //使用精灵名字创建sprite
 mountain1->setAnchorPoint(Vec2::ZERO);
    mountain1->setPosition(Vec2(-200,80));
    this->addChild(mountain1,0);

 SpriteFrame *heroSpriteFrame = frameCache->getSpriteFrameByName("hero1.png");
 Sprite *hero1 = Sprite::createWithSpriteFrame(heroSpriteFrame);//使用精灵对象来创建sprite
    hero1->setPosition(Vec2(800,200));
    this->addChild(hero1,0);

 

 

 
0 0
原创粉丝点击