游戏中经常会使用一些mask, 来做遮罩,实现不规则图的显示,在cocos2d-x中
函数实现如下:
.h
- static CCSprite* createMaskedSprite(CCSprite* src, const char* maskFile);
.cpp
- CCSprite* GOEUtilies::createMaskedSprite(CCSprite* src, const char* maskFile)
- {
- CCSprite * mask = CCSprite::create(maskFile);
- assert(src);
- assert(mask);
- CCSize srcContent = src->getContentSize();
- CCSize maskContent = mask->getContentSize();
- CCRenderTexture * rt = CCRenderTexture::create(srcContent.width, srcContent.height, kTexture2DPixelFormat_RGBA8888);
- float ratiow = srcContent.width / maskContent.width;
- float ratioh = srcContent.height / maskContent.height;
- mask->setScaleX(ratiow);
- mask->setScaleY(ratioh);
- mask->setPosition(ccp(srcContent.width / 2, srcContent.height / 2));
- src->setPosition(ccp(srcContent.width / 2, srcContent.height / 2));
- ccBlendFunc blendFunc2 = { GL_ONE, GL_ZERO };
- mask->setBlendFunc(blendFunc2);
- ccBlendFunc blendFunc3 = { GL_DST_ALPHA, GL_ZERO };
- src->setBlendFunc(blendFunc3);
- rt->begin();
- mask->visit();
- src->visit();
- rt->end();
- CCSprite * retval = CCSprite::createWithTexture(rt->getSprite()->getTexture());
- retval->setFlipY(true);
- return retval;
- }
使用方法为:
GOEUtilies::createMaskedSprite(sprite,"mask.png");
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。