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

1

9

So I'm trying to create a simple app using cocos2d-x newest build and for some reason can't get my touch wired up. Here are my 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)
{

}

I noticed when I call into the setTouchEnabled call that an internal flag called _running is set to false so it doesn't actually register my touch events. However I can't seem to figure out why that is the case. Am I calling things incorrectly or in the wrong order?

Cite answered 3/11, 2013 at 1:8 Comment(0)
K
19

Currently, cocos2dx is going through a major overhauling of the library and many things have changed including Touch registration and propagation. Here is how it works now:

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)
{

}

Hope it helps!

Kauffmann answered 3/11, 2013 at 9:37 Comment(6)
Looks like registerWithTouchDispatcher is deprecated, and getTouchDispatcher() doesn't exist any longer on the Director any idea what the equivalent is now?Cite
Well, the API has been updated again. :) Please have a look at my updated answer.Kauffmann
Thanks for the help, I'm using the 3.0 version that was just on their main page not what was on github(which changed everything as well). So my version doesn't have the eventListener either. I'm going to try to upgrade to what they have in github, I'll keep you updated if I can get it to work.Cite
Man the download on their website and what is in github is drastically different, finally got it in and this code works. Thanks again for the help.Cite
You said that the library will automatically unregister the node in the destructor, but you are adding the EventListener each time the node is added to the Scene. (i.e. if we are pooling the object or something, we are needlessly replacing the already existing eventLister, since we never reached the destructor).Hamadryad
Did exactly that and it is not working for me :/. I am using Cocos2d-x v3.7Motorcade

© 2022 - 2024 — McMap. All rights reserved.