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)
{}
cocos2d-x 基于CCClippingNode实现CCLayer遮罩功能,在滑动时超出剪切区域的实现显示部分的效果,不需要再借助其他sprite来遮挡了
2012年06月16日
⁄ 综合 ⁄ 共 1849字 ⁄ 字号
小 中 大
⁄ 评论关闭