@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