Cocos2d-x3.0下实现循环列表

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

效果:

原文地址:http://blog.csdn.net/qqmcy/article/details/27393011

用法:能够使用创建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<std::string> imageAry;
Size itemSize;
float alphaOfobjs;

float heightOffset;
float positionRatio;

void addEventDJInifiteScrollPicker(Ref* sender,SEL_InfiniteScrollViewEvent type);

private:
std::vector<Sprite*> 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;
}

Cocos2d-x3.0下实现循环列表,布布扣,bubuko.com

时间: 2024-10-19 07:59:11

Cocos2d-x3.0下实现循环列表的相关文章

cocos2d 2.x在opengl es 2.0 下自定义着色器来创建特别酷的特效(译)

cocos2d 2.x在opengl es 2.0 下自定义着色器来创建特别酷的特效(译) (2012-12-24 13:22:17) 转载▼ 标签: it cocos2d opengl 着色器 渲染 翻译:弹涂鱼 PS:欢迎加入开发群:285275050 本文翻译自:http://www.raywenderlich.com/10862/how-to-create-cool-effects-with-custom-shaders-in-opengl-es-2-0-and-cocos2d-2-x#

《Python基础教程》 读书笔记 第五章(下)循环语句

5.5.1while循环 x=1 while x<=100: print x x+=1 确保用户输入了名字: name="" while not name: name=raw_input('please enter your name:') print 'hello,%s!'%name 5.5.2for循环 while语句非常灵活.它可以用来在任何条件为真的情况下重复执行一个代码块.一般情况下这样就够用了,但是有些时候还得量体裁衣.比如要为一个集合(序列和其他可迭代对象)的每个元

【CNMP系列】CentOS7.0下安装Nginx服务

话步前言,CNMP之路,系统起步:http://www.cnblogs.com/riverdubu/p/6425028.html 这回我来讲解下CentOS7.0下如何安装和配置Nginx服务 Nginx的历史不在此赘述,轻量,快是它的特性.只是因为现在的模块没有达到apache的模块数量级,未来有超越apache的势头. 首先,我们要安装个必要的软件(上节提到过,可能有人并未安装) #yum install wget 因为Nginx以来与gcc的编译环境,所以,在mini centos中需要安

【CNMP系列】CentOS7.0下安装PHP5.6.30服务

上一节我们讲过了如何在CentOS7.0下安装MySql服务,如果没有看到欢迎页面的朋友,可以加我的个人微信详聊:litao514148204 附上一节地址:http://www.cnblogs.com/riverdubu/p/6426852.html 关于php-fpm nginx本身不能处理PHP,它只是个web服务器,当接收到请求后,如果是php请求,则发给php解释器处理,并把结果返回给客户端. nginx一般是把请求发fastcgi管理进程处理,fascgi管理进程选择cgi子进程处理

输入一批整数,输出其中的最大值和最小值,输入数字0就结束循环。如下所示

import java.util.Scanner; /** * 输入一批整数,输出其中的最大值和最小值,输入数字0就结束循环.如*下所示 请输入一个整数(输入0结束):20 请输入一个整数(输入0结束):35 * 请输入一个整数(输入0结束):1 请输入一个整数(输入0结束):57 请输入一个整数(输入0结束):0 最大值是:57 最小值是:1 */ public class Max { public static void main(String[] args) { Scanner sc =

JS网页特效操作流程——下拉菜单列表与登录注册弹窗效果

下拉菜单列表 <style>        *{            margin: 0px;            padding: 0px;        }        .menu{            width: 1100px;            height: 30px;            background-image: url(img/魅力罗兰Music炫图18.jpg);            margin-left: 200px;            ma

iOS案例:读取指定目录下的文件列表

// // main.m // 读取指定目录下的文件列表 // // Created by Apple on 15/11/24. // Copyright © 2015年 Apple. All rights reserved. // /* *读取指定目录下的文件列表 */ #import <Foundation/Foundation.h> void myQuickMethod(); int main(int argc, const char * argv[]) { //文件操作对象 NSFil

windows和linux下结束循环的操作

#include <iostream> #include <string> #include <sstream> using namespace std; int main() { string word; string line; while (getline(cin,line)) { istringstream istr(line); while(istr>>word) cout<<word<<"#"; cou

神奇的输入 while(cin&gt;&gt;....)如何在遇见换行之后进入下一层循环读入

1 cin>>m>>n; 2 for(int i=1;i<=m;i++) { 4 int x=0; 5 char ch=' '; 6 while(ch!=10) //在遇到换行之后进入下一层循环读入. 7 { 8 x++; 9 cin>>c[x]; 10 ch=getchar(); 11 } 神奇的输入. get skill!