C++ 无法在COCOS 2dx中接触?

C++ 无法在COCOS 2dx中接触?,c++,sprite,cocos2d-x,ccsprite,touchesbegan,C++,Sprite,Cocos2d X,Ccsprite,Touchesbegan,下面是我的HelloWorld.h课程: class HelloWorld : public cocos2d::CCLayer { public: HelloWorld(); // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone virtual bool init(); b2World* world; // there's no

下面是我的
HelloWorld.h
课程:

class HelloWorld : public cocos2d::CCLayer
{
public:
HelloWorld();

// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init();

b2World* world;

// there's no 'id' in cpp, so we recommend returning the class instance pointer
static cocos2d::CCScene* scene();

// a selector callback
void menuCloseCallback(CCObject* pSender);

// implement the "static node()" method manually
CREATE_FUNC(HelloWorld);

virtual void draw();
virtual void ccTouchesEnded(cocos2d::CCSet* touches, cocos2d::CCEvent* event);
virtual void ccTouchesMoved(cocos2d::CCSet* touches, cocos2d::CCEvent* event);
virtual void ccTouchesBegan(cocos2d::CCSet* touches, cocos2d::CCEvent* event);
void update(float dt);


};
在我的
HelloWorld.cpp
类中,我初始化了我的
init
方法

bool HelloWorld::init(){
setTouchEnabled( true );
setAccelerometerEnabled( true );
scheduleUpdate();
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, true);
return true;
}

这段代码现在正在为我工作!:)

目标代表用于单触事件。将事件更改为以下内容:

虚拟bool CCTouch开始(CCTouch*pTouch,CCEvent*pEvent)

您可以在下面的Cocos2D文档的iPhone端阅读更多关于目标和标准触摸代理的信息

按照下面的初始化方法编写代理解决了问题

    CCDirector::sharedDirector()->getTouchDispatcher()->addStandardDelegate(this, 1);

如果要禁用多点触控功能,可以使用:

virtual bool ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent);
virtual void ccTouchMoved(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent){}
virtual void ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent){}
因此,您需要:

bool init()
{
    ...
    setTouchEnabled(true);
    this->setTouchMode(ccTouchesMode::kCCTouchesOneByOne); // Important
}
如果省略最后一行,则需要覆盖这些(多点触控模式):


嗯,只是一个猜测,试着从cctoucks*method declarations.Nope中删除“virtual”关键字。没用(
cocos2d::CCLayer
已经从
cocos2d::CCTargetedTouchDelegate
派生而来,请尝试将其从类定义中删除。您是否尝试过将代码作为标准委托运行,以查看是否可以接收到任何内容?不,我对该编程非常陌生!所以我不知道您的确切意思!摆脱所有targeted委托代码并添加此行:CCDirector::sharedDirector()->getTouchDispatcher()->addStandardDelegate(此,1);然后查看您是否进入CCTouches以断点开始。标准代理应该像常规应用一样处理触摸,因此您应该获得所有触摸事件。使用此代理,我将从派生类hello world到cctouchdelegateYou摆脱的CCTouchDelegatePublic CoCoCos2D::CCTargetedTouchDelegate进行错误模糊转换呃,对吗?你能发布你的整个init方法吗。
void ccTouchesBegan(cocos2d::CCSet* touches, cocos2d::CCEvent* event);
void ccTouchesMoved(cocos2d::CCSet* touches, cocos2d::CCEvent* event);
void ccTouchesEnded(cocos2d::CCSet* touches, cocos2d::CCEvent* event);