ScrollView within ScrollView

  • BuzzHsu
    Likes 0

    Problem Description

    Hello,  I’m new to cocos2d, and I’m using it to build an app. The app is a bit like google play stroe, and I want to use a ui::ScrollView in a ui::ScrollView layer so I can display picture items.

    The GameInfoLayer is inherit from ui::ScrollView, its vertical scroll, and the another scrollView inside this layer is horizontal, which I use to show pictures. The problems are, the layer is strangely colored, I can’t set its background color, one of my picture(which is sibling to the child scrollView) is not shown, and the child scrollView couldn’t clip images when they’re out of bound(despite I use setClippingEnabled()).

    bool GameInfoLayer::init()
    {
      if (!ScrollView::init())
        return false;
    
      ignoreAnchorPointForPosition(false);
      setAnchorPoint(Vec2::ANCHOR_BOTTOM_LEFT);
      setContentSize(Size(640, dwLogicalHeight - 72 - 96));		//dwLogicalHeight = 1130
      setPosition(Vec2(0,96));
      setInnerContainerSize(innerContainerSize);			//innerContainerSize = (640, dwLogicalHeight*2)
      setBounceEnabled(true);
    
    	//add some stuff here
    
      auto nameDatabox = Sprite::create("Others/Others/MyName.png");
      AH::anchorAddChild(nameDatabox, MIDDLE_UP, this->getInnerContainer(), MIDDLE_UP, Vec2(0, 28));	//just a function to align and do this->getInnerContainer->addChild(nameDatabox)
      //nameDatabox->setLocalZOrder(20);
    
      
      string PNGHead = "Image/" + gameImg + ".bpg";		//gameImg is file name
      auto headImage = Sprite::create(PNGHead);
      headImage->setScale(120.f / 150.f);
      AH::anchorAddChild(headImage, LEFT_MIDDLE, nameDatabox, LEFT_MIDDLE, Vec2(10, 0));
    
      
      auto scrollView = ui::ScrollView::create();
      scrollView->setDirection(ui::ScrollView::Direction::HORIZONTAL);
      scrollView->setBackGroundImageScale9Enabled(true);
      //scrollView->ignoreAnchorPointForPosition(false);
      scrollView->setBackGroundImage("Button.png");
      scrollView->setBackGroundColorType(ui::ScrollView::BackGroundColorType::NONE);
      scrollView->setContentSize(Size(dwWidth - 20, imgHeight + 20));
      scrollView->setAnchorPoint(Vec2(0.5, 0.5));
      auto innerContainerSize = Size((IMG_SCROLL_PADDING + (IMG_SCROLL_PADDING+imgWidth)*imgNum), imgHeight);
      scrollView->setInnerContainerSize(innerContainerSize);
      scrollView->setBounceEnabled(true);
      scrollView->setScrollBarEnabled(false);
    
    
      for (int i = 0; i < imgNum; i++)
      {
        //add other images here
      }
      scrollView->setClippingEnabled(true);
      AH::addChildWithRelPos(this->getInnerContainer() ,scrollView, MIDDLE_UP, nameDatabox, MIDDLE_BOTTOM, Vec2(0, 20)); //a fuction that align and do this->getInnerContainer()->addChild(scrollView)
      //scrollView->setLocalZOrder(0);			//I tried to adjust the zOrder, but couldn't solve the problem
    
      return true;
    }

    Scene

    bool GameInfoScene::init()
    {
      if (!BasicScene_2::init())
        return false;
    
      //layer
      auto layer = GameInfoLayer::create(gameID);
      addChild(layer);
      return true;
    }
    
    GameInfoScene* GameInfoScene::create(int gameID)
    {
      GameInfoScene *pRet = new(std::nothrow) GameInfoScene();
      if (pRet)
      {
        pRet->gameID = gameID;
        if (!pRet->init())
        {
          delete pRet;
          pRet = NULL;
          return NULL;
        }
        pRet->autorelease();
        return pRet;
      }
      else
      {
        delete pRet;
        pRet = NULL;
        return NULL;
      }
    }

     

  • Sonar Systems admin
    Likes 0

    Could you show us a screenshot

  • BuzzHsu
    Likes 0

    The problem only exists in Win32 project, with Visual Studio 2013 debugger. The compiled apk works just as I expected on android phone.


  • Sonar Systems admin
    Likes 0

    Try exporting the win32 app and trying it that way.

  • BuzzHsu
    Likes 1

    I found the solution in http://discuss.cocos2d-x.org/t/problems-with-uiscrollview-green-background-and-no-clipping/15860/7 ,

    in AppDelegate.h add a method override:

    void initGLContextAttrs();
    

    in AppDelegate.cpp add a method body:

    void AppDelegate::initGLContextAttrs()
    {
        GLContextAttrs glContextAttrs = {8, 8, 8, 8, 24, 8};
    
        GLView::setGLContextAttrs(glContextAttrs);
    }

    It’s all good now! Thank you!smiley


    This reply has been verified.
  • Sonar Systems admin
    Likes 0

    Great to hear :D

Login to reply