我在处理屏幕上的触摸时遇到一个问题。我在屏幕上附有两个模块,在左边有一个操纵杆,它有自己的触摸,在右侧我有一组按钮,它们有自己的动作或触摸,两者都不能一起工作。我试图在按钮上设置触摸而不是回调,并尝试用精灵替换按钮而没有任何运气。
请参阅下面的要求
非常感谢任何帮助或建议。
感谢。
请参阅下面的代码我尝试过的内容
我正在使用 SneakyJoystick
作为操纵杆,如下所示
void Game::prepareJoystick(){
Size winSize = Director::getInstance()->getWinSize();
Rect joystickBaseDimensions;
joystickBaseDimensions = Rect(0, 0, 160.0f, 160.0f);
Point joystickBasePosition;
joystickBasePosition = Vec2(winSize.width*0.08, winSize.height*0.15);
SneakyJoystickSkinnedBase *joystickBase = new SneakyJoystickSkinnedBase();
joystickBase->init();
joystickBase->setPosition(joystickBasePosition);
joystickBase->setBackgroundSprite(CCSprite::create("iphone-joystick.png"));
joystickBase->setThumbSprite(CCSprite::create("iphone-joystick_cursor.png"));
SneakyJoystick *aJoystick = new SneakyJoystick();
aJoystick->initWithRect(joystickBaseDimensions);
aJoystick->autorelease();
joystickBase->setJoystick(aJoystick);
joystickBase->setPosition(joystickBasePosition);
leftJoystick = joystickBase->getJoystick();
leftJoystick->retain();
this->addChild(joystickBase);
joystickBase->setScale(GameConfig::controlScale());
}
对于右侧的控件我使用cocos2d :: ui :: Buttons及其回调如下
Button *button=Button::create(imageName);
button->setTouchEnabled(true);
button->setPressedActionEnabled(true);
button->addClickEventListener(callback);
button->setPosition(position);
两者都添加在同一场景中,就像我在图片图形中所示。现在两者都没有合作。一个阻止另一个,我试图添加touches delegates并尝试查看下面的代码
auto listener = EventListenerTouchAllAtOnce::create();
listener->onTouchesBegan = CC_CALLBACK_2(Game::onTouchesBegan, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
和处理如下
void GameScene::onTouchesBegan(const std::vector<Touch*>& touches, Event *unused_event){
for(int i=0;i<touches.size();i++){
Vec2 touchLocation=touches.at(i)->getLocation();
if(btnPunch->getBoundingBox().containsPoint(touchLocation)){
printf("\nshould punch");
punchCallbackAction(this);
}
}
}
上述方法均不适合我。
更新2
刚刚测试了下面的代码总是返回1
void GameScene::onTouchesBegan(const std::vector<Touch*>& touches, Event *unused_event){
printf("\nTouches = %d",(int)touches.size());
}
更新3
cocos2dx启用了 Multitouch
,见下文
bool GLViewImpl::initWithRect(const std::string& viewName, Rect rect, float frameZoomFactor)
{
CGRect r = CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
convertAttrs();
CCEAGLView *eaglview = [CCEAGLView viewWithFrame: r
pixelFormat: (NSString*)_pixelFormat
depthFormat: _depthFormat
preserveBackbuffer: NO
sharegroup: nil
multiSampling: NO
numberOfSamples: 0];
[eaglview setMultipleTouchEnabled:YES];
_screenSize.width = _designResolutionSize.width = [eaglview getWidth];
_screenSize.height = _designResolutionSize.height = [eaglview getHeight];
// _scaleX = _scaleY = [eaglview contentScaleFactor];
_eaglview = eaglview;
return true;
}
更新3 GLView initialization
auto director = Director::getInstance();
auto glview = director->getOpenGLView();
if(!glview) {
glview = GLViewImpl::create("MyGame");
director->setOpenGLView(glview);
}
上面的初始化没有Multitouch property
,我需要以不同的方式进行吗?
答案 0 :(得分:2)
如果在iOS上进行测试,则在视图可以接受多点触控之前,multipleTouchEnabled
属性需要设置为 YES
。
在AppController.mm
中,查找功能didFinishLaunchingWithOptions
并添加以下行
[eaglView setMultipleTouchEnabled: true];