I have the following JavaScript code:
if (condition == 'true'){
this.function1();
this.function2();
}
function1() code:
function1(){
this.node.getChildByName("spriteName").active = true;
this.node.getChildByName("spriteName").runAction(cc.moveTo(0.1, x, y));
}
function2() code:
function2(){
this.node.getChildByName("spriteName").active = false;
}
How can I ensure that function2 is called only after function1 has completed?
I have tried cc.delayTime()
but it's not proper.
I'm gonna assume that you're using
Cocos Creator 2.4
.The action system is currently deprecated (source). It might be wise to use
cc.tween
instead.Solution using cc.tween (recommended)
You can use
cc.tween
methods such asto
andcall
to move your node and call a method.Example
Here's an example made using the code that you provided (in TypeScript).
Solution using Action System (deprecated)
If you still wish to stay with your initial solution, you can call a method after an action is done by using
cc.sequence
andcc.callFunc
. The first method will be used to call actions in sequence one by one and the other one will be used as an action that just calls a method.Example
Here's an another example made using the code that you provided (in TypeScript).
Extra solution - cc.tween + Promise
If you wish to move several objects simultaneously with various different durations but wish to run a method after all of them have finished moving, you can use
cc.tween
in combination withPromise
class.Example