您的位置:首页 > 移动开发 > Cocos引擎

[Cocos2d-x][Cocos小技巧][1][触摸屏蔽]

2015-10-26 12:28 381 查看
虽然这是一个很简单的事但是还是有不少人不知道所以在这里我就提一下希望能帮助到还不知道的人

在Cocos2d-x里一些基础的Scene、Layer的关系在这里我就不说了讲要实现什么就可以了

比如说我们在玩游戏的时候遇到什么时间需要弹出对话框了比如购买道具

会弹出这么一个比如说



点击确定进入购买界面 点击取消关闭对话框

但是呢弹出对话框的时候之前的界面肯定还在

之前界面的按钮就有可能被触发要想屏蔽掉弹出对话框之前界面的触摸响应而又不影响现在的需要怎么做呢

最简单最直接的办法就是 把不想触发的按钮和别的触摸都屏蔽了呗

如果只有一两个按钮这样的方法还算好弄

如果有几百个几千个怎么办

所以就会有第二种方法既简单又好用

Cocos2d-x的Layer是可以响应屏幕触控的

包括ui::Button 等控件都是继承的Layer来完成触控操作的

所以我们需要先创建一个Layer

上代码吧

//
//  SystemDialog.h
//  TDFriends
//
//  Created by Song on 15/10/15.
//
//

#ifndef SystemDialog_h
#define SystemDialog_h

#include "Header.h"
#include "ui/CocosGUI.h"

using namespace ui;

class SystemDialog:public Layer{
public:
CREATE_FUNC(SystemDialog);
virtual bool init();

enum ButtonType{
SURE = 1,
CANCEL = 2
};

static SystemDialog *createWithSprite(std::string name);
static SystemDialog *createWithText(std::string text);

void addClickCallBack(const Widget::ccWidgetClickCallback &callback);
private:
void loadDialog();
};

#endif /* SystemDialog_h */
//
//  SystemDialog.cpp
//  TDFriends
//
//  Created by Song on 15/10/15.
//
//

#include "SystemDialog.h"
#include "cocostudio/Cocostudio.h"

bool SystemDialog::init(){
if (!Layer::init()) {
return false;
}

this->setLocalZOrder(10000);

auto bg = LayerColor::create(Color4B(0,0,0,170), WINSIZE.width, WINSIZE.height);
bg->setTag(8888);
this->addChild(bg);

auto listener = EventListenerTouchOneByOne::create();
listener->setSwallowTouches(true);
listener->onTouchBegan = [](Touch *t,Event *e){
return true;
};
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this);

return true;
}

void SystemDialog::loadDialog(){
auto node = CSLoader::createNode("Dialog.csb");
node->setTag(1);
node->setAnchorPoint(Vec2(0.5, 0.5));
node->setPosition(WINSIZE/2);
this->addChild(node);

auto showAni = CSLoader::createTimeline("Dialog.csb");
node->runAction(showAni);
showAni->gotoFrameAndPlay(0,false);
}

void SystemDialog::addClickCallBack(const Widget::ccWidgetClickCallback &callback){
auto node = this->getChildByTag(1);
auto sure = (Button *)node->getChildByTag(1)->getChildByTag(ButtonType::SURE);
auto cancel = (Button *)node->getChildByTag(1)->getChildByTag(ButtonType::CANCEL);
sure->addClickEventListener(callback);
cancel->addClickEventListener(callback);
}

SystemDialog *SystemDialog::createWithSprite(std::string name){
auto sd = SystemDialog::create();

sd->loadDialog();

auto bg = sd->getChildByTag(1)->getChildByTag(1);

auto text = Sprite::createWithSpriteFrameName(name);
text->setPosition(bg->getContentSize()/2);
bg->addChild(text);

return sd;
}

SystemDialog *SystemDialog::createWithText(std::string text){
auto sd = SystemDialog::create();

sd->loadDialog();

auto bg = sd->getChildByTag(1)->getChildByTag(1);

auto textL = Label::createWithSystemFont(text, "", 32);
textL->setPosition(bg->getContentSize()/2);
bg->addChild(textL);

return sd;
}


其实最重要的就是
listener->setSwallowTouches(true);

这句话 只要在当前Layer添加触摸监听的时候把SwallowTouches设为True

这个Layer层级以下的触摸都被屏蔽掉了

你只需要把这个Layer层级设的很高就可以屏蔽掉其他触摸

在这个Layer中添加你需要的对话框就不会影响其他操作了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: