解决cocoscreator+typescript开发中遇到的for循环延时执行问题

做一个卡牌游戏,有一个需求,需要在for循环中延时执行一些逻辑。

期初用了cc.tween.delay(时间).call(回调函数)的方式,发现不行。

后来想起来可以用setTimeout来解决,正解。

但是使用setTimeout是有坑的,不能直接像下面这么写,否则延时函数里的i是不对的。

for(let i=0;i<5;i++){
    setTimeout(function () {
        console.log('---------------'+i);
     }, 1000);
}

可以通过在构造一个函数的方式,就可以解决,如下:

private testFor(){
    for(let i=0;i<5;i++){
        this.testDelay();
    }
}

private testDelay(){
    setTimeout(function () {
       console.log('---------------'+i);
    }, 1000);
}