1

我正在尝试使用 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;
            }
        }
    }
}

所以我可以确保参数的类型是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->scheduleUpdate()”这句话。此外,我创建了一个名为 scroll_view 的 ScrollView 对象,并通过 addChild 函数将其添加到主节点。那么,我错在哪里?任何补充都会不胜感激并感谢收看:p

4

3 回答 3

4

我忘了CCNode::onEnter在我自己的onEnter函数中调用。因此,我们需要做的就是CCNode::onEnterScrollView::onEnter. 希望其他人不要像我一样犯错误。

于 2013-06-20T14:39:39.773 回答
2

如果您调用onEnter,scheduleUpdate()可能无法正常工作。

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

或者

CCDirector::sharedDirector()->getScheduler()->scheduleSelector(schedule_selector(NewGame::update),this,0.1,false);
于 2013-11-30T08:57:23.817 回答
0

不知道为什么您的代码不起作用,但您是否尝试过:

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

您可以使用以下方法检查节点是否响应 update() 调用:

pNode->getIsRunning();
于 2013-06-19T09:54:57.093 回答