cocos2dx 使用Texturepacker制作纹理相册以及使用

TexturePacker官网下载地址:http://www.codeandweb.com/texturepacker/download

首先要解释一下,为什么要使用TexturePacker?

这是应为我们做的游戏最终要运行在 Android手机或者苹果手机上,而Android或者ios 系统使用的是OpenGL ES来渲染。所以我们要针对OpenGL ES来进行优化。
内存方面,OpenGL ES纹理要求宽和高都是2的n次幂的倍数。想一想,如果图片的宽为33,而高为65,那么图片加载到内存后的大小为多少?考虑到宽和高都是2的n次幂,所以加载到内存后的大小是64*128。所以我们可以考虑将小的图片拼成到的图片,然后加载。
渲染速度方面,OpenGL ES要求切换的纹理少,所以将图片拼成大图片,这样就减少了纹理的切换。
所以使用TexturePacker是很有必要的。.
安装完成之后接下来使用:
Step1:打开TexturePacker,点击Add Folder,将图片全部加载进来。注意:我们应该事先将要拼凑的图片放到同一个文件夹下。或者点击Add Sprites自定义添加
这样。TexturePacker就自动将我们要拼凑的图片拼成了一个大图,并且大图的宽和高都是2的n次幂的倍数。
Step2:下面设置输出格式:
cocos2dx 使用Texturepacker制作纹理相册以及使用_第1张图片
Texture format设置成PNG。然后选择Data file和Texture file的保存位置。点击工具栏上的Publis。这样我们就得到了plist文件和png文件。【免费版生成plist和png时候会随机在png里面添加水印】
Step3:代码中使用生成的plist 和 png 
  <1>将这两个文件复制到Resources文件夹中
  <2>使用下面的代码加载着两个文件
  
CCSpriteFrameCache* cache = CCSpriteFrameCache::sharedSpriteFrameCache();  
    cache->addSpriteFramesWithFile("zcmain.plist", "zcmain.png");  
    ///* 创建Sprite */  
    CCSprite* s1 = CCSprite::createWithSpriteFrame(cache->spriteFrameByName("cangying.png"));
         s1->setPosition(ccp(80,80));  
         this->addChild(s1,0); 
    CCSprite* s2 = CCSprite::createWithSpriteFrame(cache->spriteFrameByName("kitty_2.png"));
	s2->setPosition(ccp(size.width-50, size.height-50));  
        this->addChild(s2,0); 
   CCSprite* s3 = CCSprite::createWithSpriteFrame(cache->spriteFrameByName("kitty_3.png"));  
	s3->setPosition(ccp(50, size.height-50));  
        this->addChild(s3,0); 
  CCSprite* s4 = CCSprite::createWithSpriteFrame(cache->spriteFrameByName("guaiwu_11.png")); 
	s4->setPosition(ccp(size.width-80, 80));  
        this->addChild(s4,0); 
  CCSprite* s5 = CCSprite::createWithSpriteFrame(cache->spriteFrameByName("guaiwu_9.png")); 
	s5->setPosition(ccp(size.width/2, size.height/2));  
        this->addChild(s5,0); 


效果
cocos2dx 使用Texturepacker制作纹理相册以及使用_第2张图片

你可能感兴趣的:(cocos2d-x,TexturePacker)