cocos2d-x的初步學習十之CCScrollview

這篇文章中,咱們簡單講下CCScrollview,顧名思義,Scrollview,滾動視圖,不管在android,ios,黑莓上都有這個滾動視圖。那咱們來看下cocos2dx中的CCScrollview,我這個版本的cocos2dx是2.0.4版本的,貌似前幾個版本都有點不同。OK,下面我直接上代碼:node

在頭文件中.h:android

 

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;

    
};

稍微解釋下,首頁繼承CCScrollViewDelegate這個類,裏面有兩個純虛函數,重寫這兩個方法,一個是滾動的,一個是縮放的,定義一個ccscrollview這樣一個對象。ios

看下.cpp:git

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

    //獲取窗口大小
    CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
    CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
    
    //建立一個層
    CCLayer *slayer=CCLayer::create();
    
    for (int i=1; i<5; i++) {
        //背景
        CCSprite *bgsprite = CCSprite::create("deskBG@2x.png");
        bgsprite->setPosition(ccp(visibleSize.width * (i-0.5f), visibleSize.height / 2));
        slayer->addChild(bgsprite,1);

        CCString *nameString=CCString::createWithFormat("sprite%d@2x.png",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本身寫touch事件
    showScrollView->setTouchEnabled(true);
    showScrollView->setDelegate(this);

    //滾動方向
    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);

    //建立pagecontrol
    for (int i=1; i<5; i++) {
                
        CCSprite *pageControlSprite=CCSprite::create("regitChoose@2x.png");
        
        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);
        
    }
    
    //默認是第一頁選中
    CCSprite *selectedSrite = (CCSprite *)this->getChildByTag(101);
    
    //換貼圖
    CCTexture2D *aTexture =CCTextureCache::sharedTextureCache()->addImage("regitUnchoose@2x.png");
    
    selectedSrite->setTexture(aTexture);

    
    
    curPage=1;
    
    //觸摸分發
  //  CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 1, false);
    
    
    return true;
}

回調函數:iphone

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("regitUnchoose@2x.png");
            
            selectedSrite->setTexture(aTexture);
            
        }
        else
        {
        
        
            CCSprite *selectedSrite = (CCSprite *)this->getChildByTag(100+i);
            
            CCTexture2D *aTexture =CCTextureCache::sharedTextureCache()->addImage("regitChoose@2x.png");
            
            selectedSrite->setTexture(aTexture);

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


}


void HelloWorld::scrollViewDidZoom(CCScrollView *view)
{




    



}

OK,就這麼簡單,貌似ccscrollview沒有按一頁一頁那樣的滑動,哪一個童鞋知道的能夠告訴我。。。O(∩_∩)O,看效果:函數

~~~~~~this

csdn博客地址:http://blog.csdn.net/kuloveyouweispa

相關文章
相關標籤/搜索