A preliminary study of cocos2d-x ten CCScrollview

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

In this article, we simply say CCScrollview, as its name suggests, Scrollview, scroll view, whether in Android, IOS, BlackBerry has this scroll view. We see it in cocos2dx CCScrollview, this version of the cocos2dx version is 2.0.4, as in previous versions are a bit different. OK, I direct code below.:

In the header file.H:


class HelloWorld : public cocos2d::CCLayer, public cocos2d::extension::CCScrollViewDelegate
{
public:
    // Method 'init' in cocos2d-x returns bool, instead of 'id' in cocos2d-iphone (an object pointer)
    virtual bool init();

    // there's no 'id' in cpp, so we recommend to return the class instance pointer
    static cocos2d::CCScene* scene();
    
    // a selector callback
    void menuCloseCallback(CCObject* pSender);

    // preprocessor macro for "static create()" constructor ( node() deprecated )
    CREATE_FUNC(HelloWorld);
    
    
public:
    
    void scrollViewDidScroll(cocos2d::extension::CCScrollView *view);
    
    void scrollViewDidZoom(cocos2d::extension::CCScrollView *view);

private:


    cocos2d::extension::CCScrollView *showScrollView;
    
    int curPage;

    
};

Explain a little bit, the inheritance of the CCScrollViewDelegate class, there are two pure virtual functions, rewrite these two methods, one is rolling, one is China ", the definition of a ccscrollview such an object.

See.Cpp:

bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !CCLayer::init() )
    {
        return false;
    }

    //Access to window size
    CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
    CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
    
    //Create a new layer
    CCLayer *slayer=CCLayer::create();
    
    for (int i=1; i<5; i++) {
        //Background
        CCSprite *bgsprite = CCSprite::create("");
        bgsprite->setPosition(ccp(visibleSize.width * (i-0.5f), visibleSize.height / 2));
        slayer->addChild(bgsprite,1);

        CCString *nameString=CCString::createWithFormat("sprite%",i);
        
        CCSprite *sprite = CCSprite::create(nameString->getCString());
        sprite->setPosition(ccp(visibleSize.width * (i-0.5f), visibleSize.height / 2));
        slayer->addChild(sprite,1);
    
    
    }
    
       
    
    showScrollView = CCScrollView::create(CCSizeMake(visibleSize.width, visibleSize.height), slayer);
    showScrollView->setContentOffset(CCPointZero);
    //False write touch events
    showScrollView->setTouchEnabled(true);
    showScrollView->setDelegate(this);

    //The rolling direction
    showScrollView->setDirection(kCCScrollViewDirectionHorizontal);

    showScrollView->setBounceable(true);
  //  slayer->setContentSize(CCSizeMake(visibleSize.width*3, visibleSize.height));
    showScrollView->setContentSize(CCSizeMake(visibleSize.width*4, visibleSize.height));
    
    this->addChild(showScrollView,1);

    //Create pagecontrol
    for (int i=1; i<5; i++) {
                
        CCSprite *pageControlSprite=CCSprite::create("");
        
        pageControlSprite->setPosition(ccp( origin.x + (visibleSize.width - 4 * pageControlSprite->getContentSize().width)/2 + pageControlSprite->getContentSize().width * (i-1), origin.y + 30));
        
        pageControlSprite->setTag(100+i);
        
        this->addChild(pageControlSprite, 1);
        
    }
    
    //The default is the first page of the selected
    CCSprite *selectedSrite = (CCSprite *)this->getChildByTag(101);
    
    //Change texture
    CCTexture2D *aTexture =CCTextureCache::sharedTextureCache()->addImage("");
    
    selectedSrite->setTexture(aTexture);

    
    
    curPage=1;
    
    //Touch distribution
  //  CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 1, false);
    
    
    return true;
}

The callback function:

void HelloWorld::scrollViewDidScroll(CCScrollView *view)
{

     CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
    
     curPage=(int)fabsf(showScrollView->getContentOffset().x/visibleSize.width)+1;
    

     for (int i=1; i<5; i++) {
        
        if (curPage==i) {
            
            
            CCSprite *selectedSrite = (CCSprite *)this->getChildByTag(100+curPage);
            
            CCTexture2D *aTexture =CCTextureCache::sharedTextureCache()->addImage("");
            
            selectedSrite->setTexture(aTexture);
            
        }
        else
        {
        
        
            CCSprite *selectedSrite = (CCSprite *)this->getChildByTag(100+i);
            
            CCTexture2D *aTexture =CCTextureCache::sharedTextureCache()->addImage("");
            
            selectedSrite->setTexture(aTexture);

        
        
        }
        
        
        
        
    }
    
    
    
  //  CCLOG("%d",curPage);
    
    
    


}


void HelloWorld::scrollViewDidZoom(CCScrollView *view)
{




    



}

OK, So simple, just like ccscrollview did not press a page and a page that slide, which children know that I can tell... O (∩ _∩ O), see the effect:

~~~~~~

The CSDN blog.

 

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Jennie at November 24, 2013 - 8:36 PM