Putting Time Between Spawns

Either:
a. Schedule a function call to create the sprites, or
b. Run a Sequence action that contains a DelayTime action followed by a CallFunc action.

1 Like

I’m trying to write a code that creates a sprite repeatedly forever.But I don’t know how can i put time between the creations.Can you help me ?

How can I write a valid CallFunc code makes that :
this->addChild(bowser);

I’m not sure what you mean. You want an action that includes a CallFunc action that calls this->addChild(bowser);?

Something like:

this->runAction(Sequence::create(
    DelayTime::create(5.f),
    CallFunc::create([=](){
        this->addChild(bowser);
    }),
    nullptr));

Yeah I mentioned it.But when I use the code you wrote there is no error at compiling but when I run it on my phone it stops working after the delay.

I do something like this sometimes:

// use a sequence of calls to display instructions to user
    // and then start the game
    cocos2d::CallFunc* _purpleKernel = cocos2d::CallFunc::create([=]() {
        this->purpleKernelTimer();
    });
    cocos2d::CallFunc* _ready = cocos2d::CallFunc::create([=]() {
        this->readyTimer();
    });
    cocos2d::CallFunc* _set = cocos2d::CallFunc::create([=]() {
        this->setTimer();
    });
    cocos2d::CallFunc* _pop = cocos2d::CallFunc::create([=]() {
        this->popTimer();
    });
    cocos2d::CallFunc* _game = cocos2d::CallFunc::create([=]() {
        this->gameTimer();
    });
    cocos2d::DelayTime* _delay = cocos2d::DelayTime::create(1);

    runAction(cocos2d::Sequence::create(_purpleKernel, _delay,_delay, _ready, 
        _delay, _set, _delay, _pop, _delay, _game, NULL));

    schedule(schedule_selector(GameScene::purpleKernel), 10);

What do you mean by “stops working”? Does it crash? Are there any error messages in the logs?

Did you see the code that I posted below? It works.

Yeah it crashes and gives this error:


Revision: ‘0’
ABI: ‘arm’
pid: 22436, tid: 22465, name: GLThread 1821 >>> com.azafuse.mario <<<
signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x16d
r0 cd770c80 r1 00000000 r2 00000000 r3 0000016d
r4 cef4d6d4 r5 db9d77e0 r6 12e441c0 r7 ce3b2180
r8 12e4e0d0 r9 db9dc300 sl 0079db12 fp ce3b213c
ip 00000065 sp ce3b2100 lr 00000029 pc ce8e3f08 cpsr 600f0010

backtrace:
#00 pc 00530f08 /data/app/com.azafuse.mario-2/lib/arm/libMyGame.so (_ZN7cocos2d4Node14addChildHelperEPS0_iiRKSsb+420)
#01 pc 00530c88 /data/app/com.azafuse.mario-2/lib/arm/libMyGame.so (_ZN7cocos2d4Node8addChildEPS0_iRKSs+408)
#02 pc 00531214 /data/app/com.azafuse.mario-2/lib/arm/libMyGame.so (ZN7cocos2d4Node8addChildEPS0+252)
#03 pc 0048683b /data/app/com.azafuse.mario-2/lib/arm/libMyGame.so
#04 pc 0048723d /data/app/com.azafuse.mario-2/lib/arm/libMyGame.so
#05 pc 00495191 /data/app/com.azafuse.mario-2/lib/arm/libMyGame.so
#06 pc 00511594 /data/app/com.azafuse.mario-2/lib/arm/libMyGame.so (_ZN7cocos2d8CallFunc7executeEv+204)
#07 pc 005114bc /data/app/com.azafuse.mario-2/lib/arm/libMyGame.so (_ZN7cocos2d8CallFunc6updateEf+56)
#08 pc 005167d4 /data/app/com.azafuse.mario-2/lib/arm/libMyGame.so (_ZN7cocos2d8Sequence6updateEf+1140)
#09 pc 00515590 /data/app/com.azafuse.mario-2/lib/arm/libMyGame.so (_ZN7cocos2d14ActionInterval4stepEf+424)
#10 pc 00786498 /data/app/com.azafuse.mario-2/lib/arm/libMyGame.so (_ZN7cocos2d13ActionManager6updateEf+244)
#11 pc 0062d958 /data/app/com.azafuse.mario-2/lib/arm/libMyGame.so (_ZZN7cocos2d9Scheduler14scheduleUpdateINS_13ActionManagerEEEvPT_ibENKUlfE_clEf+56)
#12 pc 0062ff30 /data/app/com.azafuse.mario-2/lib/arm/libMyGame.so (_ZNSt17_Function_handlerIFvfEZN7cocos2d9Scheduler14scheduleUpdateINS1_13ActionManagerEEEvPT_ibEUlfE_E9_M_invokeERKSt9_Any_dataf+60)
#13 pc 005227e8 /data/app/com.azafuse.mario-2/lib/arm/libMyGame.so (_ZNKSt8functionIFvfEEclEf+88)
#14 pc 0066fb70 /data/app/com.azafuse.mario-2/lib/arm/libMyGame.so (_ZN7cocos2d9Scheduler6updateEf+196)
#15 pc 00628a84 /data/app/com.azafuse.mario-2/lib/arm/libMyGame.so (_ZN7cocos2d8Director9drawSceneEv+160)
#16 pc 0062d574 /data/app/com.azafuse.mario-2/lib/arm/libMyGame.so (_ZN7cocos2d8Director8mainLoopEv+124)
#17 pc 0048e9d5 /data/app/com.azafuse.mario-2/lib/arm/libMyGame.so (Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeRender+16)
#18 pc 0018670d /data/app/com.azafuse.mario-2/oat/arm/base.odex (offset 0x17a000)

Saw and tried it but I want to know how can I use CallFunc.

I tried a cocos new ... and this code and it works for me on iOS and Android.

Have you tried in a cocos new project?

I tried but I still get error.I re-downloaded even the engine but It’s still the same.Can you upload the working class files ?

Its weird, I don’t get these errors. @grimfate do you?

Your code works for me. The error message is a seg fault, which usually means a null (or possibly uninitialised or freed) object is being used. What code are you using @Staletto? Any possibility you are creating objects and not adding them to the scene or retaining them?

I solved it.But can you tell me how can I use RepeatForever with CallFunc ?