Frizlancer
Frizlancer

Reputation: 31

Can't get multitouch work on cocos2d-x v3

I'm using cocos2d-x v3.3rc0
I'm trying to handle multi touch, but I only receive only one touch.
It's behavior is similar with single touch, not multitouch. onTouchesBegan only called once when I touch more than 1 finger.

Hope someone can help me solve this out.

Here is my code to enable 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){

}

Upvotes: 3

Views: 2630

Answers (3)

ahcox
ahcox

Reputation: 9970

For iOS you need to enable it. As of Cocos2d-x 3.16, modify a single line of the RootViewController.mm generated by the cocos new commandline tool 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];

Upvotes: 1

Mercurial
Mercurial

Reputation: 2165

You have 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's 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]; // <-----

Upvotes: 2

emidander
emidander

Reputation: 2413

You have to enable multi touch in the iOS part of your project, which will be in Objective C even if the rest of the project is written in C++. Template projects have the line [eaglView setMultipleTouchEnabled:NO]; in the app controller (AppController.mm) ready for you to change NO to YES.

Upvotes: 2

Related Questions