1
2
3
4
5
6
7
|
//
//创建,不含模板(stencil)
ClippingNode*clippingNode=ClippingNode::create();
//创建,使用模板(stencil)
ClippingNode*clippingNode=ClippingNode::create(stencil);
//
|
7
8
9
10
11
12
13
14
15
/**
* 用来做裁剪的模板(stencil)节点(Node)
* 模板(stencil)对象,默认为空(nullptr)
**/
Node*stencil=Node::create();
//模板stencil节点Node
stencil->addChild(spriteBall1);
//添加小球1
stencil->addChild(spriteBall2);
//添加小球2
stencil->addChild(spriteBall3);
//添加小球3
stencil->addChild(spriteBall4);
//添加小球4
stencil->addChild(spriteBall5);
//添加小球5
clippingNode->setStencil(stencil);
//设置模板Stencil
3.3、设置底板(Content)
4
clippingNode->addChild(content);
//设置底板
3.4、倒置显示(Inverted)
5
clippingNode->setInverted(
false
);
//裁剪节点
Node*holesstencil;
//模板节点
Node*holes;
//底板节点
//触摸回调
//添加小洞
pokeHoleAtPoint(Vec2point);
1.3、在HelloWorld.cpp中的init()中创建裁剪节点ClippingNode
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
Sprite*bg=Sprite::create(
"HelloWorld.png"
);
bg->setPosition(visibleSize/2);
this
->addChild(bg);
//[2].创建裁剪节点:holesClipper
holesClipper=ClippingNode::create();
holesClipper->setPosition(visibleSize/2);
->addChild(holesClipper);
//属性设置
holesClipper->setAlphaThreshold(0.5f);
//设置alpha透明度闸值
holesClipper->runAction(RepeatForever::create(RotateBy::create(1,45)));
//旋转动作
//[3].创建模板:holesstencil
holesstencil=Node::create();
holesClipper->setStencil(holesstencil);
//设置模板节点
//[4].创建底板:holes
holes=Node::create();
holesClipper->addChild(holes);
//设置底板
Sprite*content=Sprite::create(
"blocks.png"
);
holesClipper->addChild(content,-1,
"content"
);
//[5].触摸事件
auto
listener=EventListenerTouchAllAtOnce::create();
_eventdispatcher->addEventListenerWithSceneGraPHPriority(listener,monospace!important; font-size:1em!important; min-height:inherit!important; background:none!important">);
1.4、设置触摸事件回调。当触摸点在底板区域内部,则“打洞”
21
HelloWorld::ontouchesBegan(
ottom:auto!important; float:none!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,Event*unused_event)
{
//[1].获取触点,转换为相对holesClipper节点的相对坐标
Vec2point=touches[0]->getLocation();
point=holesClipper->convertToNodeSpace(point);
//[2].获取底板区域矩形Rect
Sprite*content=(Sprite*)holesClipper->getChildByName(
);
SizecontentSize=content->getContentSize();
Rectrect=Rect(-contentSize.width/2,-contentSize.height/2,contentSize.width,contentSize.height);
//[3].触摸点在底板内部,进行"打洞"
if
(rect.containsPoint(point))
{
pokeHoleAtPoint(point);
}
}
1.5、实现“打洞”操作函数
21
{
hole=Sprite::create(
"hole_effect.png"
);
hole->setPosition(point);
holes->addChild(hole);
holeStencil=Sprite::create(
"hole_stencil.png"
);
holeStencil->setPosition(point);
holesstencil->addChild(holeStencil);
//[3].动作效果:放大缩小
holesClipper->runAction(Sequence::create(Scaleto::create(0.05f,1.05f),Scaleto::create(0.05f,1.0f),NULL));
1.6、分析与总结
37
->addChild(bg,-1);
Sprite*gameTitle=Sprite::create(
"game_title.png"
);
//获取尺寸大小
SizeclipSize=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);
->addChild(clippingNode);
clippingNode->setAlphaThreshold(0.05f);
//设置alpha闸值
clippingNode->setContentSize(clipSize);
//设置尺寸大小
clippingNode->setStencil(gameTitle);
//设置模板stencil
clippingNode->addChild(spark,2);
//会被裁减
//[5].左右移动spark
spark->runAction(RepeatForever::create(Sequence::create(moveAction,moveBackAction,NULL)));
2.3、分析与总结
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。