CCClippingNode详解

cocos2d-x 基于CCClippingNode实现CCLayer遮罩功能,在滑动时超出剪切区域的实现显示部分的效果,不需要再借助其他sprite来遮挡了

2012年06月16日  ⁄ 综合 ⁄ 共 1849字 ⁄ 字号  小 中 大  ⁄ 评论关闭
ClippingLayer::ClippingLayer()
{

}


ClippingLayer::~ClippingLayer()
{
}

ClippingLayer* ClippingLayer::create()
{
	ClippingLayer* LAYER = new ClippingLayer();
	if(LAYER&&LAYER->init())
	{
		LAYER->autorelease();
		return LAYER;
	}
	delete LAYER;
	return NULL;
}

bool ClippingLayer::init()
{
	setTouchEnabled(true);
	m_Node = CCNode::create();
	
	CCClippingNode *clipper = CCClippingNode::create();
	clipper->setContentSize(CCSize(80,100));
	addChild(clipper,1);
	clipper->setPosition(CCPointZero);
	clipper->addChild(m_Node,1);
	CCDrawNode *stencil = CCDrawNode::create();
	CCPoint rectangle[4];
	rectangle[0] = ccp(0, 0);
	rectangle[1] = ccp(clipper->getContentSize().width, 0);
	rectangle[2] = ccp(clipper->getContentSize().width, clipper->getContentSize().height);
	rectangle[3] = ccp(0, clipper->getContentSize().height);

	ccColor4F white = {1, 1, 1, 1};
	stencil->drawPolygon(rectangle, 4, white, 1, white);
	clipper->setStencil(stencil);

	m_Node->setPosition(CCPointZero);
	int countHeight=0;
	for(int i=0;i!=10;++i)
	{

		CCLabelTTF * label = CCLabelTTF::create("112122","Arial",14);
		m_Node->addChild(label,1);
		label->setPosition(ccp(40,countHeight * 20 + 10));
		countHeight +=1;
		m_vLabeld.push_back(label);
	}
	setContentSize(CCSize(80,countHeight*10));
	CCLayerColor *colorLayer = CCLayerColor::create(ccc4(200,0,200,100),80,100);
	addChild(colorLayer,1);
	return true;
}

void ClippingLayer::registerWithTouchDispatcher()
{
	CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,1,false);
}

	
 bool ClippingLayer::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)
 {
	 m_TouchBegin = pTouch->getLocation();
	 return true;
 }

void ClippingLayer::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent)
{
	CCPoint pos = pTouch->getLocation();
	float yMub = pos.y - m_TouchBegin.y;
	
	float endY = m_Node->getPositionY() + yMub;

	if(endY < -100)
	{	
		m_Node->setPositionY(-100);
		return;
	}
	if(endY > 100)
	{	
		m_Node->setPositionY(100);
		return;
	}
	m_Node->setPositionY(endY);

	m_TouchBegin  = pos;
}

void ClippingLayer::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent)
{}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值