• cocos2d-x的初步学习十之CCScrollview


    这篇文章中,我们简单讲下CCScrollview,顾名思义,Scrollview,滚动视图,无论在android,ios,黑莓上都有这个滚动视图。那我们来看下cocos2dx中的CCScrollview,我这个版本的cocos2dx是2.0.4版本的,貌似前几个版本都有点不一样。OK,下面我直接上代码:

    在头文件中.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;
    
        
    };
    

    稍微解释下,首页继承CCScrollViewDelegate这个类,里面有两个纯虚函数,重写这两个方法,一个是滚动的,一个是缩放的,定义一个ccscrollview这样一个对象。

    看下.cpp:

    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;
    }
    

    回调函数:

    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,看效果:

    ~~~~~~

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

     

  • 相关阅读:
    HTML的基本知识
    java script后续
    java script
    CSS
    DAY 33 进程理论与进程方法
    DAY 32 UDP协议、Socketserver模块,并发编程基础
    DAY 30 网络编程基础
    DAY 25多态、反射、异常处理
    DAY 24继承与组合
    DAY 23 面向对象(二)
  • 原文地址:https://www.cnblogs.com/henrendadi/p/3163135.html
Copyright © 2020-2023  润新知