加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 20:46:34 所属栏目:百科 来源:网络整理
导读:游戏要用动画,美工还没做出来,于是,从网上找来了几张图,不过图是JavaME时代的图,动画都是靠裁切来显示的,于是想将大图分割成小图来用。 图片规格:图中每个小图的大小是相等的,基本上都是正行,整列的。 上代码: const char * CutNormalImage::IMAGE

游戏要用动画,美工还没做出来,于是,从网上找来了几张图,不过图是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);
			}
		}
	}
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读