同时触摸场景

Simultaneous touches on scene

我在触摸屏幕时遇到一个问题。我在屏幕上连接了两个模块,左边有一个操纵杆,它有自己的触摸,在右边我有一组按钮,它们有自己的动作或触摸,两者不能一起工作。我尝试在按钮上设置触摸而不是回调,并尝试用精灵替换按钮但没有任何运气。

查看下面我的要求

非常感谢任何帮助或建议。

谢谢。

查看下面我的代码 我试过的方法

我正在使用 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);

两者都添加在同一个场景中,就像我在 图像图形 中显示的那样。现在两者不能一起工作。一个阻止另一个,我尝试添加触摸委托并尝试查看下面的代码

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);
        }

    }

}

None 以上方法对我有用。

更新 2

刚刚测试了下面的代码returns总是1

void GameScene::onTouchesBegan(const std::vector<Touch*>& touches, Event *unused_event){
    printf("\nTouches = %d",(int)touches.size());
}

更新 3

Multitouch 已为 cocos2dx 启用,见下文

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,我需要用不同的方式来做吗?

如果在iOS上测试,multipleTouchEnabled 属性需要设置为YES才能接受视图多点触控。

在您的 AppController.mm 中,查找函数 didFinishLaunchingWithOptions 并添加以下行

[eaglView setMultipleTouchEnabled: true];