Inputs in Cocos2d-x are event driven. Every time you touch the screen or press a key on the keyboard, an event is sent and you can process the data from those input devices in your event callbacks.
Note that your class should inherit from the Layer class to handle the touch events.
Events
There are three basic steps to setup an input device. In your init() method:
- Create a listener for the type of device you want to use.
- Assign a callback method or lambda to each event that you want to use for that device.
- Add your listener to the event dispatcher _eventDispatcher.
For step 1:
Each device has its own listener type:
// Inside the init() method auto listener = EventListenerKeyboard::create(); // For Keyboard auto listener = EventListenerMouse::create(); // For Mouse auto listener = EventListenerTouchOneByOne::create(); // For Single Touch auto listener = EventListenerTouchAllAtOnce::create(); // For Multi Touch auto listener = EventListenerAcceleration::create(); // For Accelerometer
For step 2:
In your class header, add a method as a callback for an event (onKeyPressed for example).
virtual void onKeyPressed(EventKeyboard::KeyCode keyCode, Event* event);
Inside the cpp file:
// Inside the init() method listener->onKeyPressed = CC_CALLBACK_2(KeyboardScene::onKeyPressed, this); // Defining the callback method for a class called KeyboardScene void KeyboardScene::onKeyPressed(EventKeyboard::KeyCode keyCode, Event* event) { // Do Something With The Data }
You could also use a lambda instead of a method if you like C++11 style:
// Inside the init() method listener->onKeyPressed = [](EventKeyboard::KeyCode keyCode, Event* event){ // Do Something With The Data }
For step 3:
We add the listener to the _eventDispatcher in the init() method.
// Inside the init() method _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
Now that we know the basic principle, lets have a look at each device explicitly and see them in action.
Working With Different Inputs
I divide this part to four different sections:
What is The CC_CALLBACK_2 Macro?
These macros that define a callback type are defined as:
// new callbacks based on C++11 #define CC_CALLBACK_0(__selector__,__target__, ...) std::bind(&__selector__,__target__, ##__VA_ARGS__) #define CC_CALLBACK_1(__selector__,__target__, ...) std::bind(&__selector__,__target__, std::placeholders::_1, ##__VA_ARGS__) #define CC_CALLBACK_2(__selector__,__target__, ...) std::bind(&__selector__,__target__, std::placeholders::_1, std::placeholders::_2, ##__VA_ARGS__) #define CC_CALLBACK_3(__selector__,__target__, ...) std::bind(&__selector__,__target__, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, ##__VA_ARGS__)
The std::bind binds some parameters to a function. In the case of our callback, the __selector__ is KeyboardScene::onKeyPressed and the __target__ is a this pointer and we have two extra parameters attached to it. The attached parameters are the EventKeyboard::KeyCode keyCode and Event* event.
Those _0, _1, _2, etc… are place holders for number of parameters defined in std::placeholders. Because we have 2 parameters to pass, we use _2 to pass them.
The std::bind and the _1, _2, _3, …, _N notations are new C++11 stuff!
If you want to learn more about them, I recommend to have a look at the standard C++ page.