Is startGameCallback a static method. It isn’t. But I create SelectGameScene through a static method createScene(). So compiler think that all methods of this class are static. That’s why I can’t get a reference.
Error is : “Reference to non-static member function must be called”
void MainMenuScene::startCallback(Ref* pSender)
{
Director::getInstance()->replaceScene(TransitionFade::create(TRANSITION_TIME, SelectGameScene::createScene()));
}
class SelectGameScene : public cocos2d::Layer
{
public:
static cocos2d::Scene* createScene();
virtual bool init();
void onTouchEnded(cocos2d::Touch* touch, cocos2d::Event* event);
void startGameCallback( float dt);
}
Scene* SelectGameScene::createScene()
{
auto scene = Scene::create();
auto layer = SelectGameScene::create();
scene->addChild(layer);
return scene;
}
bool SelectGameScene::init()
{
if (!Layer::init())
return false;
visibleSize = Director::getInstance()->getVisibleSize();
this->setName("SelectGameScene");
}
void SelectGameScene::onTouchEnded(Touch* touch, Event* event)
{
this->scheduleOnce(schedule_selector(SelectGameScene::startGameCallback),this, 0.0f, false);
}
void SelectGameScene::startGameCallback(float dt)
{
Director::getInstance()->replaceScene(TransitionFade::create(TRANSITION_TIME, GameScene::createScene()));
}
Where
typedef void (Ref::*SEL_SCHEDULE)(float);
#define CC_SCHEDULE_SELECTOR(_SELECTOR) static_cast<cocos2d::SEL_SCHEDULE>(&_SELECTOR)
void Node::scheduleOnce(SEL_SCHEDULE selector, float delay)
{
this->schedule(selector, 0.0f, 0, delay);
}
void Node::schedule(SEL_SCHEDULE selector, float interval, unsigned int repeat, float delay)
{
CCASSERT( selector, "Argument must be non-nil");
CCASSERT( interval >=0, "Argument must be positive");
_scheduler->schedule(selector, this, interval , repeat, delay, !_running);
}
void Scheduler::schedule(SEL_SCHEDULE selector, Ref *target, float interval, unsigned int repeat, float delay, bool paused)
{
CCASSERT(target, "Argument target must be non-nullptr");
tHashTimerEntry *element = nullptr;
HASH_FIND_PTR(_hashForTimers, &target, element);
if (! element)
{
element = (tHashTimerEntry *)calloc(sizeof(*element), 1);
element->target = target;
HASH_ADD_PTR(_hashForTimers, target, element);
// Is this the 1st element ? Then set the pause level to all the selectors of this target
element->paused = paused;
}
else
{
CCASSERT(element->paused == paused, "element's paused should be paused.");
}
if (element->timers == nullptr)
{
element->timers = ccArrayNew(10);
}
else
{
for (int i = 0; i < element->timers->num; ++i)
{
TimerTargetSelector *timer = dynamic_cast<TimerTargetSelector*>(element->timers->arr[i]);
if (timer && !timer->isExhausted() && selector == timer->getSelector())
{
CCLOG("CCScheduler#schedule. Reiniting timer with interval %.4f, repeat %u, delay %.4f", interval, repeat, delay);
timer->setupTimerWithInterval(interval, repeat, delay);
return;
}
}
ccArrayEnsureExtraCapacity(element->timers, 1);
}
TimerTargetSelector *timer = new (std::nothrow) TimerTargetSelector();
timer->initWithSelector(this, selector, target, interval, repeat, delay);
ccArrayAppendObject(element->timers, timer);
timer->release();
}