过三秒之后,创建精灵并显示在屏幕上
auto vSize = Director::getInstance()->getVisibleSize();
auto delayTime = DelayTime::create(3.0f);
auto func = CallFunc::create([this, vSize]()
{
auto sp = Sprite::create("blue.png");
sp->setPosition(vSize / 2);
this->addChild(sp);
});
auto seq = Sequence::create(delayTime, func, nullptr);
this->runAction(seq);
当然,也可以让lambda的方法抽出来,增加复用性。
bool HelloWorld::init()
{
if(!Layer::init())
{
return false;
}
auto delayTime = DelayTime::create(3.0f);
auto func = CallFunc::create(CC_CALLBACK_0(HelloWorld::initBlue, this));
auto seq = Sequence::create(delayTime, func, nullptr);
this->runAction(seq);
return true;
}
void HelloWorld::initBlue()
{
auto vSize = Director::getInstance()->getVisibleSize();
auto sp = Sprite::create("blue.png");
sp->setPosition(vSize / 2);
this->addChild(sp);
}
还可以传递参数,注意要用CallFuncN
bool HelloWorld::init()
{
if(!Layer::init())
{
return false;
}
auto delayTime = DelayTime::create(3.0f);
auto func = CallFuncN::create(CC_CALLBACK_1(HelloWorld::initBlue, this, 1));
auto seq = Sequence::create(delayTime, func, nullptr);
this->runAction(seq);
return true;
}
void HelloWorld::initBlue(Ref* ref, int num)
{
log("%d", num);
auto vSize = Director::getInstance()->getVisibleSize();
auto sp = Sprite::create("blue.png");
sp->setPosition(vSize / 2);
this->addChild(sp);
}