首页 > 代码库 > Cocos2d-x3.2 ClippingNode裁减节点(模板遮罩)
Cocos2d-x3.2 ClippingNode裁减节点(模板遮罩)
//GameScene.h #include "cocos2d.h" USING_NS_CC; class GameScene : public cocos2d::Layer { public: static cocos2d::Scene* createScene(); virtual bool init(); virtual bool onTouchBegan(Touch *touch, Event *unused_event); CREATE_FUNC(GameScene); private: //注意不能用auto关键字 Size size; Sprite *sprite; Node *node; //模板节点 ClippingNode *clippingNode; //被裁减的节点 };
//GameScene.cpp // Created by Jacedy on 14-8-11. #include "GameScene.h" USING_NS_CC; cocos2d::Scene* GameScene::createScene() { auto scene = Scene::create(); //创建一个场景 auto layer = GameScene::create(); //创建一个图层 scene->addChild(layer); return scene; } //初始化当前的图层 bool GameScene::init() { if(!Layer::init()) //初始化父类 return false; //获取屏幕大小 size = Director::getInstance()->getVisibleSize(); //auto size = Director::getInstance()->getWinSize(); //加载背景 auto bg = Sprite::create("OnePiece_1.png"); bg->setPosition(Vec2(size.width/2, size.height/2)); this->addChild(bg); auto 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 this->addChild(clippingNode); clippingNode->addChild(target); //创建监听事件对象 auto listener = EventListenerTouchOneByOne::create(); //定义监听事件的回调函数 listener->onTouchBegan = CC_CALLBACK_2(GameScene::onTouchBegan, this); //在事件分发器中注册 _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); return true; } bool GameScene::onTouchBegan(Touch *touch, Event *unused_event) { auto pos = touch->getLocation(); //每次点击屏幕,在点击处添加射击效果图片,该图片也需要被打穿 auto holeBg = Sprite::create("hole_effect.png"); holeBg->setPosition(pos); clippingNode->addChild(holeBg); //将弹孔添加到模板上,造成板底裁减洞口形状 auto hole = Sprite::create("hole_stencil.png"); hole->setPosition(pos); node->addChild(hole); return false; }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。