博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Cocos2d-x3.0下实现循环列表
阅读量:6541 次
发布时间:2019-06-24

本文共 8053 字,大约阅读时间需要 26 分钟。

本文的实现是參照我之前在做iOS时实现的一个能够循环的列表这里用C++重写一遍。

效果:

用法:能够使用创建project时的HelloWorld測试

,h文件里加入循环列表的监听回调

void djInfiniteScroll(Ref* sender);

.cpp

#include "DJInfiniteScrollPicker.h"
DJInfiniteScrollPicker* picker = DJInfiniteScrollPicker::create();        picker->setSize(Size(500, 500));        picker->imageAry = set1;        picker->setPosition(Point(10, 100));        picker->setBackGroundColorType(cocos2d::ui::LAYOUT_COLOR_SOLID);        picker->setBackGroundColor(Color3B(128, 128, 128));        picker->initInfiniteScrollView();                addChild(picker);                picker->addEventDJInifiteScrollPicker(this, djInfinitescrollvieweventselector(TestScene::djInfiniteScroll));
实现类:

DJInfiniteScrollPicker.h

////  DJInfiniteScrollPicker.h//  maptest////  Created by 杜甲 on 14-5-27.////#ifndef __maptest__DJInfiniteScrollPicker__#define __maptest__DJInfiniteScrollPicker__#include "cocos2d.h"#include "ui/CocosGUI.h"#include "extensions/cocos-ext.h"USING_NS_CC;typedef enum{    sc = 0,    sc1}ScType;typedef void (Ref::*SEL_InfiniteScrollViewEvent)(Ref*);#define djInfinitescrollvieweventselector(_SELECTOR) (SEL_InfiniteScrollViewEvent)(&_SELECTOR)class DJInfiniteScrollPicker:public  ui::Layout,public extension::ScrollViewDelegate{    public:    CREATE_FUNC(DJInfiniteScrollPicker);    virtual bool init();        static DJInfiniteScrollPicker* create(Size wsize);    virtual bool initWithSize(Size wsize);        virtual void scrollViewDidScroll(extension::ScrollView* view);    /**     * @js NA     * @lua NA     */    virtual void scrollViewDidZoom(extension::ScrollView* view);        void initInfiniteScrollView();        extension::ScrollView* infiniteScrollView;        std::vector
imageAry; Size itemSize; float alphaOfobjs; float heightOffset; float positionRatio; void addEventDJInifiteScrollPicker(Ref* sender,SEL_InfiniteScrollViewEvent type); private: std::vector
imageStore; float beforePointX; std::string fontName; void reloadView(float offset); void snapToAnEmotion(); Sprite* currSprite; bool isFirstDrag; int currNum; Ref* _infiniteEventListener; SEL_InfiniteScrollViewEvent _infiniteEventSelector; };#endif /* defined(__maptest__DJInfiniteScrollPicker__) */
DJInfiniteScrollPicker.cpp

////  DJInfiniteScrollPicker.cpp//  maptest////  Created by 杜甲 on 14-5-27.////#include "DJInfiniteScrollPicker.h"bool DJInfiniteScrollPicker::init(){    bool bRet = false;    do {        CC_BREAK_IF(!ui::Layout::init());                bRet = true;    } while (0);    return bRet;}DJInfiniteScrollPicker* DJInfiniteScrollPicker::create(Size size){    DJInfiniteScrollPicker* pRet = new DJInfiniteScrollPicker();    if (pRet && pRet->initWithSize(size))    {        pRet->autorelease();    }    else    {        CC_SAFE_DELETE(pRet);    }    return pRet;}bool DJInfiniteScrollPicker::initWithSize(cocos2d::Size size){    bool bRet = false;    do {        CC_BREAK_IF(!ui::Layout::init());                                                      bRet = true;    } while (0);    return bRet;}void DJInfiniteScrollPicker::initInfiniteScrollView(){    isFirstDrag = false;    fontName = "TrebuchetMS-Bold";            alphaOfobjs = 1.0f;    heightOffset = 0.0f;    positionRatio = 1.0f;        beforePointX = 0.0f;        infiniteScrollView = extension::ScrollView::create(getSize());    infiniteScrollView->retain();    infiniteScrollView->setPosition(Point(0, 0));            infiniteScrollView->setBounceable(false);    infiniteScrollView->setDelegate(this);    //infiniteScrollView->setContentSize(get);    addChild(infiniteScrollView);       Sprite* temp = Sprite::create("res/set2/s2_1.png");//    log("anchor = %f",temp->getAnchorPoint().x);//    temp->setPosition(Point(temp->getContentSize().width / 2, temp->getContentSize().height / 2));//    // temp->setPosition(Point( itemSize.width, 0));//    infiniteScrollView->addChild(temp);            if (imageAry.size() > 0) {                itemSize  = temp->getContentSize();    }            //        if (imageAry.size() > 0) {        for (int i = 0; i < (imageAry.size() * 5); i++) {            //ui::Text* temp = ui::Text::create(StringUtils::format("%d",i % (int)imageAry.size()), fontName, 23);            Sprite* sprite = Sprite::create(imageAry.at(i % imageAry.size()));            sprite->setTag(i % imageAry.size());            sprite->setPosition(Point(i * itemSize.width + itemSize.width / 2, itemSize.height / 2));            imageStore.push_back(sprite);            infiniteScrollView->addChild(sprite);        }    }           infiniteScrollView->setContentSize(Size(imageAry.size() * 5 * itemSize.width, getSize().height));    float viewMiddle = imageAry.size() * 2 * itemSize.width;        infiniteScrollView->setContentOffset(Point(-viewMiddle , 0));        }void DJInfiniteScrollPicker::scrollViewDidScroll(extension::ScrollView *view){    float sectionSize = imageAry.size() * itemSize.width;    log("QQQQQQQQQQQQ%f,%f",infiniteScrollView->getContentOffset().x,beforePointX);                if (beforePointX != infiniteScrollView->getContentOffset().x)    {                if (infiniteScrollView->getContentOffset().x < 0)        {            if (infiniteScrollView->getContentOffset().x >= (-sectionSize + sectionSize / 2))            {                view->setContentOffset(Point(-sectionSize * 2 +sectionSize / 2, 0));            }            else if (view->getContentOffset().x <= (-sectionSize * 3 - sectionSize / 2))            {                view->setContentOffset(Point(-sectionSize * 2 - sectionSize / 2, 0));            }           // if (infiniteScrollView->isDragging()) {               // log("拖拽中");                 reloadView(view->getContentOffset().x - getSize().width / 2);         //   }                                }        else        {            view->setContentOffset(Point(-sectionSize * 2 + sectionSize / 2, 0));        }                        //推断是否被拖拽        if (!infiniteScrollView->isDragging()) {                        //推断是否是首次进入,首次不拖拽            if (isFirstDrag) {                beforePointX = -currSprite->getPosition().x + getSize().width / 2;                view->setContentOffset(Point(beforePointX, 0));                  //log("停止拖拽view->getContentOffset().xQQ = %f",view->getContentOffset().x);                if (_infiniteEventListener && _infiniteEventSelector) {                    (_infiniteEventListener->*_infiniteEventSelector)(currSprite);                                                        }                //                if (_scrollViewEventListener && _scrollViewEventSelector)//                {//                    (_scrollViewEventListener->*_scrollViewEventSelector)(this, SCROLLVIEW_EVENT_SCROLL_TO_TOP);//                }                            }                                    isFirstDrag = true;        }    }    log("view->getContentOffset().x = %f",view->getContentOffset().x);        beforePointX = view->getContentOffset().x;                  }void DJInfiniteScrollPicker::reloadView(float offset){       float biggestSize = 0.0f;    Ref* biggestView;    Sprite* sprite = imageStore.at(2);                for (int i = 0; i < imageStore.size(); i++)    {        Sprite* sprite = imageStore.at(i);                        if (-sprite->getPosition().x > (offset - itemSize.width / 2) && -sprite->getPosition().x < (offset + itemSize.width / 2)) {                        currSprite = sprite;            currNum = i;                                    sprite->setScale(2);                    }else        {            sprite->setScale(1);        }    }}void DJInfiniteScrollPicker::snapToAnEmotion(){        float offset = infiniteScrollView->getContentOffset().x - getSize().width / 2;            }void DJInfiniteScrollPicker::scrollViewDidZoom(extension::ScrollView *view){    }void DJInfiniteScrollPicker::addEventDJInifiteScrollPicker(cocos2d::Ref *sender, SEL_InfiniteScrollViewEvent type){    _infiniteEventListener = sender;    _infiniteEventSelector = type;}

你可能感兴趣的文章
python面试题(八)
查看>>
Vue 应用单元测试的策略与实践 01 - 前言
查看>>
老司机 iOS 周报 #5
查看>>
leetcode30 与所有单词相关联的字串
查看>>
Java泛型详解
查看>>
Smali —— 数学运算,条件判断,循环
查看>>
公司wifi安全
查看>>
木马盗用“风行播放器签名”流氓推广
查看>>
JQuery基础
查看>>
一起撸个朋友圈吧(step2) 数据结构(JSON结构)【上】篇
查看>>
看动画轻松理解时间复杂度(二)
查看>>
iOS 移动存储初探(一):SQLite
查看>>
【容器人必看】你一定要来 ECUG Con 2018 的三个理由!
查看>>
Spring Cloud(十)高可用的分布式配置中心 Spring Cloud Config 中使用 Refresh
查看>>
获取Android设备DeviceId与反Xposed Hook
查看>>
vue-cli3.x 新特性及踩坑记
查看>>
MaxCompute JOIN优化小结
查看>>
聊聊storm WindowTridentProcessor的FreshCollector
查看>>
EOSIO MySQL 插件 issue 2
查看>>
ConstraintLayout基础介绍
查看>>