3

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){

}
CodeSmile
  • 64,284
  • 20
  • 132
  • 217
Frizlancer
  • 31
  • 1
  • 2

3 Answers3

2

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]; // <-----
Steve T
  • 7,729
  • 6
  • 45
  • 65
Mercurial
  • 2,095
  • 4
  • 19
  • 33
  • 1
    The question is for cocos2d-x which uses c++ and your answer is for objective-c with cocos2d. – whiteSkar Feb 10 '15 at 00:31
  • @whiteSkar My answer is for cocos2d-x. Each native AppDelegate will delegate each UIApplicationDelegate call to the shared(c++) delegate. Therefore, the principle of enabling the multitouch is valid. The example I gave was the iOS implementation. I can't possibly put each and every implementation in the answer. – Mercurial Feb 10 '15 at 22:05
  • 1
    I see. I thought your "example" was the "answer". If that is just an example, I want to remove the downvote I made. However, it says I cannot remove my downvote unless your post is edited. Can you edit your post so I can remove my downvote? – whiteSkar Feb 12 '15 at 06:16
2

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.

emidander
  • 2,383
  • 22
  • 29
1

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];
ahcox
  • 9,349
  • 5
  • 33
  • 38