今天测试CCScrollView,单独创建并添加到layer上后工作正常,如果这时修改所在layer的position位置,CCScrollView在layer上的绘制感觉出现了偏移,绘制显示和触屏的滑动区域出现了偏移,不知道是不是设置问题?请问有那位有经验的朋友能告知一二吗,谢谢!
我发现底层CCScrollView.cpp里面似乎有两个BUG,493行beforeDraw方法里的CCPoint screenPos = this->convertToWorldSpace(this->getParent()->getPosition());这句转化坐标有问题,ccTouchBegan方法里面592行判定触摸区域的时候,frame = CCRectMake(this->getPosition().x, this->getPosition().y, m_tViewSize.width, m_tViewSize.height);这个区域的起始坐标点有问题,没考虑父容器的坐标偏移.
想问下同志,CCScrollView如何通过手指滑动到下一页的,希望你能把应用例子发来看看。
我想通过CCScrollView做成一个技能列表,通过滑动选择。
你的这个需求貌似CCListView可以满足。我这有个网上有朋友给出的1个示例。
h文件:#ifndef ListViewDemo_ListViewDemoLayer_h
#define ListViewDemo_ListViewDemoLayer_h
#include
#include <string.h>
#include “cocos2d.h”
#include “extensionsCCListViewCCListView.h”
USING_NS_CC;
using namespace cocos2d::extension;
class ListViewDemoLayer: public CCLayer, public CCListViewDelegate
{
private:
CCLabelTTF *m_InfoLabel;
private:
// 存放的List数据
std::liststd::string *m_pDataList;
CCListView *m_pListView;
bool m_bFresh;
public:
ListViewDemoLayer();
~ListViewDemoLayer();
virtual bool init();
LAYER_CREATE_FUNC(ListViewDemoLayer);
virtual void visit();
public:
// 继承自CCListViewDelegate所需要实现的方法
virtual void CCListView_numberOfCells(CCListView *listView, CCListViewProtrolData *data);
virtual void CCListView_cellForRow(CCListView *listView, CCListViewProtrolData *data);
virtual void CCListView_didClickCellAtRow(CCListView *listView, CCListViewProtrolData *data);
virtual void CCListView_didScrollToRow(CCListView *listView, CCListViewProtrolData *data);
};
#endif
cpp文件:
#include “ListViewDemoLayer.h”
#include “extensionsCCListViewCCListViewCell.h”
using namespace cocos2d::extension;
ListViewDemoLayer::ListViewDemoLayer()
{
m_bFresh = true;
}
ListViewDemoLayer::~ListViewDemoLayer()
{
}
bool ListViewDemoLayer::init()
{
bool bRet = false;
do {
CC_BREAK_IF(!CCLayer::init());
// 初始化List数据
m_pDataList = new std::list<std::string>;
for (int i=0; i<15; i++) {
char info;
sprintf(info, "Cell %d", i);
m_pDataList->push_back(info);
}
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
// 初始化控件ListView
CCListView *listView = CCListView::create(CCListViewModeVertical);
CCLOG("getAnchorPoint=%f,%f",listView->getAnchorPoint().x,listView->getAnchorPoint().y);
CCSize listSize = CCSizeMake(winSize.width * 0.5, winSize.height);
listView->setContentSize(listSize);
listView->setDelegate(this);
listView->setPosition(CCPointZero);
this->addChild(listView);
m_pListView = listView;
// 初始化控件Label,显示ListView信息
m_InfoLabel = CCLabelTTF::create("Info", "", 32);
m_InfoLabel->setPosition(ccp(winSize.width * 3 / 4, winSize.height / 2));
this->addChild(m_InfoLabel);
CCLOG("listView->getSlideDir=%d",listView->getSlideDir());
bRet = true;
} while (0);
return bRet;
}
void ListViewDemoLayer::visit()
{
CCLayer::visit();
if (m_bFresh) {
m_pListView->reload();
m_bFresh = false;
}
}
void ListViewDemoLayer::CCListView_numberOfCells(cocos2d::extension::CCListView *listView, cocos2d::extension::CCListViewProtrolData *data)
{
data->nNumberOfRows = m_pDataList->size();
CCLOG(“ListViewDemoLayer::CCListView_numberOfCells=%u,NumberOfRows=%u,point=%d”,m_pDataList->size(),data->nNumberOfRows,data);
}
void ListViewDemoLayer::CCListView_cellForRow(cocos2d::extension::CCListView *listView, cocos2d::extension::CCListViewProtrolData *data)
{
CCSize listSize = m_pListView->getContentSize();
CCSize cellSize = CCSizeMake(listSize.width, listSize.height / 5);
CCListViewCell *cell = CCListViewCell::node();
cell->setOpacity(0);
cell->setContentSize(cellSize);
cell->setSelectionColor(ccc4(255, 0, 0, 255));
data->cell = cell;
std::list<std::string>::iterator it = m_pDataList->begin();
CCLOG("ListViewDemoLayer::CCListView_cellForRow data->nRow=%u,NumberOfRows=%u,point=%d",data->nRow,data->nNumberOfRows,data);
for (unsigned int i=0; i<data->nRow; ++i) {
++it;
}
CCLabelTTF *cellLabel = CCLabelTTF::create(((std::string) *it).c_str(), "Arial", 32);
cellLabel->setPosition(ccp(cellSize.width / 2, cellSize.height / 2));
cell->addChild(cellLabel);
}
void ListViewDemoLayer::CCListView_didClickCellAtRow(cocos2d::extension::CCListView *listView, cocos2d::extension::CCListViewProtrolData *data)
{
char info;
CCLOG(“ListViewDemoLayer::CCListView_didClickCellAtRow nRow=%u,nNumberOfRows=%u,point=%d”,data->nRow,data->nNumberOfRows,data);
sprintf(info, “No. %d Row”, data->nRow);
m_InfoLabel->setString(info);
}
void ListViewDemoLayer::CCListView_didScrollToRow(cocos2d::extension::CCListView *listView, cocos2d::extension::CCListViewProtrolData *data)
{
m_InfoLabel->setString(“Scrolling…”);
}
调用代码:
ListViewDemoLayer *listViewDemoLayer = dynamic_cast<ListViewDemoLayer *>(ListViewDemoLayer::create());
this->addChild(listViewDemoLayer);
不是,我的里面是要放一堆sprite,和menu的,上面呢个可以的吗
CCListViewCell本身就是个layer,至于里面添加什么,怎么组合自己发挥咯。
我也是遇到这个问题,一但父节点对象坐标不是0,0切屏显示就会有问题,我是把他的底层代码给改了,直接把beforeDraw里面那句CCPoint screenPos = this->convertToWorldSpace(this->getParent()->getPosition())改成CCPoint screenPos = this->convertToWorldSpace(ccp(0,0))这样就可以了,不过不知道你是怎么解决的?