无法在多平台 cocos2d-x 应用程序中工作

Can't get touch to work in multi-platform cocos2d-x app

本文关键字:应用程序 工作 cocos2d-x 平台      更新时间:2023-10-16

所以我正在尝试使用 cocos2d-x 最新版本创建一个简单的应用程序,但由于某种原因无法连接我的触摸。 以下是我的课程:

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 的内部标志设置为 false,因此它实际上并没有注册我的触摸事件。 但是,我似乎无法弄清楚为什么会这样。 我是否调用错误或顺序错误?

目前,cocos2dx正在对库进行重大改革,许多事情都发生了变化,包括触摸注册和传播。这是它现在的工作原理:

游戏层.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);
};

游戏层.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)
{
}

希望对您有所帮助!

相关文章: