CCRenderTexture,它允许你来动态创建纹理,并且可以在游戏中重用这些纹理。
使用 CCRenderTexture非常简单 – 你只需要做以下5步就行了:
- ①.创建一个新的CCRenderTexture:指定所要创建纹理的宽度和高度。
②.调用CCRenderTexture的begin方法:设置OpenGL以便之后的任何图形绘制都在CCRenderTexture上,而不是屏幕上。
③.绘制纹理:使用原始的OpenGL命令来绘制,或通过调用现有的Cocos2D对象的visit方法。
④.调用CCRenderTexture的end方法:渲染纹理,关闭绘制到纹理上。
⑤.以纹理创建一个新的精灵:以CCRenderTexture的getSprite()->getTexture()来创建一个新的精灵。
注意这里不是调用CCRenderTexture:begin方法,而是调用一个更方便的方法beginWithClear,可以在绘制之前,用特定的颜色来清除纹理。
步骤如下:
1.新建Cocos2d-win32工程,工程名为"TinySeal",勾选"Box2D"选项(后续文章会使用到),勾选"Simple Audio Engine in Cocos Denshion"选项;
2.打开HelloWorldScene.cpp文件,在添加如下方法:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
CCSprite *HelloWorld::spriteWithColor(ccColor4F bgColor,
float textureWidth,
float textureHeight)
{ // 1: Create new CCRenderTexture CCRenderTexture *rt = CCRenderTexture::create(textureWidth, textureHeight); // 2: Call CCRenderTexture:begin rt->beginWithClear(bgColor.r, bgColor.g, bgColor.b, bgColor.a); // 3: Draw into the texture // You'll add this later // 4: Call CCRenderTexture:end rt->end(); // 5: Create a new Sprite from the texture return CCSprite::createWithTexture(rt->getSprite()->getTexture()); } |
3.接着打开HelloWorldScene.h文件,添加如下代码:
1
2 |
private:
cocos2d::CCSprite *_background; |
打开HelloWorldScene.cpp文件,在构造函数里添加如下代码:
1
|
_background =
NULL;
|
修改init函数为如下:
1
2 3 4 5 6 7 8 9 10 11 12 |
bool HelloWorld::init()
{ bool bRet = false; do { CC_BREAK_IF(! CCLayer::init()); bRet = true; } while ( 0); return bRet; } |
添加以下方法:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
ccColor4F HelloWorld::randomBrightColor()
{ while ( true) { float requiredBrightness = 192; ccColor4B randomColor = ccc4(rand() % 255, rand() % 255, rand() % 255, 255); if (randomColor.r > requiredBrightness || randomColor.g > requiredBrightness || randomColor.b > requiredBrightness) { return ccc4FFromccc4B(randomColor); } } } void HelloWorld::genBackground() { if (_background) { _background->removeFromParentAndCleanup( true); } ccColor4F bgColor = this->randomBrightColor(); _background = this->spriteWithColor(bgColor, 512, 512); CCSize winSize = CCDirector::sharedDirector()->getWinSize(); _background->setPosition(ccp(winSize.width / 2, winSize.height / 2)); this->addChild(_background, - 1); } void HelloWorld::onEnter() { CCLayer::onEnter(); this->genBackground(); this->setTouchEnabled( true); } void HelloWorld::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent) { this->genBackground(); } |
cocos2d-x添加遮罩【CCRenderTexture】
//===========Declaration Part==========================
//===========Declaration End===========================
CCSprite* HelloWorld::maskedSpriteWithSprite(CCSprite* srcSp,CCSprite* maskSp)
{
}
以上是参考http://blog.sina.com.cn/s/blog_c2beacdd0101c0oj.html和http://blog.csdn.net/akof1314/article/details/9190901在此表示感谢原文作者
以下是在做一个项目中要实现一个擦除的圆去寻找动物的场景
在.h中添加
CCRenderTexture *renderTexture;
在.cpp中
void FindMediator::init(){
Sprite_BG1 = new BBSprite(TAG_BG1,"FindAnimal/bg.png",_ccp_(FindAnimalScene.bg));
Sprite_BG1->retain();
Sprite_BG2 = new BBSprite(TAG_BG2,"FindAnimal/bg2.png",_ccp_(FindAnimalScene.bg));
getView()->addChild(Sprite_BG2,-1);
animalArray = CCArray::create();
animalArray->retain();
//增加5种动物,蝙蝠,猴子,猫,猫头鹰,青蛙
for (int i=1; i<6; i++) {
FindSprite *animals = new FindSprite(this,("FindAnimal/animals.plist@" +$str(i)+$str(".png")),i,_ccp("FindAnimalScene.animal"+$str(i)));
getView()->addChild(animals, i, TAG_ANIMAL+i);
// animals->setVisible(false);
animalArray->addObject(animals);
}
finderSprite =new BBSprite("FindAnimal/colorBg.png",_ccp_(FindAnimalScene.bg));
getView()->addChild(finderSprite);
finderSprite->setZOrder(11111);
finderSprite->setOpacity(230);
finderSprite->setVisible(false);
// finderSprite->setAnchorPoint(CCPoint(0.53, 0.46));
tempSprite = new BBSprite("FindAnimal/xiantou_quan.png",_ccp_(FindAnimalScene.temp));
finderSprite->addChild(tempSprite);
BBSharePre::setBool("isExisted", true);
this->addAnimalsAction();
this->addButton();
this->schedule(schedule_selector(FindMediator::addAnimalsAction), 5.0);
//*******************************************
renderTexture = CCRenderTexture::create(1024, 768);
renderTexture->setPosition(_ccp_(FindAnimalScene.bg));
renderTexture->getSprite()->setBlendFunc((ccBlendFunc) { GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA });
getView()->addChild(renderTexture,999);
brushSprite = new BBSprite("FindAnimal/temp.png",_ccp_(FindAnimalScene.bg));
brushSprite->retain();
brushSprite->setBlendFunc((ccBlendFunc) { GL_ZERO, GL_ONE_MINUS_SRC_ALPHA });
renderTexture->clear(0.0f, 0.0f, 0.0f, 0.0f);
renderTexture->begin();
Sprite_BG1->visit();
brushSprite->visit();
renderTexture->end();
}