代码之家  ›  专栏  ›  技术社区  ›  Darvas

Cocos2d-x按下按钮时滚动滚动视图

  •  2
  • Darvas  · 技术社区  · 9 年前

    使用cocos2d-x-3.13.1

    ScrollView 有88个按钮可以在不同的游戏级别开始。

    我拥有的功能 答:用户可以启动所选级别。但仅当初始点击位置不在按钮上时才可滚动。

    我想要的功能 卷轴视图 如果他的初始触摸位置是on按钮。

    创建 卷轴视图

    containerLayer = cocos2d::LayerColor::create();
    containerLayer->setContentSize(Size(visibleSize.width, visibleSize.height * 4.5));
    containerLayer->setPosition(Point(0, -visibleSize.height * 3.7));
    
    auto scrollView = cocos2d::extension::ScrollView::create();
    scrollView->setContentSize(Size(containerLayer->getContentSize().width, containerLayer->getContentSize().height));
    scrollView->setPosition(Point(visibleSize.width * 0.05, visibleSize.height * 0.05));
    
    // set the scroll-direction for scroll-view
    scrollView->setDirection(cocos2d::extension::ScrollView::Direction::VERTICAL);
    
    scrollView->setViewSize(Size(visibleSize.width * 0.90, visibleSize.height * 0.8));
    
    // set the content offset of the scrollview
    scrollView->setContentOffset(Vec2(0, 0));
    
    scrollView->setTouchEnabled(true);
    
    // add / set the container-layer to the scrollview.
    scrollView->setContainer(containerLayer);
    
    // add scroll-view to your scene-layer.
    this->addChild(scrollView, 100);
    

    添加按钮

    int level = 1;
    const Size buttonSize(100,50);
    
    for (int h = 0; h < 22; h++) {
    
        for (int w = 0; w < 4; w++) {
    
            const Color4B buttonColor(random(0, 255), random(0, 255), random(0, 255), 255);
            auto button = ui::Widget::create();
            button->setContentSize(buttonSize);
            button->setPosition(Point(containerLayer->getContentSize().width * 0.15 + this->getContentSize().width * 0.2 * w, containerLayer->getContentSize().height - containerLayer->getContentSize().height / 23 * (h + 1) + containerLayer->getContentSize().height / 46));
            button->setTouchEnabled(true);
            button->addClickEventListener([=](Ref* _sender)
                                          {
                                              auto scene = GameScene::createSceneWithLevel(level);
                                              Director::getInstance()->replaceScene(TransitionFade::create(1.0, scene, Color3B(0, 0, 0)));
                                          });
            button->addChild(LayerColor::create(buttonColor, buttonSize.width, buttonSize.height));
            scrollView->addChild(button);
    
            level++;
        }
    }
    
    2 回复  |  直到 9 年前
        1
  •  2
  •   Stephen Chan    9 年前

    一种不好的方法是创建button的子类,其中可以存储 ScrollView 作为成员变量:

    auto button = _YourBtnClass_::create(pScrollView);
    

    你需要禁用触摸 卷轴视图 :

    pScrollView->setTouchEnabled(false);
    

    最后,覆盖 onTouchBegan , onTouchMoved , onTouchEnded onTouchCancelled 在里面 YourBtnClass ,其中调用pScrollView的相应函数

        2
  •  1
  •   Darvas    9 年前

    而不是 cocos2d::extension::ScrollView 我曾经 cocos2d::ui::ScrollView , cocos2d::ui::Button 它满足了我的要求 ScrollView .

    scrollView = cocos2d::ui::ScrollView::create();
    scrollView->setDirection(ui::ScrollView::Direction::VERTICAL);
    scrollView->setContentSize(Size(visibleSize.width * 0.9, visibleSize.height * 0.8)); // What user see
    scrollView->setInnerContainerSize(Size(visibleSize.width * 0.9, visibleSize.height * 0.8 * 4.5));
    scrollView->setBounceEnabled(true);
    scrollView->setAnchorPoint(Point(0.5, 0.5));
    scrollView->setPosition(Point(visibleSize.width / 2 + origin.x, visibleSize.height * 0.45 + origin.y));
    
    int level = 1;
    
    for (int h = 0; h < 22; h++) {
    
        for (int w = 0; w < 4; w++) {
    
            auto button = ButtonLevel::create(this, Point(scrollView->getInnerContainerSize().width / 5 * (w + 1), scrollView->getInnerContainerSize().height - scrollView->getInnerContainerSize().height / 22 * (h + 1) + scrollView->getInnerContainerSize().height / 44), level, canBePlayed);
            button->addClickEventListener([=](Ref* _sender)
                                          {
                                              auto scene = GameScene::createSceneWithLevel(level);
                                              Director::getInstance()->replaceScene(TransitionFade::create(1.0, scene, Color3B(0, 0, 0)));
                                          });
            scrollView->addChild(button);
    
            level++;
        }
    }
    
    this->addChild(scrollView, 100);
    

    而且 #include "ui/CocosGUI.h"