类似于iphone的主桌面,下面有一排小圆点的指示器,指示有几页,当前第几页。
ScrollView是继承CCLayer,我们直接把指示器加到这里面试试。
先加小圆点的图片进去。
HelloWorld::init方法
this->addChild(_scrollView); CCSprite *pointer2 = CCSprite::create("pointer.png"); pointer2->setPosition(ccp(VisibleRect::center().x,200)); _scrollView->addChild(pointer2, 10); CCSprite *pointer1 = CCSprite::create("pointer.png"); pointer1->setPosition(ccp(VisibleRect::center().x-30,200)); _scrollView->addChild(pointer1, 10); CCSprite *pointer3 = CCSprite::create("pointer.png"); pointer3->setPosition(ccp(VisibleRect::center().x+30,200)); _scrollView->addChild(pointer3, 10); return true;
发现上面的做法不行,小圆点跟着拖动走了....否决!!
那就把小圆点加到外面吧~~
_scrollView = MyScrollView::create(CCSizeMake(640, 1136)); //_scrollView->setContentSize(); _scrollView->setDelegate(this); _scrollView->setTouchPriority(1); _scrollView->setContainer(container); _scrollView->setDirection(kCCScrollViewDirectionHorizontal); CCSprite *pointer1 = CCSprite::create("pointer_selected.png"); pointer1->setPosition(ccp(VisibleRect::center().x-30,200)); this->addChild(pointer1, 10,101); this->addChild(_scrollView); CCSprite *pointer2 = CCSprite::create("pointer.png"); pointer2->setPosition(ccp(VisibleRect::center().x,200)); this->addChild(pointer2, 10,102); CCSprite *pointer3 = CCSprite::create("pointer.png"); pointer3->setPosition(ccp(VisibleRect::center().x+30,200)); this->addChild(pointer3, 10,103); return true;
void HelloWorld::scrollViewDidScroll(CCScrollView* view){ int offset = (int)view->getContentOffset().x; CCTexture2D *texture = CCTextureCache::sharedTextureCache()->addImage("pointer_selected.png"); CCTexture2D *textureNor = CCTextureCache::sharedTextureCache()->addImage("pointer.png"); CCSprite *sp101 = (CCSprite*) this->getChildByTag(101); CCSprite *sp102 = (CCSprite*) this->getChildByTag(102); CCSprite *sp103 = (CCSprite*) this->getChildByTag(103); sp101->setTexture(textureNor); sp102->setTexture(textureNor); sp103->setTexture(textureNor); if(offset>-320){ sp101->setTexture(texture); }else if(offset<=-320&&offset>-960){ sp102->setTexture(texture); }else{ sp103->setTexture(texture); } CCLog("scrollViewDidScroll offset:%d",offset); }
见效果图
等有时间把以上这些封装总结一下吧。