Problem about DelayTime

Hi everyone,

I need to spawn sprites at different times. Therefore i used “DelayTime” and “Sequence”. First of all i build a class in different cpp file.

    TeamCut::TeamCut(cocos2d::Layer* layer)
{
	teamPipeSprite = Sprite::create("TeamPipe.png");
	teamPipeSprite->setAnchorPoint(Vec2(0.0, 0.0));
	teamPipeSprite->setPosition(Point(750, 750));
	layer->addChild(teamPipeSprite, 5);
}

and than i wrote that code for spawn my sprite:

auto noder = Node::create();
	
auto delayer = DelayTime::create(5.0);

auto seq = Sequence::create(delayer, TeamCut(this), nullptr);
	
noder->runAction(seq);

When i run the code my sprite spawned right now, not 5 seconds later. Where is the problem ?

Read here

https://docs.cocos2d-x.org/cocos2d-x/v3/en/actions/sequences.html

I read but still don’t get it. I think i found another solution but i don’t know how to write it. How can i write setLocalZOrder in CallFunc ?

Why do you need local z order?

If you dont want to repeat the sequence, you can use schedule with lamda

this-

… Oops, i commit mistake pushing reply button sorry…

this->schedule([&](){
     //spawn your player or your things
}, 0.0f, 0, 5.0f, "schedulekey");

This will spawn something after 5 seconds

Because my code doesn’t work. I spawn my sprite at layer(-5) and than after the delay i change my sprites layer to 5. Therefore i need to write setLocalZOrder in CallFunc.

Sequence needs objects of type FiniteTimeAction to be passed to it, so passing it your own method directly is going to cause problems. You are on the right track though, as you do need to use CallFunc (which is a FiniteTimeAction).

auto seq = Sequence::create(delayer, CallFunc::create([this]() {
        TeamCut(this);
    }), nullptr);

EDIT: added the missing ) in the CallFunc.

cough cough mentioned above and contains the exact answer to your question.

1 Like

It gave this error:

@Refetizm you pass nullptr as second argument to CallFunc::create but it expects only one callback

Problem Solved. Thanks @R101 . After your advice i gave 2 errors. @dimon4eg solved first one. The second one vas C2512 error. To solve this error i modified your advice:

auto seq = Sequence::create(delayer, CallFunc::create([this]() { teamCut = new TeamCut(this);} ), nullptr);

Thanks everbody to help to solve this problem :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.