Quantcast
Channel: Cocos中文社区 - 最新主题
Viewing all articles
Browse latest Browse all 88737

cocos2dx 中给按钮绑定了一个触摸监听,结果不论点击哪,都能触发回调函数

$
0
0

@ch7flyer 写道:

我想给一个button控件绑定一个触摸监听,结果整个屏幕都能触发回调函数。应该怎么绑定才能只有点击按钮时才能用。看官方文档里,它对比了点击处的坐标和按钮的坐标能解决,有其他方便的方法吗?
下面附上代码:

auto UI = CSLoader::createNode("MainScene.csb");
this->addChild(UI);
auto outputBtn = dynamic_cast<cocos2d::ui::Button*>(UI->getChildByName("Button_1"));
this->addChild(outputBtn);
CCLOG("the scene is created succesful!!");
auto listener = EventListenerTouchOneByOne::create();
listener->setEnabled(true);
listener->setSwallowTouches(true);
listener->onTouchBegan = [](Touch* touch, Event* event) {
	return true;
};
listener->onTouchEnded = [&](Touch* touch,Event* event){
	auto self = new TowerPosEditorLayer();
	self->outputPosOnClick();
	CCLOG("the callback function is worked successful!!");
};
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener,outputBtn);

帖子: 2

参与者: 1

阅读整个主题


Viewing all articles
Browse latest Browse all 88737

Trending Articles