hello,
I have search a lot in google and this forum about how to input a gamepad controller, but didn’t really succed to follow how to actually make it work. Some topic are very old, and some other have snippets of code that didn’t really help.
Can someone provide some beginner code to make it work please? I tried with a very simple test. All I want is to see if the event is triggered when I press down something.
#include <CCGameController.h>
USING_NS_CC;
//....
void Player::gamePadListener()
{
Controller::startDiscoveryController();
auto eventListenerController = EventListenerController::create();
eventListenerController->onKeyDown = [=](cocos2d::Controller* controller, int key_code, cocos2d::Event* evt) {
CCLOG("gamepad key pressed %i", key_code);
};
this->_eventDispatcher->addEventListenerWithSceneGraphPriority(eventListenerController, this);
}
I get the following:
ControllerImpl: Could not find a button input map for controller: Xbox Controller
ControllerImpl: Could not find an axis input map for controller: Xbox Controller
Does that means that it only works with an Xbox Controller or am I doing something wrong? I have tried this with 2 controllers: one, the xbox one wireless controller, an two a nacom controller (so a random controller).
Can someone provide some “guide for dummys” . Many topic about it seem to be already in the assumptions on how all this works. How can we make any controller work on windows, mac or linux?? is this too complex to achieve with cocos2dx? Because obviously, every user have a different type of controller. I am even playing games with the ps5 controller on pc.
And one last thing. They keyboard event listener works perfectly fine. I have somthing like this
void Player::createEventListenerKeyboard()
{
auto eventListenerKeyboard = EventListenerKeyboard::create(); // hacer attr privado?
eventListenerKeyboard->onKeyPressed = [=](EventKeyboard::KeyCode code, Event* event) {
//....some code
}
this->_eventDispatcher->addEventListenerWithSceneGraphPriority(eventListenerKeyboard, GameGlobals::playerSprite);
}
my question is about the last line.
this->_eventDispatcher->addEventListenerWithSceneGraphPriority(eventListenerKeyboard, GameGlobals::playerSprite);
if go back to the controller code and do this
// this doesn't crash
// this->_eventDispatcher->addEventListenerWithSceneGraphPriority(eventListenerController, this);
// this crashes
this->_eventDispatcher->addEventListenerWithSceneGraphPriority(eventListenerController, GameGlobals::playerSprite);
then the game crashes before it starts with the error
Program: ...in\bin\spaceShooter_0.1\Debug\spaceShooter_0.1.exe
File: D:\CHIMERA STUDIO\CHIMERA GAMES\lea...\CCEvent...her.cpp
Line: 504
Expression: listener && node
For information on how your program can cause an assertion
failure, see the Visual C++ documentation on asserts
so why using GameGlobals::playerSprite crashes for the controller, but works fine in the keyboard?
Any, I hope someone shed some light on this because I am about to publish my first game and I can’t if I don’t figure out all this.
thanks in advance