CCMenuItemLabel 在 isTouchEnabled 为 true 时不起作用?
CCMenuItemLabel doesn't work when isTouchEnabled is true?
我正在做我的Cocos2d-x cpp项目。我已经成功添加触摸事件以移动图层中的背景。现在我想将 CCMenuItemLabel 添加到图层,但我发现 CCMenuItemLabel 在我触摸它时不起作用。我该如何解决?
我在我的图层中添加了这些功能:
virtual void ccTouchesBegan (CCSet *pTouches, CCEvent *pEvent);
virtual void ccTouchesMoved (CCSet *pTouches, CCEvent *pEvent);
virtual void ccTouchesEnded (CCSet *pTouches, CCEvent *pEvent);
在 MyLayer::init() 函数中:
this->setTouchEnabled(true);
CCLabelTTF* test = CCLabelTTF::create("tesetdd","Arial",40);
CCMenuItemLabel* menuLabel = CCMenuItemLabel::create(test,this,menu_selector(GameWall::menuCall));
menuLabel->setPosition(ccp(winSize.width/2,winSize.height/2));
this->addChild(menuLabel,1);
更新:I已将 CCMenuItemLabel 放入 CCMenu。但它仍然不起作用。
CCLabelTTF* test = CCLabelTTF::create("tesetdd","Arial",40);
CCMenuItemLabel* menuLabel = CCMenuItemLabel::create(test,this,menu_selector(GameWall::menuCall));
menuLabel->setPosition(ccp(winSize.width/2,winSize.height/2));
CCMenu* menu = CCMenu::create(menuLabel,NULL);
menu->setPosition(CCPointZero);
this->addChild(menu,1);
不要将 CCMenuItems 直接添加到图层。将它们添加到 CCMenu 并将该 CCMenu 添加到图层中。
首先,感谢您的@Kreiri。
我已将触摸事件功能更改为
virtual bool ccTouchBegan (CCTouch *pTouch, CCEvent *pEvent);
virtual void ccTouchMoved (CCTouch *pTouch, CCEvent *pEvent);
virtual void ccTouchEnded (CCTouch *pTouch, CCEvent *pEvent);
我又添加了两个函数
virtual void onEnter();
virtual void registerWithTouchDispatcher();
我将初始代码移动到输入
CCLabelTTF* test = CCLabelTTF::create("tesetdd","Arial",40);
CCMenuItemLabel* menuLabel = CCMenuItemLabel::create(test,this,menu_selector(GameWall::menuCall));
menuLabel->setPosition(ccp(winSize.width/2,winSize.height/2));
CCMenu* menu = CCMenu::create(menuLabel,NULL);
menu->setPosition(CCPointZero);
this->addChild(menu,1);
再向 onEnter() 添加三个代码:
this->setTouchEnabled(true);
registerWithTouchDispatcher();
menu->registerWithTouchDispatcher();
而 registerWithTouchDispatcher() :
void GameWall::registerWithTouchDispatcher(){
//registe the single point touch,and take over all touch event
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,kCCMenuHandlerPriority,true);
}
最后,不要忘记在 onExit() 中 romoveDelegate():
void GameWall::onExit(){
CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);
}
让我解释一下,检查文档 registerWithTouchDispatcher() 说 If isTouchEnabled, this method is called onEnter.
和 Override it to change the way CCLayer receives touch events.
与 CCMenu 相同。
相关文章:
- 我的神经网络不起作用 [XOR 问题]
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- C++为什么尽管我调用了void函数,它却不起作用
- 为什么在保护模式下继承升级不起作用
- 循环在计数器中不起作用
- 在其他文件中创建类时在 c++ 项目中不起作用
- Visual studio代码重构似乎不起作用(例如,重命名符号-f2)
- 为什么二进制搜索在我的测试中不起作用
- 我的代码中有错误吗?使用BGI图形的C++代码对我不起作用
- 为什么 const std::p air<K,V>& 在 std::map 上基于范围的 for 循环不起作用?
- 带有指定长度字符* 参数的 std::regex_search 在 VS2017 中不起作用?
- Bjarne Stroustrup Book - std_lib_facilities.h - 不起作用(未知类型名称)
- 为什么简单的算术减法在"if"条件下不起作用?
- 为什么Stroustup书中的has_f不起作用
- 你能检查一下为什么在这个代码中从链接列表中删除项目不起作用吗
- CCMenuItemLabel 在 isTouchEnabled 为 true 时不起作用?
- 如果向量包含重复项,则此函数应返回 true。知道为什么它不起作用吗?
- 为什么 SearchBack 中的 if (find == A[i]) 除了第一次之外没有给出 true?在调试时,除了第一次之外显然不起作用
- 为什么当bool = true时补码运算符不起作用?