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

前端之家收集整理的这篇文章主要介绍了通过cocos裁切图片,将大图分为小图前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

游戏要用动画,美工还没做出来,于是,从网上找来了几张图,不过图是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);
				rrt->begin();
				pCell ->visit();
				rrt->end();
				CCImage *pImage = rrt->newCCImage(true);
				std::string fullpath = CCFileUtils::sharedFileUtils()->getWritablePath() + pName;
				pImage->saveToFile(fullpath.c_str(),false);
			}
		}
	}
}
原文链接:https://www.f2er.com/cocos2dx/343635.html

猜你在找的Cocos2d-x相关文章