CCTimer can not call the callback function

CCTimer *t = CCTimer::timerWithTarget(this, schedule_selector(HelloWorld::test), 1);
// t->update(1);
anything wrong?
btw,
can cctimer perform the block(like iOS) as seletor?

Lincoln Law wrote:

btw,
can cctimer perform the block(like iOS) as seletor?
I’ve found some way to do that recently but it needs c11 support enabled. Cococ2d-x still has some problems compiling with c11-support.

I’ve found some way to do that recently but it needs c11 support enabled. Cococ2d-x still has some problems compiling with c11-support.

hey dudes,just show me how,thanks

Not sure but I think schedule callbacks should have CCTime delta as arguments.

void MyLayer::timerCallback( CCTime delta )

Lincoln Law wrote:

> I’ve found some way to do that recently but it needs c11 support enabled. Cococ2d-x still has some problems compiling with c11-support.
>
hey dudes,just show me how,thanks

I’ll show but this won’t work until cocos2d-x gains compatibility with c++11. The code below may contain mistakes since I’ve made a major cleanup to my game since I’ve tested it and now I’m writing it from my memory.

Usage example:

CCTimer *t = CCTimer::timerWithTarget(CCScheduleCallback::create(std::function([](float dt)
    {
        // your code here
        // any variables you need to capture from context need to be listed in square brackets above
    })),  schedule_selector(CCScheduleCallback::execute), 1);

CCScheduleCallback (and classes for other kinds of callbacks) is defined like this:

class CCScheduleCallback : public CCObject
{
public:
    static CCScheduleCallback* create(std::function tFunc)
    {
        CCScheduleCallback* pRet = new CCScheduleCallback();
        pRet->m_tFunc = tFunc;
        return (CCScheduleCallback*)pRet->autorelease();
    }
    void execute(float dt)
    {
        m_tFunc(dt);
    }
private:
    std::function m_tFunc;
};

You’ll need to prevent CCScheduleCallback object you pass as target from being destroyed before the selector is executed in case it won’t be executed immediatelly (need non-autorelease constructor and manual release?).

You’ll need to prevent CCScheduleCallback object you pass as target from being destroyed before the selector is executed in case it won’t be executed immediatelly (need non-autorelease constructor and manual release?).

thanks for all.