码迷,mamicode.com
首页 > 其他好文 > 详细

通过cocos裁切图片,将大图分为小图

时间:2015-04-03 09:34:13      阅读:112      评论:0      收藏:0      [点我收藏+]

标签:图片裁切

游戏要用动画,美工还没做出来,于是,从网上找来了几张图,不过图是JavaME时代的图,动画都是靠裁切来显示的,于是想将大图分割成小图来用。

图片规格:图中每个小图的大小是相等的,基本上都是正行,整列的。

技术分享

上代码:

const char * CutNormalImage::IMAGE_FORMAT_NAME = "image_ (%d).png" ;
const char * pSaveImageFromar = "image_%d_%d_%d.png" ;
int row = 4 ;
int col = 4 ;
int begin = 1 ;
int end = 116 ;
USING_NS_CC ;
void CutNormalImage::cutImage()
{
	for (int k = begin ;k <= end ; k++)
	{
		const char * pImageName = CCString::createWithFormat(CutNormalImage::IMAGE_FORMAT_NAME,k)->getCString() ;
		CCSprite* pSprite = CCSprite::create(pImageName);
		pSprite->setAnchorPoint(ccp(0,0));

		float width = pSprite->getContentSize().width / col ;
		float height = pSprite->getContentSize().height / row ;

		for (int i = 0 ; i < row ;i++)
		{
			for (int j = 0 ;j < col ;j++)
			{
				float x = j * width ;
				float y = i * height ;
				CCSprite * pCell = CCSprite::create(pImageName,CCRectMake(x,y,width,height)) ;
				pCell->setAnchorPoint(ccp(0,0)) ;
				const char* pName = CCString::createWithFormat(pSaveImageFromar,k,i ,j)->getCString() ;
				CCRenderTexture* rrt = CCRenderTexture::create(width,height);
				rrt->clear(0,0,0,0);
				rrt->begin();
				pCell ->visit();
				rrt->end();
				CCImage *pImage = rrt->newCCImage(true);
				std::string fullpath = CCFileUtils::sharedFileUtils()->getWritablePath() + pName;
				pImage->saveToFile(fullpath.c_str(), false);
			}
		}
	}
}


通过cocos裁切图片,将大图分为小图

标签:图片裁切

原文地址:http://blog.csdn.net/c_boy_lu/article/details/44836305

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!