Cocos2d-x CCScrollview正在向上滚动

Pri*_*ohn 4 c++ eclipse iphone android cocos2d-x

问候Stackoverflow,

我目前正在尝试将滚动菜单实现为我正在开发的cocos2d-x游戏.问题是这些项目正朝着我想要的相反方向滚动.例如,如果我将手指从屏幕底部拖到顶部,精灵将弹回原位.

如果我将手指从顶部拖到底部,精灵将跟随我的手指(直到它到达边界).

这是我的代码:

    /////////////////////
    //SCROLL VIEW + LAYER
    CCLayer *layer = CCLayer::create();
    layer->setContentSize( CCSizeMake( winSize.width, winSize.height ) );
    layer->addChild( menu );

    scrollView = CCScrollView::create();
    scrollView->retain();
    scrollView->setContentSize( CCSizeMake ( layer->getContentSize().width,  layer->getContentSize().height ) );
    scrollView->setDirection( CCScrollViewDirectionVertical );
    scrollView->setPosition( ccp( 0,0 ) );
    scrollView->setContainer( layer );

    this->addChild(scrollView);

    scrollView->setContentOffset( CCPointZero );
Run Code Online (Sandbox Code Playgroud)

任何建议或帮助都非常感谢.如果您还有其他任何方法可以使用其他方法创建滚动菜单,我很乐意听到.

谢谢!

最好的问候Andreas

PAT*_*PAT 8

请参阅:cocos2dx/extensions/GUI/CCScrollView/CCScrollView.cpp

特别是CCScrollView::minContainerOffsetCCScrollView::maxContainerOffset

CCScrollViewOpenGL坐标(与窗口坐标相对)中工作 - 值相对于(左,下),正Y轴向上.另外,请记住滚动视图的定位,容器锚定(CCNode::setAnchorPoint)到(左,底).

当您向下滚动(向上移动/拉动内容以查看剪切/剪辑下方的内容)时,您会看到屏幕下边缘下方的内容,但是当您放开触摸/拖动时它会弹回,因为maxContainerOffset返回(0, 0)并且您我只是试图转向积极的内容抵消.

CCScrollView和容器的坐标

该图显示了创建/初始化时scrollview容器的状态.这是设置和定位子元素容器时"思考"的状态或坐标.灰色矩形(左侧,下方)显示滚动容器的有效区域.想象一下,容器的锚点在其内部移动.

要查看容器滚动到顶部以开始(在窗口坐标中工作时您期望的那样),请相应地设置内容偏移(在设置之后立即).这将给出预期的结果/行为.

scrollView->setContentOffset(ccp(0.f, (scrollViewHeight-scrollContainerHeight)), false);
Run Code Online (Sandbox Code Playgroud)

更完整的示例位于下面的编辑代码中.

  1. 允许在窗口坐标(正Y轴向下)中滚动的"修复"可能是调整扩展,这将需要您重建cocos2dx库并影响所有项目(甚至可能尝试其他示例代码).

    /*
    // (StackOverflow Post Edit: This hack is not required.)
    CCPoint CCScrollView::maxContainerOffset()
    {
        // Default CCPointZero;
        return ccp(0.0f, m_pContainer->getContentSize().height*m_pContainer->getScaleY() - m_tViewSize.height);
    }
    CCPoint CCScrollView::minContainerOffset()
    {
        // Default Y = m_tViewSize.height - m_pContainer->getContentSize().height*m_pContainer->getScaleY();
        return ccp(m_tViewSize.width - m_pContainer->getContentSize().width*m_pContainer->getScaleX(), 
               0.f);
    }
    */
    
    Run Code Online (Sandbox Code Playgroud)
  2. 一个较少侵入 的黑客,是规模滚动型实例-1和孩子们的的容器节点也为-1.您还必须考虑反比例重新定位子节点.两个级别的缩放结果是直接(不翻转)内容(子节点).将ScrollView缩放为-1 的结果是滚动在预期的方向上发生.请注意,虽然此"修复"也会在X轴上翻转滚动,因此适用于您希望垂直滚动(CCScrollViewDirectionVertical)的情况.

    CCSize winSize = CCDirector::sharedDirector()->getWinSize();
    CCLayer* scrollContainer = CCLayer::create(); // Container for the scroll view
    scrollContainer->setAnchorPoint(CCPointZero); // CCScrollView does this too when it's set as the container.
    
    // Content for the container
    CCSprite *tallContentA = CCSprite::create("TallContentA.png");
    tallContentA ->setPosition(ccp(winSize.width*0.5f, winSize.height*0.9f));
    CCSprite *tallContentB = CCSprite::create("TallContentB.png");
    tallContentB ->setPosition(ccp(winSize.width*0.5f, winSize.height*0.1f));
    scrollContainer->addChild(tallContentA, 2);
    scrollContainer->addChild(tallContentB, 2);
    
    float scrollContainerHeight = tallContentA->getContentSize().height + tallContentB->getContentSize().height;
    scrollContainer->setPosition(CCPointZero);
    scrollContainer->setContentSize(CCSizeMake(winSize.width, scrollContainerHeight*1.05f));
    
    // Set up scroll view
    CCScrollView* scrollView = CCScrollView::create(winSize, scrollContainer);
    scrollView->setPosition(CCPointZero);
    scrollView->setDirection(CCScrollViewDirectionVertical);
    // ScrollView initializes at the (left, bottom). The container also gets positioned relative to that and goes Y-up.
    // Pre-set it to the value CCScrollView::minContainerOffset will return when it's scrolled to the top
    // (note, this is a negative number, indicating the touch moving downwards, i.e. it's pre-scrolled such that the top of the content is visible when we begin)
    scrollView->setContentOffset(ccp(0.f, (winSize.height-scrollContainerHeight*1.05f)), false);
    /*
    // (StackOverflow Post Edit: This hack is not required.)
    // Hack: CCScrollView's maxContainerOffset is (0, 0) and minContainerOffset is (difference between view and content size which is negative)
    // It's designed to be (left, bottom) based and positive scrolling means showing stuff above the top of the screen.
    // Since we're using it in terms of Window coordinates ((left, top) based), we scale the scroll view
    // and it's container's children by -1 and position the children differently
    // (eg. Y position winSize.height*0.1f was changed to winSize.height*0.9f)
    // We can't just set the scroll view's Y scale to -1 because CCNode::getScale asserts that X and Y scale must be the same.
    scrollView->setScale(-1.f);
    tallContentA->setScale(-1.f);
    tallContentB->setScale(-1.f);
    */
    
    addChild(scrollView);
    
    Run Code Online (Sandbox Code Playgroud)

还要注意的是,上述两个定位点是相互排斥的,也不能适用两种.

现在在cocos2d-x论坛上回答.