废话不多说,我直接封装了一个类,是个layer,需要的时候直接添加layer就行 //白白原创
头文件
#pragma once #include "cocos2d.h" USING_NS_CC; const int kTagBackground=0; const int kTagClipNode=1; const int kTagTip=2; class TestLayer : public CCLayer { public: CREATE_FUNC(TestLayer); virtual bool init(); virtual void registerWithTouchDispatcher(); virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent); CCClippingNode* clip; CCSprite* tip; };
#include "TestLayer.h" bool TestLayer::init() { CCSize visibleSize=CCDirector::sharedDirector()->getVisibleSize(); CCSprite* background = CCSprite::create("HelloWorld.png"); background->setPosition(ccp(visibleSize.width/2,visibleSize.height/2)); this->addChild(background,kTagBackground); clip=CCClippingNode::create(); //设置裁剪节点 clip->setInverted(TRUE); //设置ALPHA镂空的阈值, ture:裁剪区域镂空,其他区域显示底板颜色 false:裁剪区域显示底板颜色,其他区域镂空 clip->setAlphaThreshold(1000.5f); //设置ALPHA的测试参考值 //奇怪的东东 /设置alpha值(0~1),这个很重要,裁剪是按像素抠图的,所以只有大于这个alpha值的模版像素才会被画出来 //默认是1,也就是完全裁剪。 this->addChild(clip,kTagClipNode); //创建一个剪辑区域 CCLayerColor* back=CCLayerColor::create(ccc4(153,0,0,200)); clip->addChild(back);//给clip加一个颜色层,clip不受影响,其他区域有层级遮挡 // 以下模型是drawnode遮罩 // CCDrawNode* front=CCDrawNode::create(); // ccColor4F yellow = {1, 1, 0, 1}; // CCPoint rect[4]={ccp(-30,30),ccp(30,30),ccp(30,-30),ccp(-30,-30)}; // front->drawPolygon(rect, 4, yellow, 0, yellow); //绘制四边形 // front->setPosition(ccp(visibleSize.width/2, visibleSize.height/2)); // clip->setStencil(front); //一定要有,设置裁剪模板 // //以下模型是带图像遮罩 CCNode* nodef=CCNode::create(); CCSprite* close=CCSprite::create("CloseSelected.png"); nodef->addChild(close); nodef->setPosition(ccp(visibleSize.width/2,visibleSize.height/2)); clip->setStencil(nodef); //印刷,使图片被印刷出来,带图形的遮罩 tip = CCSprite::create("tip.png"); tip->setScale(0.5f); tip->setRotation(60); tip->setPosition(ccp(visibleSize.width/2-70,visibleSize.height/2+50)); this->addChild(tip,kTagTip); tip->runAction(CCRepeatForever::create(CCSequence::create(CCScaleBy::create(0.25f,0.95f),CCScaleTo::create(0.25f,0.5),NULL))); this->setTouchEnabled(true); return true; } void TestLayer::registerWithTouchDispatcher() { CCDirector *pDirector=CCDirector::sharedDirector(); pDirector->getTouchDispatcher()->addTargetedDelegate(this,0,true); } bool TestLayer::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent) { CCSize visibleSize=CCDirector::sharedDirector()->getVisibleSize(); CCPoint point=CCDirector::sharedDirector()->convertToGL(pTouch->getLocationInView()); CCRect rect=CCRectMake(visibleSize.width/2-30,visibleSize.height/2-30,60,60); if (rect.containsPoint(point)) { this->removeChild(tip); this->removeChild(clip); return true; } return false; }
cocos2d-x新手引导遮罩CCClippingNode裁剪区域,布布扣,bubuko.com
cocos2d-x新手引导遮罩CCClippingNode裁剪区域
原文:http://blog.csdn.net/u010229677/article/details/26727243