cocos2dx 3.x 裁剪节点 ClippingNode

效果1:

wKioL1Q2K1zyVlF5AABvxwSUhrM302.gif

效果2:

wKiom1Q2s2vhdFf5AAjqUzp_E3c902.gif

//
//[1].背景图片
Sprite* bg = Sprite::create(“HelloWorld.png”);
bg->setPosition(visibleSize / 2);
this->addChild(bg, -1);

//[2].创建主题文字 : gameTitle
Sprite* gameTitle = Sprite::create(“game_title.png”);

//获取尺寸大小
Size clipSize = gameTitle->getContentSize();

//[3].创建底板的发光图片 : spark
Sprite* spark = Sprite::create(“spark.png”);
spark->setPosition(-clipSize.width, 0);

//[4].创建裁剪节点 : clippingNode
ClippingNode* clippingNode = ClippingNode::create();
clippingNode->setPosition(visibleSize / 2);
this->addChild(clippingNode);

clippingNode->setAlphaThreshold(0.05f); //设置alpha闸值
clippingNode->setContentSize(clipSize); //设置尺寸大小

clippingNode->setStencil(gameTitle); //设置模板stencil
clippingNode->addChild(gameTitle, 1); //先添加标题,会完全显示出来,因为跟模板一样大小
clippingNode->addChild(spark,2); //会被裁减

//[5].左右移动spark
MoveTo* moveAction = MoveTo::create(2.0f, Vec2(clipSize.width, 0));
MoveTo* moveBackAction = MoveTo::create(2.0f, Vec2(-clipSize.width, 0));
spark->runAction(RepeatForever::create(Sequence::create(moveAction, moveBackAction, NULL)));

发表评论

电子邮件地址不会被公开。 必填项已用*标注