通過cocos裁切圖片,將大圖分為小圖
阿新 • • 發佈:2019-02-02
遊戲要用動畫,美工還沒做出來,於是,從網上找來了幾張圖,不過圖是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); } } } }