Cocos2d x 计划更新似乎不起作用
我正试图用cocos2d-2.0-rc2-x-2.0.1创建一个ScrollView类,我想在update函数中做一些事情来实现自动滚动效果。不幸的是,我发现该函数从未被调用过。尽管我做了很多工作,比如在internet上搜索、逐步调试等等,我找到的可能的解决办法帮不了什么忙 据我所知,我的ScrollView类是从CCNode派生的,我已经实现了update函数,ScrollView的声明如下:Cocos2d x 计划更新似乎不起作用,cocos2d-x,Cocos2d X,我正试图用cocos2d-2.0-rc2-x-2.0.1创建一个ScrollView类,我想在update函数中做一些事情来实现自动滚动效果。不幸的是,我发现该函数从未被调用过。尽管我做了很多工作,比如在internet上搜索、逐步调试等等,我找到的可能的解决办法帮不了什么忙 据我所知,我的ScrollView类是从CCNode派生的,我已经实现了update函数,ScrollView的声明如下: class ScrollView:public CCNode,public CCTouchDele
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);
};
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;
}
下面是更新函数的定义:
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;
}
}
}
}
通过调试,我可以确保“this->scheduleUpdate()”这句话被调用。此外,我创建了一个名为scroll_view的ScrollView对象,并通过addChild函数将其添加到主节点。那么,我错在哪里呢?任何addvice都将感谢您观看:p不知道您的代码为什么不工作,但您是否尝试过这一点:
CCDirector::sharedDirector()->getScheduler()->scheduleUpdateForTarget(cocos2d::CCObject *pTarget, int nPriority, bool bPaused);
您可以使用以下命令检查节点是否响应update()调用:
pNode->getIsRunning();
不知道为什么代码不起作用,但您是否尝试过以下方法:
CCDirector::sharedDirector()->getScheduler()->scheduleUpdateForTarget(cocos2d::CCObject *pTarget, int nPriority, bool bPaused);
您可以使用以下命令检查节点是否响应update()调用:
pNode->getIsRunning();
我忘记在我自己的
onEnter
函数中调用CCNode::onEnter
。因此,我们所需要做的就是调用ScrollView::oneter
中的CCNode::oneter
。希望其他人不要像我一样犯错误。我忘记在自己的onEnter
函数中调用CCNode::onEnter
。因此,我们所需要做的就是调用ScrollView::oneter
中的CCNode::oneter
。希望其他人不要像我一样犯错误。如果调用onEnter
,scheduleUpdate()
可能不起作用
CCDirector::sharedDirector()->getScheduler()->scheduleUpdateForTarget(this,0,false);
或
如果调用
oneter
,scheduleUpdate()
可能不起作用
CCDirector::sharedDirector()->getScheduler()->scheduleUpdateForTarget(this,0,false);
或
尝试将更新函数的参数设置为cctime。我尝试了,但没有成功。谢谢。尝试将更新函数的参数设置为cctime。我尝试了,但没有成功。谢谢。我发现了问题。我重新定义了oneter方法,但是我忘了在ScrollView::Onner函数体中调用CCNode::Onner。所以解决方案是在我自己的Onner方法中调用CCNode::Onner。谢谢你的回答:我发现了问题。我重新定义了Onner方法,但是我忘了在ScrollView::Onter函数体中调用CCNode::Onter。所以解决方案是在我自己的Onter方法中调用CCNode::Onter。感谢您的回复:Dvoid HelloWorld::Onter(){Node::Onter();this->scheduleUpdate();}void HelloWorld::Update(float dt){CCLOG(“HelloWorld::Update”);}我的代码像scheduleUpdate一样也不起作用,你能给我看看你的代码吗?非常感谢。void HelloWorld::onEnter(){Node::onEnter();this->scheduleUpdate();}void HelloWorld::Update(float dt){CCLOG(“HelloWorld::Update”);}我的类似scheduleUpdate的代码也不起作用,你能给我看看你的代码吗?非常感谢。