9

所以我正在尝试使用 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 时,它​​实际上并没有注册我的触摸事件。但是我似乎无法弄清楚为什么会这样。我是在错误地调用事物还是以错误的顺序调用事物?

4

1 回答 1

19

目前,cocos2dx 正在对库进行大修,包括 Touch 注册和传播在内的许多事情都发生了变化。这是它现在的工作方式:

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

}

希望能帮助到你!

于 2013-11-03T09:37:30.267 回答