无法接触COCOS 2dx?

时间:2014-03-12 18:30:26

标签: 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 '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;
}

这段代码现在对我有用! :)

2 个答案:

答案 0 :(得分:1)

目标代表适用于单点触控事件。将您的活动更改为以下内容:

virtual bool ccTouchBegan (CCTouch *pTouch, CCEvent *pEvent)

您可以在此Link

的Cocos2D文档的iPhone一侧阅读有关目标和标准触控代理的更多信息

按照以下方法在我的初始化方法中编写委托解决了问题

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

答案 1 :(得分:0)

如果要禁用multiTouch功能,可以使用:

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
}

如果省略最后一行,则需要覆盖这些(multiTouch模式):

void ccTouchesBegan(cocos2d::CCSet* touches, cocos2d::CCEvent* event);
void ccTouchesMoved(cocos2d::CCSet* touches, cocos2d::CCEvent* event);
void ccTouchesEnded(cocos2d::CCSet* touches, cocos2d::CCEvent* event);
相关问题