CCScrollView returns 一个 children

CCScrollView returns one children

我正在研究 cocos2dx 3.0,并且正在研究 cocos2d::extension::ScrollView,遇到一个奇怪的问题,是滚动视图return 没有添加所有 节点 ,只是 return 一个 child,那也是一个不同的节点。请参阅下面的代码

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));
}

现在我正在做的每个单元格选择见下文

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);

}

chooseCharacterCallback 回调函数中,下面一行给出 children count as 1.

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

而令人惊讶的是,下面一行删除了所有 children.

scrollView->removeAllChildren();

这是一种不寻常的行为,可以帮助我摆脱这种情况吗?

谢谢。

滚动视图的唯一子视图是 "content node"。您不应该直接添加或获取子项 to/from 滚动视图,而是通过 contentNode 属性 添加内容节点并从该内容节点添加和获取子项。