Cocos2d-x3.2 ClippingNode裁减节点(模板遮罩)

前端之家收集整理的这篇文章主要介绍了Cocos2d-x3.2 ClippingNode裁减节点(模板遮罩)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//GameScene.h
#include"cocos2d.h"
USING_NS_CC;
class GameScene: public cocos2d::Layer
{
:
static cocos2d::Scene*createScene();
virtual bool init();
onTouchBegan(Touch*touch,Event*unused_event);
CREATE_FUNC(GameScene);
private : //注意不能用auto关键字
Sizesize;
Sprite*sprite;
Node*node; //模板节点
ClippingNode*clippingNode; //被裁减的节点
};



?
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//GameScene.cpp
#include"GameScene.h"
USING_NS_CC;
cocos2d::Scene*GameScene::createScene()
{
auto scene=Scene::create(); //创建一个场景
layer=GameScene::create(); //创建一个图层
scene->addChild(layer);
return scene;
}
//初始化当前的图层
GameScene::init()
{
if (!Layer::init()) //初始化父类
return false ;
//获取屏幕大小
size=Director::getInstance()->getVisibleSize();
//autosize=Director::getInstance()->getWinSize();
//加载背景
bg=Sprite::create( "OnePiece_1.png" );
@H_488_403@ bg->setPosition(Vec2(size.width/2,size.height/2));
this ->addChild(bg);
@H_301_413@
target=Sprite::create( "target.png" );
target->setPosition(Vec2(size.width/2,size.height/2));
//target->setScale(2);
node=Node::create();
clippingNode=ClippingNode::create();
clippingNode->setStencil(node); //设置模板
clippingNode->setInverted( true ); //设置底板可见
clippingNode->setAlphaThreshold(0); //设置绘制底板的Alpha值为0
->addChild(clippingNode);
clippingNode->addChild(target);
//创建监听事件对象
listener=EventListenerTouchOneByOne::create();
//定义监听事件的回调函数
listener->onTouchBegan=CC_CALLBACK_2(GameScene::onTouchBegan, );
//在事件分发器中注册
_eventDispatcher->addEventListenerWithSceneGraPHPriority(listener,monospace!important; border:0px!important; bottom:auto!important; float:none!important; height:auto!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-size:1em!important; min-height:inherit!important">);
;
}
GameScene::onTouchBegan(Touch*touch,Event*unused_event)
{
pos=touch->getLocation();
//每次点击屏幕,在点击处添加射击效果图片,该图片也需要被打穿
holeBg=Sprite::create( "hole_effect.png" );
holeBg->setPosition(pos);
clippingNode->addChild(holeBg);
//将弹孔添加到模板上,造成板底裁减洞口形状
hole=Sprite::create( "hole_stencil.png" );
hole->setPosition(pos);
node->addChild(hole);
;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//GameScene.h
#include"cocos2d.h"
USING_NS_CC;
class GameScene: public cocos2d::Layer
{
:
static cocos2d::Scene*createScene();
virtual bool init();
CREATE_FUNC(GameScene);
private : //注意不能用auto关键字
Sizesize;
Sprite*sprite;
Node*node; //模板节点
ClippingNode*clippingNode; //被裁减的节点
};



?
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//GameScene.cpp
#include"GameScene.h"
USING_NS_CC;
cocos2d::Scene*GameScene::createScene()
{
auto scene=Scene::create(); //创建一个场景
layer=GameScene::create(); //创建一个图层
scene->addChild(layer);
return scene;
}
//初始化当前的图层
GameScene::init()
{
if (!Layer::init()) //初始化父类
return false ;
//获取屏幕大小
size=Director::getInstance()->getVisibleSize();
//autosize=Director::getInstance()->getWinSize();
//加载背景
bg=Sprite::create( "OnePiece_1.png" );
@H_488_403@ this ->addChild(bg); @H_301_413@
target=Sprite::create( "target.png" );
//target->setScale(2);
node=Node::create();
clippingNode=ClippingNode::create();
clippingNode->setStencil(node); //设置模板
clippingNode->setInverted( true ); //设置底板可见
clippingNode->setAlphaThreshold(0); //设置绘制底板的Alpha值为0
->addChild(clippingNode);
clippingNode->addChild(target);
//创建监听事件对象
listener=EventListenerTouchOneByOne::create();
//定义监听事件的回调函数
);
//在事件分发器中注册
);
;
}
{
pos=touch->getLocation();
//每次点击屏幕,在点击处添加射击效果图片,该图片也需要被打穿
holeBg=Sprite::create( "hole_effect.png" );
holeBg->setPosition(pos);
clippingNode->addChild(holeBg);
//将弹孔添加到模板上,造成板底裁减洞口形状
hole=Sprite::create( "hole_stencil.png" );
hole->setPosition(pos);
node->addChild(hole);
;
}
原文链接:https://www.f2er.com/cocos2dx/344232.html

猜你在找的Cocos2d-x相关文章