Is it possible to cc.sequence entire js-functions?

I am trying to get some form of sequential logic into my game with Cocos2d-js, instead of the big callback hierachy. So I would like to simply sequence two JS-methods, i.e.: “Do this first. When returning, do this” - is it possible with cc.sequence? I am not sure how. Right now I have only been able to sequence animations like cc.Move, etc.

Yes, you can call function with action.

var callFunc1 = new cc.callFunc(this.function1, this);
var callFunc2 = new cc.callFunc(this.function2, this);

this.runAction(cc.sequence(callFunc1, callFunc2));

http://www.cocos2d-x.org/reference/html5-js/V3.5/symbols/cc.CallFunc.html

But i don’t think action will wait for callFunc1 finish then only callFunc2 on sequence.
So i suggest you to make call of Func1, then in Func1, call Func2

1 Like

OK, this was really my essential problem. Calling it nested in callbacks will not be that dynamic for what I’m trying to do.

May be you can try use delay between Func1 and Func2 in sequence.
But you have to make sure Func1 complete within the delayTime.

var delay = cc.delayTime(1.0); <- time in second

this.runAction(cc.sequence(callFunc1, delay, callFunc2));

Maybe with http://cocos2d-x.org/docs/manual/framework/html5/v3/cc-async/en

1 Like

OK, I’ll give it a try.

It actually seems that:

var callFunc1 = new cc.callFunc(this.function1, this);
var callFunc2 = new cc.callFunc(this.function2, this);

Will actually run the function, not just store it until cc.sequence runs it. Are you sure this is not the case?

Make a mistake, it should be without the “new”

var callFunc1 = cc.callFunc(this.function1, this);

Yes, it won’t call the function until you use it on a runAction();

1 Like

OK, Zinitter’s suggestion is working, but the delay-solution is not viable in my case. I need to be sure that the entire function has finished before I can move on to the next. Perhaps @thomasjab can help me here. The async is not very well documentet either place.

You can try this code : Cc.async.parallel

Edit :

 cc.async.parallel([
            function(next){
                first();
                next();
            }, function(next){
                second()
                next();
            }
        ], function(){
            cc.log("end");
        });

Looks like that relies on timeouts as well for what I’m doing.

Thing is, I have a method that is like this: (simplified)

scrollOnceForward:function (timeOfOneScroll) {
    cc.log("scrollOnceForward Start: " + timeOfOneScroll);
    // Bunch of stuff happens that needs to be done BEFORE runAction
    someNode.runAction(cc.moveTo(timeOfOneScroll, toPosition));
    // Bunch of stuff happens that needs to be done AFTER runAction
    cc.log("scrollOnceForward End: " + timeOfOneScroll);
}

So I want to sequence this method a bunch of times. But the method returns ASAP, not after runAction has finished. I think this is the main issue.

I am having the same issue. I am using Cocos2D-x v3.7
This is what I tried:

var delay = cc.delayTime(5);
var callFunc1 = cc.callFunc(cc.log("test1"), this);
var callFunc2 = cc.callFunc(cc.log("test2"), this);

var sequenceAction = new cc.Sequence(callFunc1, delay, callFunc2);
this.sprite.runAction(sequenceAction);

Both test1 and test2 are being logged at the same time. It’s like the delay is not doing anything.