void NestedTest::setup() { static int depth = 9; CCNode *parent = this; for (int i = 0; i < depth; i++) { int size = 225 - i * (225 / (depth * 2)); CCClippingNode *clipper = CCClippingNode::create(); clipper->setContentSize(CCSizeMake(size, size)); clipper->setAnchorPoint(ccp(0.5, 0.5)); clipper->setPosition( ccp(parent->getContentSize().width / 2, parent->getContentSize().height / 2) ); clipper->setAlphaThreshold(0.05f); clipper->runAction(CCRepeatForever::create(CCRotateBy::create(i % 3 ? 1.33 : 1.66, i % 2 ? 90 : -90))); parent->addChild(clipper); CCNode *stencil = CCSprite::create(s_pPathGrossini); stencil->setScale( 2.5 - (i * (2.5 / depth)) ); stencil->setAnchorPoint( ccp(0.5, 0.5) ); stencil->setPosition( ccp(clipper->getContentSize().width / 2, clipper->getContentSize().height / 2) ); stencil->setVisible(false); stencil->runAction(CCSequence::createWithTwoActions(CCDelayTime::create(i), CCShow::create())); clipper->setStencil(stencil); clipper->addChild(stencil); parent = clipper; } }
void ScrollViewDemo::setup() { CCClippingNode *clipper = CCClippingNode::create(); clipper->setTag( kTagClipperNode ); clipper->setContentSize( CCSizeMake(200, 200) ); clipper->setAnchorPoint( ccp(0.5, 0.5) ); clipper->setPosition( ccp(this->getContentSize().width / 2, this->getContentSize().height / 2) ); clipper->runAction(CCRepeatForever::create(CCRotateBy::create(1, 45))); this->addChild(clipper); 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); CCSprite *content = CCSprite::create(s_back2); content->setTag( kTagContentNode ); content->setAnchorPoint( ccp(0.5, 0.5) ); content->setPosition( ccp(clipper->getContentSize().width / 2, clipper->getContentSize().height / 2) ); clipper->addChild(content); m_bScrolling = false; this->setTouchEnabled(true); }
bool ImageEditLayer::init(){ if (DialogLayer::init()) { this->setTitle("头像编辑"); CCPoint middle=ccp(286, 154); CCSize size=CCSizeMake(570,300); CCClippingNode* clippingNode = CCClippingNode::create(); //设置裁剪区域大小 clippingNode->setContentSize(size); clippingNode->setAnchorPoint(ccp(0.5, 0.5)); clippingNode->setPosition(middle); m_contentLayer->addChild(clippingNode); CCTexture2D* textrue=new CCTexture2D(); textrue->autorelease(); textrue->initWithImage(_image); _sprite=CCSprite::createWithTexture(textrue); _initPoint=ccp(clippingNode->getContentSize().width/2, clippingNode->getContentSize().height/2); _sprite->setPosition(_initPoint); CCSize spriteSize=_sprite->getContentSize(); if (size.width/spriteSize.width<size.height/spriteSize.height) { _scale=size.width/spriteSize.width; }else{ _scale=size.height/spriteSize.height; } _sprite->setScale(_scale); clippingNode->addChild(_sprite); //创建裁剪模板,裁剪节点将按照这个模板来裁剪区域 CCDrawNode *stencil = CCDrawNode::create(); CCPoint rectangle[4]; rectangle[0] = ccp(0, 0); rectangle[1] = ccp(clippingNode->getContentSize().width, 0); rectangle[2] = ccp(clippingNode->getContentSize().width, clippingNode->getContentSize().height); rectangle[3] = ccp(0, clippingNode->getContentSize().height); ccColor4F white = {1, 1, 1, 1}; //画一个多边形 这画一个200x200的矩形作为模板 stencil->drawPolygon(rectangle, 4, white, 1, white); clippingNode->setStencil(stencil); //用来设置显示裁剪区域还是非裁剪区域的 clippingNode->setInverted(false);//在裁剪区域内显示加入的内容 _clip=CCClippingNode::create();//创建裁剪节点,成员变量 _clip->setInverted(true);//设置底板可见 _clip->setAlphaThreshold(0.0f);//设置alpha为0 m_contentLayer->addChild(_clip);//添加裁剪节点 _mask=CCLayerColor::create(ccc4(0,0,0,160),size.width,size.height); CCPoint point=ccp(middle.x-size.width/2,middle.y-size.height/2); _mask->setPosition(point); _clip->addChild(_mask);//为裁剪节点添加一个黑色带透明(看起了是灰色)的底板 _stencil=CCSprite::create("default_avatar.png");//使用头像原图作为模板 _stencil->setPosition(middle); _stencil->setScale(STENCIL_SCALE); _clip->setStencil(_stencil);//设置模版 CCSprite* queding=CCSprite::createWithSpriteFrameName("touxiang_queding.png"); CCMenuItemSprite* item=CCMenuItemSprite::create(queding, queding, this, menu_selector(ImageEditLayer::menuCallback)); item->setPosition(ccp(290,-30));//注意contentlayer坐标原点位置 item->setScale(0.7); item->setTag(kTagConfirm); this->addMenuItem(item,true); CCSprite* fanhui=CCSprite::createWithSpriteFrameName("touxiang_fanhui.png"); fanhui->setScale(0.87); CCMenuItemSprite* fanhuiItem=CCMenuItemSprite::create(fanhui, fanhui, this, menu_selector(ImageEditLayer::menuCallback)); fanhuiItem->setPosition(ccp(19,368)); fanhuiItem->setTag(kTagBack); this->addMenuItem(fanhuiItem,true); CCPoint contentPoint=m_contentLayer->getPosition(); _rect=CCRectMake(contentPoint.x+point.x, contentPoint.y+point.y, size.width,size.height); return true; } return false; }