CCScrollView练习
MyScrollItem是CCScrollView容器内项的接口,MyScrollView主要处理添加子节点和事件的处理,MyScrollViewTestItem是对MyScrollItem实现的测试项,MyScrollViewTest测试类
/*MyScrollView.h*/
#ifndef _MY_SCROLL_VIEW_
#define _MY_SCROLL_VIEW_
#include "cocos2d.h"
#include "CCScrollView.h"
#include "ExtensionMacros.h"
using namespace cocos2d;
USING_NS_CC_EXT;
class MyScrollItem : public CCNode
{
public:
bool isSelected(CCTouch* pTouch);
void touched();
virtual bool init(CCSize size);
virtual void selected()=;
};
class MyScrollView : public CCLayer,public CCScrollViewDelegate
{
private:
CCScrollView* m_pScrollView;
CCNode* m_pContainer;
CCSize m_itemSize;
bool m_isScrolling;
public:
MyScrollView();
static MyScrollView* create(CCPoint pos,CCSize viewSize,CCScrollViewDirection dir);
bool init(CCPoint pos,CCSize viewSize,CCScrollViewDirection dir);
void addItem(MyScrollItem* item);
virtual void registerWithTouchDispatcher();
virtual bool ccTouchBegan(CCTouch* pTouch,CCEvent* pEvent);
virtual void ccTouchMoved(CCTouch* pTouch,CCEvent* pEvent);
virtual void ccTouchEnded(CCTouch* pTouch,CCEvent* pEvent);
virtual void scrollViewDidScroll(CCScrollView* view);
virtual void scrollViewDidZoom(CCScrollView* view);
virtual void draw();
};
#endif /*MyScrollView.cpp*/ #include "cocos2d.h"
#include "CCScrollView.h"
#include "MyScrollView.h"
#include <stdio.h>
using namespace cocos2d;
using namespace cocos2d::extension; bool MyScrollItem::isSelected(CCTouch* pTouch)
{
CCPoint touch = this->convertTouchToNodeSpace(pTouch);
CCSize size = this->boundingBox().size; // boundingBox()返回的是当前节点经过缩放旋转后在本地坐标的矩形
return CCRect(,,size.width,size.height).containsPoint(touch);
}
bool MyScrollItem::init(CCSize size)
{
if(!CCNode::init()) return false;
this->setContentSize(size);
return true;
}
void MyScrollItem::touched()
{
this->stopAllActions();
CCScaleTo* scaleTo1 = CCScaleTo::create(0.1f,0.9f);
CCScaleTo* scaleTo2 = CCScaleTo::create(0.1f,1.0f);
this->runAction(CCSequence::create(scaleTo1,scaleTo2,NULL));
} MyScrollView::MyScrollView(){}
MyScrollView* MyScrollView::create(CCPoint pos,CCSize viewSize,CCScrollViewDirection dir)
{
MyScrollView* scrollview = new MyScrollView();
if(scrollview && scrollview->init(pos,viewSize,dir))
{
scrollview->autorelease();
return scrollview;
}
return NULL;
} bool MyScrollView::init(CCPoint pos,CCSize viewSize,CCScrollViewDirection dir)
{
if(!CCLayer::init()) return false;
m_pScrollView = CCScrollView::create(viewSize);
m_pContainer = CCNode::create();
m_pScrollView->setBounceable(false);//设置弹性效果
m_pScrollView->setContainer(m_pContainer);//设置容器
m_pScrollView->setTouchPriority(-);//设置优先级
m_pScrollView->setTouchEnabled(true);//设置可触摸
m_pScrollView->setDirection(dir); //设置方向
m_pScrollView->setDelegate(this); //设置代理 m_pScrollView->setPosition(ccp(,)); /*CCLayerColor* layer = CCLayerColor::create(ccc4(255,0,0,255),viewSize.width,viewSize.height);
layer->setPosition(pos);
this->addChild(layer,1,100);*/ this->addChild(m_pScrollView);
this->setTouchEnabled(true);
this->setPosition(pos);
return true;
}
void MyScrollView::registerWithTouchDispatcher()
{
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,,true);
}
void MyScrollView::addItem(MyScrollItem* item)
{
CCPoint pos;
int count = m_pContainer->getChildrenCount();
item->ignoreAnchorPointForPosition(false);
item->setAnchorPoint(ccp(0.5f,0.5f));
if(count)
{
CCNode* endItem = m_pContainer->getChildByTag(count-);
pos = ccp(endItem->getPositionX()+m_itemSize.width,endItem->getPositionY());
}
else
{
if(m_itemSize.width== || m_itemSize.height==) m_itemSize = item->getContentSize();
pos = ccp(m_itemSize.width/,m_itemSize.height/);
}
item->setPosition(pos);
m_pContainer->setContentSize(CCSizeMake(m_itemSize.width*(count+),m_itemSize.height));
m_pContainer->addChild(item,,count);
}
bool MyScrollView::ccTouchBegan(CCTouch* pTouch,CCEvent* pEvent)
{
m_isScrolling = false;
if(m_pContainer->getChildrenCount())
{
CCPoint touch = m_pContainer->convertTouchToNodeSpace(pTouch);
MyScrollItem* item = (MyScrollItem*)m_pContainer->getChildByTag(touch.x/m_itemSize.width);
if(item && item->isSelected(pTouch))
{
item->touched();
return true;
}
}
return false;
}
void MyScrollView::ccTouchMoved(CCTouch* pTouch,CCEvent* pEvent)
{
m_isScrolling = true;
}
void MyScrollView::ccTouchEnded(CCTouch* pTouch,CCEvent* pEvent)
{
if(!m_isScrolling)
{
CCPoint touch = m_pContainer->convertTouchToNodeSpace(pTouch);
MyScrollItem* item = (MyScrollItem*)m_pContainer->getChildByTag(touch.x/m_itemSize.width);
item->selected();
}
}
void MyScrollView::scrollViewDidScroll(CCScrollView* view)
{
char str[];
sprintf(str,"pos=(%d,%d)\n",m_pContainer->getPositionX(),m_pContainer->getPositionY());
OutputDebugString(str);
}
void MyScrollView::scrollViewDidZoom(CCScrollView* view)
{
}
void MyScrollView::draw()
{
CCLayer::draw();
//int count = m_pContainer->getChildrenCount();
//m_pContainer->setPosition(ccp(0,0));
}
/*ScrollViewTest.h*/
#ifndef _SCROLLVIEW_TEST_
#define _SCROLLVIEW_TEST_
#include "cocos2d.h"
USING_NS_CC;
#include"MyScrollView.h"
class MyScrollViewTestItem : public MyScrollItem
{
public:
static MyScrollViewTestItem* create(CCSize size);
virtual bool init(CCSize size);
virtual void selected();
};
class MyScrollViewTest : public CCLayer
{
public:
CREATE_FUNC(MyScrollViewTest);
bool init();
};
#endif
/*ScrollViewTest.cpp*/
#include "cocos2d.h"
#include "ScrollViewTest.h"
#include "MyScrollView.h"
#include <iostream>
using namespace std;
MyScrollViewTestItem* MyScrollViewTestItem::create(CCSize size)
{
MyScrollViewTestItem* item = new MyScrollViewTestItem();
if(item && item->init(size))
{
item->autorelease();
return item;
}
CC_SAFE_DELETE(item);
return NULL;
}
bool MyScrollViewTestItem::init(CCSize size)
{
if(!MyScrollItem::init(size)) return false;
CCSprite* sprite = CCSprite::create("1.png");
sprite->setPosition(ccp(size.width/,size.height/));
this->addChild(sprite);
return true;
}
void MyScrollViewTestItem::selected()
{
char str[];
sprintf(str,"selected tag = %d\n",this->getTag());
OutputDebugString(str);
}
bool MyScrollViewTest::init()
{
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
CCSize viewSize = CCSizeMake(,);
CCPoint pos = ccp(,);
CCSize itemSize = CCSizeMake(,);
MyScrollView* myScroll = MyScrollView::create(ccp(,),viewSize,kCCScrollViewDirectionHorizontal);
for(int i=;i<;i++)
{
MyScrollViewTestItem* item = MyScrollViewTestItem::create(itemSize);
myScroll->addItem(item);
}
this->addChild(myScroll);
this->setPosition(pos);
return true;
}
/*添加几个cocos2dx函数源码*/
void CCScrollView::addChild(CCNode * child, int zOrder, int tag)
{
child->ignoreAnchorPointForPosition(false);
child->setAnchorPoint(ccp(0.0f, 0.0f));
if (m_pContainer != child) {
m_pContainer->addChild(child, zOrder, tag);
} else {
CCLayer::addChild(child, zOrder, tag);
}
}
由此可以看出CCScrollView中只有m_pContainer一个子节点,向CCScrollView中添加节点也就是向m_pContainer节点。
void CCScrollView::setContentSize(const CCSize & size)
{
if (this->getContainer() != NULL)
{
this->getContainer()->setContentSize(size);
this->updateInset();
}
}
由此看出CCScrollView设置内容大小其实就是设置容器大小
void CCScrollView::setContainer(CCNode * pContainer)
{
this->removeAllChildrenWithCleanup(true);
if (!pContainer) return;
this->m_pContainer = pContainer;
this->m_pContainer->ignoreAnchorPointForPosition(false);
this->m_pContainer->setAnchorPoint(ccp(0.0f, 0.0f));
this->addChild(this->m_pContainer);
this->setViewSize(this->m_tViewSize);
}
由此看出CCScrollView添加容器时删除了自己的其他所有子节点
void CCScrollView::setViewSize(CCSize size)
{
m_tViewSize = size;
CCLayer::setContentSize(size);
}
setViewSize才是设置CCScrollView本身的大小
CCScrollView练习的更多相关文章
- 【Cocos2d-x for WP8 学习整理】(3)CCScrollView 实现捕鱼达人一样的场景选择界面
UI 界面一般是游戏里比较独立的地方,因为游戏引擎一般都比较注意基础的功能封装,很少会关注UI,但是 UI 确是玩家第一眼看到的效果,因此能否实现一个美观的UI对于提升游戏的整体美观有着很大的帮助. ...
- cocos2d-x CCScrollView和CCTableView的使用(转载)
转载请注明来自:Alex Zhou的程序世界,本文链接:http://codingnow.cn/cocos2d-x/1024.html //============================== ...
- cocos2dx 2.0 CCScrollView的用法以及滑动的原理
#ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H__ #include "cocos2d.h" USING_N ...
- [cocos2dx]让CCScrollView支持分页
[cocos2dx]让CCScrollView支持分页 做过IOS开发的朋友, 肯定知道UIScrollView有一个isPaged属性. 当设置其为true的时候, 滑动会自动分页. 即, 每次滑动 ...
- CCScrollView/CCTableView(CCTableViewDelegate CCTableViewDataSource CCTableView-滑动列表-游戏中大量使用 非常重要的一个类)
tableview scrollViewDidScroll函数中有一段 ---- 即---滑动tableview时触发的函数 : 会将全部显示的cell又一次刷新(刷新函数中调用了自己定义的ta ...
- CCScrollView 实现帮助界面、关卡选择
本文出自[无间落叶]:http://blog.leafsoar.com/archives/2013/07-27.html 本文介绍了 CCScrollView 来编写帮助界面和关卡选择界面的方法,在编 ...
- cocos2d-x CCScrollView
转自:http://www.cnblogs.com/dcxing/archive/2012/12/31/2840217.html ScrollView一般用在游戏的关卡选择这种类似的场景还有帮助这种场 ...
- Cocos2d-x滚动列表具体解释(CCScrollView的使用)
今天要写一个滚动列表功能,类似以下这样.(图片资源都是自己从天天酷跑里面抠的,仅用于学习方便) 首先,这样一个列表就和iOS里面的UITableView没什么两样,当然,Android中肯定也存在类似 ...
- Cocos2d—X游戏开发之CCScrollView(滑动视图)(十二)
CCScrollView在Cocos2d-X引擎中主要使用在图片尺寸远大于屏幕尺寸的时候使用. 总体来说,使用起来比较简单. 一个是CCScrollView控件本身,一个是CCScrollViewDe ...
- cocos2d-x中的CCScrollView滑动体验不佳
在最近的项目中,使用了Cocos2d-x (2.2.0版本)提供的CCScrollView来拖动一个比较大的画面,但是发现滑动体验非常不佳, 手指离开屏幕后,滑动没有惯性,一个不算太大的画面,要滑动好 ...
随机推荐
- SPUtils
public class SPUtils { /** * 保存在手机里的SP文件名 */ public static final String FILE_NAME = "my_sp" ...
- Java运算符,位运算
注意:位运算符针对整数的补码进行运算,所以运算结果也是补码 &(与运算) 将数据转化为补码形式,然后将0看作false,将1看作true,按位进行与运算,最后将结果转化为十进制来显示 ...
- Maven构建项目速度太慢的解决办法 Maven 调试
Apache Maven是当今非常流行的项目构建和管理工具,它把开发人员从繁杂的项目依赖关系处理事务中解放出来,完全自动化管理依赖问题.在Web应用开发过程中,通常我们会用到maven的archety ...
- .NET单点登录实现方法----两种
第一种模式:同一顶级域名下cookie共享,代码如下 HttpCookie cookies = new HttpCookie("Token"); cookies.Expires = ...
- MongoDB day02
1.非关系型数据库和关系型数据库比较 1. 不是以关系模型构建的,结构自由 2. 非关系型数据库不保证数据的一致性 3. 非关系型数据库可以在处理高并发和海量数据时弥补关系型数据库的不足 4. 非关系 ...
- redis的二种启动方式
.直接启动 进入redis根目录,执行命令: #加上‘&’号使redis以后台程序方式运行 1 ./redis-server & 2.通过指定配置文件启动 可以为redis服务启 ...
- 「小程序JAVA实战」小程序的flex布局(22)
转自:https://idig8.com/2018/08/09/xiaochengxu-chuji-22/ 之前已经把小程序的框架说完了,接下来说说小程序的组件,在说组件之前,先说说布局吧.源码:ht ...
- qrcode——js生成二维码
1.引入 qrcode.min.js(点击下载demo) 2.html: <div id="qrcode"></div> js: var qrcode = ...
- 【源码阅读】Java集合之一 - ArrayList源码深度解读
Java 源码阅读的第一步是Collection框架源码,这也是面试基础中的基础: 针对Collection的源码阅读写一个系列的文章,从ArrayList开始第一篇. ---@pdai JDK版本 ...
- Mysql Windows 7 异常关闭, 2003 - Can't connect to Mysql server on 'localhost' (10061) "Unknown error")
如下: 按Win+R在窗口输入services.msc 启动mysql服务