在新的cocos2d-x 3.0rc中,我想检测图层中的触摸。我的课程中有过多的功能,如下所述。
virtual bool onTouchBegan(CCTouch* touch, CCEvent* event);
virtual void onTouchMoved(CCTouch* touch, CCEvent* event);
virtual void onTouchEnded(CCTouch* touch, CCEvent* event);
但未检测到触摸。任何想法都会发生这种情况吗?
答案 0 :(得分:3)
要启用触摸,我使用了以下代码。在cocos2d-x 3.0 RC1中完美运行
void class_name::onEnter()
{
Layer::onEnter();
// Register Touch Event
auto dispatcher = Director::getInstance()->getEventDispatcher();
auto listener = EventListenerTouchOneByOne::create();
listener->onTouchBegan = CC_CALLBACK_2(class_name::onTouchBegan, this);
listener->onTouchMoved = CC_CALLBACK_2(class_name::onTouchMoved, this);
listener->onTouchEnded = CC_CALLBACK_2(class_name::onTouchEnded, this);
dispatcher->addEventListenerWithSceneGraphPriority(listener, this);
}
答案 1 :(得分:1)
在Cocos2d-x 3.0中检测触摸
在(HelloWorld.h)中编写代码 {
cocos2d::EventListenerTouchAllAtOnce *Listner;
void onTouchesBegan(const std::vector<cocos2d::Touch *> &touches, cocos2d::Event *event);
void onTouchesMoved(const std::vector<cocos2d::Touch *> &touches, cocos2d::Event *event);
void onTouchesEnded(const std::vector<cocos2d::Touch *> &touches, cocos2d::Event *event);
}
在init方法中编写代码(HelloWorld.cpp) {
Listner = EventListenerTouchAllAtOnce::create();
Listner->onTouchesBegan = CC_CALLBACK_2(HelloWorld::onTouchesBegan, this);
Listner->onTouchesMoved = CC_CALLBACK_2(HelloWorld::onTouchesMoved, this);
Listner->onTouchesEnded = CC_CALLBACK_2(HelloWorld::onTouchesEnded, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(Listner, this);
}
答案 2 :(得分:-1)
enable touch on init() Or onEnter()
this->setTouchEnabled(true);
CCDirector::sharedDirector() -> getTouchDispatcher() -> addTargetedDelegate( this, 0, true );