cocos2d-x新手引导遮罩CCClippingNode裁剪区域

废话不多说,我直接封装了一个类,是个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;
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值