Cocos2d-x handling input with multiple layers

How i can handle input one time for all layer? It’s possible? Something like this https://github.com/libgdx/libgdx/wiki/Event-handling

Sorry for my English.

I have created an InputManager singleton, similiar to the Director class.
This class handles the keypress and keyrelease methods, and also I have a method that is called: isKeyPressed.
That way, wherever in my program i can just call:

InputManager::getInstance()->isKeyPressed(EventKeyboard::KeyCode::KEY_RIGHT_ARROW)

There are plenty examples out there of how to implement a singleton class, let me know if you need any more help.

Koby

1 Like

Thanks! Have you some open source example of this implementation?

Also if you want to manage all touches yourself, if you want to handle them all yourself you can either do so on your root Layer (2.x) or root Node (3.x, since a using a literal cocos2d::Layer is no longer necessary for touches). This is sometimes useful if you want to detect multiple taps, clicks, or gestures. You can even have your UI on a separate layer so that the Widgets/MenuItems/Buttons will handle their touches with higher priority if for example you have a game where you want to pan, zoom, and select units within your game play layer.

e.g. XTLayer :

Otherwise you may find benefit learning to use the touch handlers per object instead of globally with an InputManager.

This is a nice idea steve, but I got one question:

When I try to override the onKeyPressed of a subclass of Layer.
I must also call

setKeyboardEnabled(true);

in order for the keyboard listener to be added to the _eventDispatcher (under CCLayer.ccp)

However setKeyboardEnabled gives me a deprecated warning - is that correct?

I dont see it as deprecated in the docs? perhaps a mistake?

It’s neither in these two:

http://www.cocos2d-x.org/reference/native-cpp/V3.4/df/de4/classcocos2d_1_1_layer.html#a5f57122dad1afa3f4f412412af9a43eb

http://www.cocos2d-x.org/reference/native-cpp/V3.4/da/d58/deprecated.html

Koby

If you’re using 3.x (or at least 3.3, unsure of earlier) then you’ll want to use the new event handlers.
http://cocos2d-x.org/programmersguide/8/

Note: you no longer need Layers for event handling, so your root object could be Node instead, just FYI. You might want to setup input handlers in onEnter and remove them in onExit.

If you’re using the scene graph priority handlers you can do the following

void  ClassName::onEnter()
{
    Layer::onEnter(); // or Node::onEnter()
    this->setupInput();
}

void  ClassName::onExit()
{
    getEventDispatcher()->removeEventListenersForTarget(this);
   // or you could save reference/pointer to listeners and remove them individually
    Layer::onExit(); // or Node::onExit()
}

void ClassName::setupInput()
{
  // setup an event listener
  auto listenerTouch = //...  
  auto listenerKeyboard = //...

  getEventDispatcher()->addEventListenerWithSceneGraphPriority(listenerTouch, this);
  getEventDispatcher()->addEventListenerWithSceneGraphPriority(listenerKeyboard, this);
}
```