我正在尝试使用cocos2d-2.0-rc2-x-2.0.1创建一个ScrollView类,我的意思是在更新函数中执行一些操作来实现自动滚动效果。不幸的是,我发现该函数从未出现过虽然我做了很多工作,比如在互联网上搜索,逐步调试等等,但我找到的可能解决方案帮助不大。
据我所知,我的ScrollView类派生自CCNode,我实现了更新功能.ScrollView的声明如下:
class ScrollView:public CCNode,public CCTouchDelegate
{
ClippingNode* visible_view;
CCNode* content_view;
//CCArray* items;
float row_margin;
float col_margin;
float interval_margin;
float last_y;//起始y方向坐标
float interval_dis;//间隔时间段内y方向上的位移。
bool touch_stopped;//标识触摸是否停止,主要用于自动滚动。
float up_bounder_y,down_bounder_y;//content_view的y方向坐标上下限
int items_num;
public:
static ScrollView* New(CCSize visible_view_size,float row_margin,float col_margin,float interval_margin,CCNode* background = NULL);
void ccTouchBegin(cocos2d::CCNode *node,const cocos2d::CCPoint &point);
void ccTouchMove(cocos2d::CCNode *node,const cocos2d::CCPoint &point);
void ccTouchEnd(cocos2d::CCNode *node,const cocos2d::CCPoint &point);
virtual void onEnter();
protected:
CCNode* makeCard();
void initContent();
private:
ScrollView():visible_view(NULL),content_view(NULL),touch_stopped(true){}
virtual ~ScrollView();
bool init(CCSize visible_view_size,float row_margin,float col_margin,float interval_margin,CCNode* background);
void update(float dt);
};
以下是更新功能的定义:
void ScrollView::update(float dt)
{
CCLOG("update");
if(touch_stopped)
{
if(abs(interval_dis) < a)
{
interval_dis = 0.0f;
this->unscheduleUpdate();
}else
{
if(interval_dis < 0)
interval_dis += a;
else
interval_dis -= a;
const float future_y = content_view->getPositionY() + interval_dis;
if(future_y > down_bounder_y && future_y < up_bounder_y)
{
content_view->setPositionY(interval_dis);
}else if(future_y <= down_bounder_y)
{
content_view->setPositionY(down_bounder_y);
interval_dis = 0.0f;
}else
{
content_view->setPositionY(up_bounder_y);
interval_dis = 0.0f;
}
}
}
}
所以我可以确保param的类型是float而不是CCTime或ccTime,这可能导致永远不会调用更新函数。另外,我在init方法中调用scheduleUpdate,如下所示:
bool ScrollView::init(CCSize visible_view_size,float row_margin,float col_margin,float interval_margin,CCNode* background)
{
visible_view = ClippingNode::New(visible_view_size);
CHECK_RETURN(visible_view,NULL,false);
visible_view->retain();
content_view = CCNode::create();//node函数中已调用autorelease
CHECK_RETURN(content_view,NULL,false);
content_view->retain();
this->row_margin = row_margin;
this->col_margin = col_margin;
this->interval_margin = interval_margin;
this->setAnchorPoint(ccp(0.5f,0.5f));
this->setContentSize(visible_view_size);
visible_view->setPosition(0,0);
content_view->setAnchorPoint(ccp(0,1));
content_view->setPosition(row_margin,visible_view_size.height);
content_view->setContentSize(CCSize(visible_view_size.width - 2 * row_margin,2 * col_margin));
this->addChild(visible_view);
visible_view->addChild(content_view);
down_bounder_y = visible_view_size.height;
up_bounder_y = content_view->getContentSize().height > visible_view_size.height?content_view->getContentSize().height:visible_view_size.height;
UserData* user_data = UserData::getUserData(this,true);
CHECK_RETURN(user_data,NULL,false);
user_data->setContainer(true);
items_num = 0;
initContent();
if(background)
{
background->setScaleX(visible_view_size.width/background->getContentSize().width);
background->setScaleY(visible_view_size.height/background->getContentSize().height);
background->setAnchorPoint(ccp(0.5f,0.5f));
background->setPosition(visible_view_size.width/2,visible_view_size.height/2);
user_data = UserData::getUserData(background,true);
user_data->setHitable(false);
this->addChild(background,-1);
}
this->scheduleUpdate();
return true;
}
通过调试,我可以确保调用句子“this-&gt; scheduleUpdate()”。此外,我创建了一个名为scroll_view的ScrollView对象,并通过addChild函数将其添加到主节点。所以,我错了?任何建议都会受到赞赏,并感谢您的观看:p
答案 0 :(得分:4)
我忘了在我自己的CCNode::onEnter
函数中调用onEnter
。因此,我们需要做的就是在CCNode::onEnter
中调用ScrollView::onEnter
。希望别人不会像我一样犯错误。
答案 1 :(得分:2)
如果您调用onEnter
,scheduleUpdate()
可能无效。
CCDirector::sharedDirector()->getScheduler()->scheduleUpdateForTarget(this,0,false);
或
CCDirector::sharedDirector()->getScheduler()->scheduleSelector(schedule_selector(NewGame::update),this,0.1,false);
答案 2 :(得分:0)
不知道为什么你的代码不起作用但是你试过这个:
CCDirector::sharedDirector()->getScheduler()->scheduleUpdateForTarget(cocos2d::CCObject *pTarget, int nPriority, bool bPaused);
您可以使用以下命令检查节点是否响应update()调用:
pNode->getIsRunning();