2013-06-18 62 views
1

我试图创建的cocos2d-2.0-RC2-X-2.0.1滚动型类和我的意思是做更新功能的东西来实现自动滚动effect.Unfortunately,我找到了功能从来没有被称为。尽管我做了很多工作,如在互联网上搜索,逐步调试等,但我发现的可能解决方案几乎没有什么帮助。似乎scheduleUpdate不工作

据我所知,我的滚动型类从CCNode派生和我实现了滚动型的更新function.The声明如下:

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的类型浮代替CCTime或ccTime这可能会导致更新函数不为called.Moreover,我调用scheduleUpdate在类似于下列init方法:

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; 
} 

通过调试,我可以保证了句“这 - > scheduleUpdate()”是invoked.In此外,我创建了一个名为滚动型对象scroll_view并把它添加到主节点通过的addChild function.So,其中上午?我错了任何addvice将不胜感激和感谢收看:p

+0

尝试制作更新功能的参数cctime。 –

+0

我试过了,但没有奏效。谢谢。 – sky

回答

4

我忘了调用CCNode::onEnter在我自己的onEnter功能。因此,我们所需要做的就是在ScrollView::onEnter中调用CCNode::onEnter。希望其他人不要像我那样犯错。

+0

无效的HelloWorld ::的OnEnter(){ 节点 ::的OnEnter(); this-> scheduleUpdate(); } 空隙的HelloWorld ::更新(浮动DT) { CCLOG(的 “HelloWorld ::更新”); }我的代码就像scheduleUpdate不工作,你能告诉我你的代码吗?谢谢! – Dracuuula

0

不知道为什么你的代码不能正常工作,但你尝试过这样的:

CCDirector::sharedDirector()->getScheduler()->scheduleUpdateForTarget(cocos2d::CCObject *pTarget, int nPriority, bool bPaused); 

您可以检查节点是否响应更新()呼叫使用:

pNode->getIsRunning(); 
+1

我发现的疑难问题重新定义了的OnEnter方法,但我忘了来调用了滚动::的OnEnter功能CCNode ::的OnEnter body.So的解决方法是调用CCNode ::的OnEnter在我自己的OnEnter方法。感谢您的回复:d – sky

2

如果你调用onEnterscheduleUpdate()可能无法正常工作。

CCDirector::sharedDirector()->getScheduler()->scheduleUpdateForTarget(this,0,false); 

CCDirector::sharedDirector()->getScheduler()->scheduleSelector(schedule_selector(NewGame::update),this,0.1,false);