• cocos2dx ScrollView 测试三 指示器


    类似于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);
    }

    见效果图

    等有时间把以上这些封装总结一下吧。

  • 相关阅读:
    hdu 5352 匈牙利(多重匹配)
    hdu 2112 最短路+stl
    hdu 1811 拓扑排序+并查集+细心
    hdu5407 CRB and Candies
    hdu1018 Big Number
    hdu5410 CRB and His Birthday
    hdu1023 Train Problem II
    hdu4812 D Tree
    hdu1085 Holding Bin-Laden Captive!
    hdu4810 Wall Painting
  • 原文地址:https://www.cnblogs.com/hanhongmin/p/3501899.html
Copyright © 2020-2023  润新知