【问题标题】:Implement a scrollview with cocos2d-x 3.6用 cocos2d-x 3.6 实现滚动视图
【发布时间】:2015-06-17 23:04:16
【问题描述】:

想知道是否有人知道如何在 cocos2d-x 3.6 (C++) 中实现滚动视图。我找到的所有教程都是针对早期 cocos2d-x 版本的。

谢谢

我在下面添加了我的代码,我可以显示灰色的滚动视图框,但它不能滚动并且按钮没有出现在上面:

头文件:“CocosGUI.h”和“cocos-ext.h”

//add scroll view
Size scollFrameSize = Size(visibleSize.width, visibleSize.height/4);
auto scrollView = cocos2d::ui::ScrollView::create();
scrollView->setContentSize(scollFrameSize);
scrollView->setBackGroundColorType(cocos2d::ui::Layout::BackGroundColorType::SOLID);
scrollView->setBackGroundColor(Color3B(200, 200, 200));
scrollView->setPosition(Point(0, visibleSize.height/1.5));
scrollView->setDirection(cocos2d::ui::ScrollView::Direction::HORIZONTAL);
scrollView->setBounceEnabled(true);
scrollView->setTouchEnabled(true);
auto containerSize = Size(scollFrameSize.width*2, scollFrameSize.height*2);
 scrollView->setInnerContainerSize(containerSize);

this->addChild(scrollView);


auto button1 = cocos2d::ui::Button::create();
button1->setColor(Color3B(250, 200, 50));
button1->setTouchEnabled(true);
button1->setContentSize(Size(100, 100));
button1->setPosition(Point(containerSize.width / 4, containerSize.height / 2));
scrollView->addChild(button1);

auto button2 = cocos2d::ui::Button::create();
button2->setColor(Color3B(250, 200, 50));
button1->setContentSize(Size(100, 100));
button2->setTouchEnabled(true);
button2->setPosition(Point(containerSize.width / 8, containerSize.height / 2));
scrollView->addChild(button2);

【问题讨论】:

    标签: c++ xcode cocos2d-x


    【解决方案1】:

    我想通了,它正在滚动,但我添加了错误的按钮。对于任何有兴趣的人,请添加这样的按钮

    auto button1 = ui::Button::create();
    button1->setTouchEnabled(true);
    button1->ignoreContentAdaptWithSize(false);
    button1->setContentSize(Size(100, 100));
    button1->loadTextures("pic1.png", "pic2.png");
    button1->setPosition(Point(containerSize.width / 8, containerSize.height / 2));
    scrollView->addChild(button1);
    

    【讨论】:

    • 我认为按钮会吞下触摸事件,因此滚动视图无法响应触摸移动事件。你可以添加一个优先级更高但不会吞下触摸的事件监听器,将事件传递给滚动视图
    【解决方案2】:
    Size scroll_size = Director::getInstance()->getWinSize();
    Size container_size = Size(scroll_size.width * 2, scroll_size.height);
    Layer* container = Layer::create();
    container->setContentSize(container_size);
    ScrollView* scroll = ScrollView::create(scroll_size, container);
    mScroll->setDelegate(this);
    mScroll->setDirection(ScrollView::Direction::HORIZONTAL);
    

    1.容器的大小应该大于滚动视图的大小

    2.将子元素添加到容器中,而不是滚动视图

    3.实现ScrollViewDelegate

    【讨论】:

    • 没用。我在第 5 行(参数太多)和第 6 行(cocos2d::ui::ScrollView 中没有名为“set delegate”的成员)遇到错误
    • 我在上面添加了我的代码,如果你找到解决方案请告诉我
    • @ctapp1 cocos2dx 中有两个 Scrollview,你使用的是 cocos/ui/widgets/UIScrollview 我的意思是 cocos/extensions/GUI/CCScrollview
    猜你喜欢
    • 1970-01-01
    • 2016-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多