cocos creator连续动作执行随笔
分别适用Promise和Generator进行连续动作。
const pos = [[6, 1, 1], [6, 2, 1], [6, 3, 0], [6, 4, 0], [6, 5, 0], [5, 6, 0], [4, 6, 0], [3, 6, 0],
[2, 6, 0], [1, 6, 0], [0, 6, 0], [0, 7, 0], [0, 8, 0], [1, 8, 1], [2, 8, 0], [3, 8, 0], [4, 8, 0], [5, 8, 0], [6, 9, 0], [6, 10, 0],
[6, 11, 0], [6, 12, 0], [6, 13, 0], [6, 14, 0], [7, 14, 0], [8, 14, 0], [8, 13, 1], [8, 12, 0], [8, 11, 0], [8, 10, 0], [8, 9, 0],
[9, 8, 0], [10, 8, 0], [11, 8, 0], [12, 8, 0], [13, 8, 0], [14, 8, 0], [14, 7, 0], [14, 6, 0], [13, 6, 1], [12, 6, 0], [11, 6, 0],
[10, 6, 0], [9, 6, 0], [8, 5, 0], [8, 4, 0], [8, 3, 0], [8, 2, 0], [8, 1, 0], [8, 0, 0], [7, 0, 0], [6, 0, 0],
[7, 1, 1], [7, 2, 1], [7, 3, 1], [7, 4, 1], [7, 5, 1], [7, 6, 1]];
使用Promise:
async runTest() {
const d = 20;
for(let i = 0,len = pos.length;i < len;i++){
await this.runActionFunc(
cc.tween().to(0.2, { position:cc.v2(pos[i][0] * d,pos[i][1] * d) })
);
}
}
runActionFunc(move:cc.Tween){
return new Promise((resolve, reject) => {
cc.tween(this.testNode).then(move).then(cc.callFunc(resolve)).start();
});
}
使用Generator:
var iter = null;
var g = this.runTest();
g.next();
*runTest() {
const d = 20;
for (let i = 0, len = pos.length; i < len; i++) {
yield this.runActionFunc(
cc.tween().to(0.2, { position: cc.v2(pos[i][0] * d, pos[i][1] * d) })
);
}
}
runActionFunc(move: cc.Tween) {
cc.tween(this.testNode).then(move).then(cc.callFunc(()=>{
iter = g.next();
if(iter == null || iter.done){
g = null;
}
})).start();
}
浙公网安备 33010602011771号