I had my share of problems with CCScrollView, I think there’s still room for improvement but here is a working example with a fixed menu button.
What I do here is scale an image with info’s on it to the full width of the device and then make it scrollable (vertically). Its still not perfect some adjustments have to be made to the boundaries but works as intended 🙂
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
#ifndef INFOSCENE_H_ #define INFOSCENE_H_ #include "cocos2d.h" #include "cocos-ext.h" using namespace cocos2d; using namespace extension; class InfoScene : public CCLayer { public: CCScrollView *scrollView; CCSprite *background; CCLayer *scrollContainer; bool init(); static CCScene* scene(); void menuCallBack(CCObject *sender); virtual void keyBackClicked(); CREATE_FUNC(InfoScene); InfoScene(); virtual ~InfoScene(); }; #endif /* INFOSCENE_H_ */ |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
#include "InfoScene.h" #include "globals.h" bool InfoScene::init(){ if(!CCLayer::init()){ return false; } //SETUP SCROLL CONTAINER CCSize winSize = CCDirector::sharedDirector()->getWinSize(); scrollContainer = CCLayer::create(); scrollContainer->setAnchorPoint(CCPointZero); background = CCSprite::create("info.png"); background->setScale(winSize.width/background->getContentSize().width); background->setPosition(ccp(winSize.width/2 , winSize.height - scm->Y_ORIGIN)); scrollContainer->addChild(background); scrollContainer->setPosition(CCPointZero); CCSize csize = CCSizeMake(background->getScale() * background->getContentSize().width, background->getScale() * background->getContentSize().height); scrollContainer->setContentSize(csize); //SETUP SCROLL VIEW scrollView = CCScrollView::create(winSize, scrollContainer); scrollView->setPosition(CCPointZero); scrollView->setDirection(kCCScrollViewDirectionVertical); scrollView->setContentOffset(ccp(0.f, (winSize.height - csize.height)), false); //SETUP MENU CCSprite *btn1 = CCSprite::create("button.png"); CCSprite *btn2 = CCSprite::create("button.png"); CCMenuItemSprite *button2 = CCMenuItemSprite::create(btn1, btn2, this, menu_selector(InfoScene::menuCallBack) ); button2->setTag(BACK_BUTTON); // MENU CCMenu* startMenu = CCMenu::create( button2, NULL); startMenu->alignItemsHorizontallyWithPadding(winSize.width/13);// this is optional startMenu->setPosition( ccp(200, 200) ); // just a random position, you should place it where you want it this->addChild(startMenu,100); scrollContainer->retain(); addChild(scrollView); return true; } void InfoScene::menuCallBack(CCObject* sender){ CCMenuItem* pMenuItem = (CCMenuItem *)(sender); int tag = (int)pMenuItem->getTag(); switch(tag){ case BACK_BUTTON: break; default: break; } } void InfoScene::keyBackClicked(){ } CCScene* InfoScene::scene(){ CCScene *scene = CCScene::create(); InfoScene *layer = InfoScene::create(); scene->addChild(layer); return scene; } InfoScene::InfoScene() { setKeypadEnabled(true); } InfoScene::~InfoScene() { scrollContainer->release(); } |