Can't get multitouch work on cocos2d-x v3
I am using cocos2d-x v3.3rc0
I am trying to handle multitouch but I only get one touch.
This behavior is similar to one touch, not multitouch. onTouchesBegan only calls once when I touch more than one finger.
Hope someone can help me solve this problem.
Here is my code for enabling multi touch
ControlLayer.h
#include "cocos2d.h"
class ControlLayer : public cocos2d::Layer{
public:
static ControlLayer* create();
virtual bool init();
void onTouchesBegan(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event *unused_event);
void onTouchesMoved(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event *unused_event);
void onTouchesEnded(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event *unused_event);
};
ControlLayer.cpp
bool ControlLayer::init(){
if (!Layer::init()){
return false;
}
auto touchListener = EventListenerTouchAllAtOnce::create();
touchListener->onTouchesBegan = CC_CALLBACK_2(ControlLayer::onTouchesBegan, this);
touchListener->onTouchesMoved = CC_CALLBACK_2(ControlLayer::onTouchesMoved, this);
touchListener->onTouchesEnded = CC_CALLBACK_2(ControlLayer::onTouchesEnded, this);
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(touchListener, this);
return true;
}
void ControlLayer::onTouchesBegan(const std::vector<Touch*>& touches, Event *unused_event){
CCLOG("onTouchesBegan[%lu]", touches.size());
}
void ControlLayer::onTouchesMoved(const std::vector<Touch*>& touches, Event *unused_event){
CCLOG("onTouchesMoved[%lu]", touches.size());
}
void ControlLayer::onTouchesEnded(const std::vector<Touch*>& touches, Event *unused_event){
}
You need to enable multitouch on each platform to support it natively.
Here's an example (iOS):
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// Add the view controller view to the window and display.
window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]];
// Init the CCEAGLView
CCEAGLView *eaglView = [CCEAGLView viewWithFrame: [window bounds]
pixelFormat: kEAGLColorFormatRGBA8
depthFormat: GL_DEPTH24_STENCIL8_OES
preserveBackbuffer: NO
sharegroup: nil
multiSampling: NO
numberOfSamples: 0];
[eaglView setMultipleTouchEnabled:YES]; // <-----
You need to enable multi touch on the iOS part of your project that will be in Objective C, even if the rest of the project is written in C ++. Template projects have a line [eaglView setMultipleTouchEnabled:NO];
in the app controller (AppController.mm) ready for you to change NO to YES.
For iOS, you need to enable it. Starting with Cocos2d-x 3.16, change one line RootViewController.mm
generated by the command line tool cocos new
to enable multi-touch.
--- a/proj.ios_mac/ios/RootViewController.mm
+++ b/proj.ios_mac/ios/RootViewController.mm
@@ -52,7 +52,7 @@
numberOfSamples: 0 ];
// Enable or disable multiple touches
- [eaglView setMultipleTouchEnabled:NO];
+ [eaglView setMultipleTouchEnabled:YES];