cocos2d-x添加遮罩【CCRenderTexture】
来源:互联网 发布:淘宝上的盗版书 编辑:程序博客网 时间:2024/05/16 04:20
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文件,在添加如下方法:
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
{
// 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文件,添加如下代码:
2
cocos2d::CCSprite *_background;
打开HelloWorldScene.cpp文件,在构造函数里添加如下代码:
修改init函数为如下:
2
3
4
5
6
7
8
9
10
11
12
{
bool bRet = false;
do
{
CC_BREAK_IF(! CCLayer::init());
bRet = true;
} while (0);
return bRet;
}
添加以下方法:
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
{
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 =newBBSprite("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();
}
- cocos2d-x添加遮罩【CCRenderTexture】
- cocos2d-x CCRenderTexture
- cocos2d-x CCRenderTexture
- 更改 cocos2d-x CCRenderTexture 的使用方式
- cocos2d-x 字体描边CCRenderTexture glBlendFunc
- cocos2d-x 字体描边CCRenderTexture glBlendFunc
- cocos2d-x节点(CCRenderTexture.h)API
- cocos2d-x 字体描边CCRenderTexture glBlendFunc
- cocos2d-x 字体描边CCRenderTexture glBlendFunc
- cocos2d-x 界面截屏CCRenderTexture
- cocos2d-x 字体描边CCRenderTexture glBlendFunc
- cocos2d-x初探学习笔记(25)--CCRenderTexture
- cocos2d-X之蒙板,局部高亮可点,CCRenderTexture
- cocos2d-X之蒙板,局部高亮可点,CCRenderTexture
- cocos2d/-x 用CCRenderTexture为一个CCLabelTTF创建阴影。
- 如何使用CCRenderTexture创建动态纹理 Cocos2d-x 2.1.4
- 如何使用CCRenderTexture创建动态纹理 cocos2d-x-3.0alpha0
- 如何使用CCRenderTexture创建动态纹理 Cocos2d-x 2.1.4
- 用JavaScript的eval获取Ajax返回的JavaScript代码
- C++习题:野人与修道士过河问题
- 选择最好的,不一定是最好的选择!
- Linux下mysql修改密码
- 关于对WinCE工程的编译命令使用
- cocos2d-x添加遮罩【CCRenderTexture】
- 鼠标右键菜单demo
- 在sae中,怎么将图片上传到storage的二级目录的代码?
- 世界上最优秀的程序员同时也是活得最快乐的人
- Nginx stick cookie curl 模块测试
- 最牛B的编码套路
- add(int index, E element) 在列表的指定位置插入元素
- ubuntu更改IP
- 懂户型图 如何选择好户型