所有项目都出现在一个位置而不是滚动视图中的不同位置

时间:2015-07-22 07:06:11

标签: cocos2d-x

现在我在cocos2dx(2.2.6 c ++)中实现滚动视图  所有滚动视图项目只有一个位置。我想要所有的ietm与一些特定的填充,以便我给填充,但然后它也不能正常工作。我的滚动视图代码如下所示。在那里我创建CCMenuItemImage数组并在CCMenu中添加。我还添加abc->alignItemsHorizontallyWithPadding(100),然后也不考虑填充,所以我的代码中对此问题的更改是什么。

scrollview=cocos2d::extension::CCScrollView::create(CCSize(ccp(winsize.width/768*760, winsize.height/1024*550)),NULL);
// scrollview->setPosition(ccp(winsize.width/768*160,winsize.height/1024*1200));
scrollview->setPosition(ccp(winsize.width/768*0,winsize.height/1024*0));
scrollview->retain();
scrollview->setContentSize(CCSizeMake(slider->getContentSize().width+500,slider->getContentSize().height+250));
scrollview->setViewSize(ccp(winsize.width/768*724,winsize.height/1024*500));

scrollview->setContentOffset(ccp(winsize.width/768*350, winsize.height/1024*120));
scrollview->setDirection(cocos2d::extension::kCCScrollViewDirectionHorizontal);
scrollview->setContentOffsetInDuration(ccp(500,0), 0.5);

for (int i=1; i<10; i++) {
    CCString *str = CCString::createWithFormat("haircopy%d.png", i);
    flower_menuietm[i]=CCMenuItemImage::create(str->getCString(),NULL,this,menu_selector(HelloWorldScene::selectSprite));
    flower_menuietm[i]->setTag(i);
    //flower_menuietm[i]->setScale(0.9);

    abc=CCMenu::create(flower_menuietm[i],NULL);
    abc->alignItemsHorizontallyWithPadding(100);
    scrollview->addChild(abc);
}
this->addChild(scrollview);

1 个答案:

答案 0 :(得分:1)

您的问题是您为每个 CCMenuItem 创建单独的 CCMenu 要解决此问题,只需创建一个 CCMenu 并将所有 CCMenuItems 添加到其中;

...

abc = CCMenu::create();
scrollview->addChild(abc);
for (int i=1; i<10; i++) {
    CCString *str = CCString::createWithFormat("haircopy%d.png", i);
    flower_menuietm[i]=CCMenuItemImage::create(str->getCString(),NULL,this,menu_selector(HelloWorldScene::selectSprite));

    flower_menuietm[i]->setTag(i);
    //flower_menuietm[i]->setScale(0.9);
    abc->addChild(flower_menuietm[i]);
}
abc->alignItemsHorizontallyWithPadding(100);

this->addChild(scrollview);