首页 > 开发 > C++ > 正文

Cocos2d-x跨平台中无法实现多点触控

2017-09-11 21:35:13  来源: 网友分享

我想用cocos2d-x的最新版本开发一个简单的应用,但是不能正常运行touch功能。这是我的classes:

class GameLayer : public cocos2d::Layer{public:    static cocos2d::Layer* createLayer();    void update(float dt);    virtual bool init();    CREATE_FUNC(GameLayer);private:    bool onTouchBegan(cocos2d::Touch* touch, cocos2d::Event* event);    void onTouchMoved(cocos2d::Touch* touch, cocos2d::Event* event);    void onTouchEnded(cocos2d::Touch* touch, cocos2d::Event* event);};cocos2d::Layer* GameLayer::createLayer(){    GameLayer *layer = GameLayer::create();    return layer;}bool GameLayer::init(){    if (!cocos2d::Layer::init())    {        return false;    }    this->schedule(schedule_selector(GameLayer::update));    this->setTouchEnabled(true);    return true;}void GameLayer::update(float dt){}bool GameLayer::onTouchBegan(cocos2d::Touch* touch, cocos2d::Event* event){    cocos2d::log("You touched %f, %f", touch->getLocationInView().x, touch->getLocationInView().y);    return true;}void GameLayer::onTouchMoved(cocos2d::Touch* touch, cocos2d::Event* event){}void GameLayer::onTouchEnded(cocos2d::Touch* touch, cocos2d::Event* event){}

当我启动setTouchEnabled命令时,会出现_running的内部标识,提醒我出现错误,因此也就不能正常实现touch事件。但我不知道为什么会出现这种情况,是我的调用顺序出现问题了吗?

原问题:Cocos2d-x跨平台中无法实现多点触控

解决方案

答:nomannasim
(最佳答案)
如今,cocos2d-x正在全面完善函数库(library),包括touch registration和propagation在内的很多内容都发生了变化,下面是它的工作原理:
GameLayer.h

class GameLayer : public cocos2d::Layer{public:    static cocos2d::Layer* createLayer();    void update(float dt);    virtual bool init();    CREATE_FUNC(GameLayer);private:    virtual void onEnter();    virtual void onExit();    bool onTouchBegan(cocos2d::Touch* touch, cocos2d::Event* event);    void onTouchMoved(cocos2d::Touch* touch, cocos2d::Event* event);    void onTouchEnded(cocos2d::Touch* touch, cocos2d::Event* event);};

GameLayer.cpp

cocos2d::Layer* GameLayer::createLayer(){    GameLayer *layer = GameLayer::create();    return layer;}bool GameLayer::init(){    if (!cocos2d::Layer::init())    {        return false;    }    this->schedule(schedule_selector(GameLayer::update));    return true;}void GameLayer::onEnter(){    Layer::onEnter();    // Register Touch Event    auto dispatcher = Director::getInstance()->getEventDispatcher();    auto listener = EventListenerTouchOneByOne::create();    listener->onTouchBegan = CC_CALLBACK_2(GameLayer::onTouchBegan, this);    listener->onTouchMoved = CC_CALLBACK_2(GameLayer::onTouchMoved, this);    listener->onTouchEnded = CC_CALLBACK_2(GameLayer::onTouchEnded, this);    dispatcher->addEventListenerWithSceneGraphPriority(listener, this);}void GameLayer::onExit(){    // You don't need to unregister listeners here as new API    // removes all linked listeners automatically in CCNode's destructor    // which is the base class for all cocos2d DRAWING classes    Layer::onExit();}void GameLayer::update(float dt){}bool GameLayer::onTouchBegan(cocos2d::Touch* touch, cocos2d::Event* event){    cocos2d::log("You touched %f, %f", touch->getLocationInView().x, touch->getLocationInView().y);    return true;}void GameLayer::onTouchMoved(cocos2d::Touch* touch, cocos2d::Event* event){}void GameLayer::onTouchEnded(cocos2d::Touch* touch, cocos2d::Event* event){}