我正在使用cocos2d-x v3.3rc0
我试图处理多点触控,但我只收到一次触摸
它的行为类似于单点触控,而不是多点触控。当触摸超过1个手指时,onTouchesBegan只调用一次。
希望有人可以帮我解决这个问题。
以下是启用多点触控的代码
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){
}
答案 0 :(得分:2)
您必须在每个平台上启用多点触控才能在本地支持它。
这是一个例子(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]; // <-----
答案 1 :(得分:2)
您必须在项目的iOS部分启用多点触控,即使项目的其余部分是用C ++编写的,也会在Objective C中启用。模板项目在app控制器(AppController.mm)中有行 Msg 208, Level 16, State 1, Line 1
Invalid object name 'dbo.tdnResetTicket'.
,您可以将NO更改为YES。
答案 2 :(得分:0)
对于iOS,您需要启用它。从Cocos2d-x 3.16开始,修改RootViewController.mm
命令行工具生成的cocos new
的单行以启用多点触控。
--- 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];