Search code examples
cocos2d-xcocos2d-x-3.0ccscrollview

CCScrollView returns one children


I am working on cocos2dx 3.0, and working on cocos2d::extension::ScrollView, getting a strange problem, is the scrollview doesn't return all the Nodes added to it, just returns one child, that too a different one.. See my code below

void Characters::setupScroll(){
    Size winSize = Director::getInstance()->getWinSize();

    if(!scrollView){
        scrollView=cocos2d::extension::ScrollView::create(Size(winSize.width*1.0,winSize.height*0.8));
        scrollView->setDirection(cocos2d::extension::ScrollView::Direction::HORIZONTAL);
        scrollView->setPosition(Vec2(winSize.width*0.0,winSize.height*0.0));
        this->addChild(scrollView);

        scrollView->setClippingToBounds(true);
        scrollView->setBounceable(true);
        scrollView->setTouchEnabled(true);

    }

    float x=0.20;
    for(int i=0;i<4;i++){
        CharacterCell *cell=CharacterCell::createCell((kPlayer)i, CC_CALLBACK_1(Characters::chooseCharacterCallback, this));

        Vec2 pos=Vec2(winSize.width*x,winSize.height*0.3);
        cell->setPosition(pos);
        x=x+0.35;
        scrollView->addChild(cell);
        cell->setTag(100+i);
        cell->setScale(.9);

    }
    scrollView->setContentSize(Size(winSize.width*x,0.0));
}

Now on each cell selection I am doing see below

void Characters::chooseCharacterCallback(Ref *sender){
    CharacterCell *cell=(CharacterCell *)sender;

    //HERE I GET 1 AS CHILDREN COUNT
    printf("%zd",scrollView->getChildrenCount()); 


    Vector<Node*> cells=scrollView->getChildren();

    for(int i=0;i<scrollView->getChildrenCount();i++){
        CharacterCell *iCell=(CharacterCell *)cells.at(i);
        iCell->setScale(0.9);
    }

    auto scaleup=ScaleTo::create(.3, 1.15);
    auto ease=EaseBackIn::create(scaleup->clone());
    cell->runAction(ease);

}

In the chooseCharacterCallback callback function below line gives children count as 1.

printf("%zd",scrollView->getChildrenCount());

Whereas surprisingly below line removes all the children.

scrollView->removeAllChildren();

This is an unusual behaviour, can any help me to get out of this?

Thanks.


Solution

  • The scroll view's only child is the "content node". You should not be adding or getting children to/from the scroll view directly, instead add a content node via the contentNode property and add and get children from that content node.