优雅的异步编程 Ccos2dx Js+async.js ,eachSeries和each使用实例

数组内容如下:

		var lines = [
			{
				number:1,
				node:self.Sprite_1,
				pos:cc.p(400,300),
			},
			{
				number:2,
				node:self.Sprite_2,
				pos:cc.p(400,400),
			},
		]

根据一个数组里面的参数,串行执行一系列功能,代码如下:

        async.eachSeries(lines, function(line, callback) {
        	cc.log("action="+line.number+" start");
            line.node.runAction(cc.sequence(
                cc.moveTo(10,cc.p(line.pos.x,line.pos.y)),
                cc.callFunc(function () {
                    cc.log("action="+line.number+" end");
                    callback();
                })
            ));
        }, function(err) {
            // if any of the file processing produced an error, err would equal that error
            if( err ) {
                // One of the iterations produced an error.
                // All processing will now stop.
                cc.log('failed');
            } else {
                cc.log('successfully');
            }
        });

根据一个数组里面的参数,并行执行一系列功能,代码如下:

        async.each(lines, function(line, callback) {
        	cc.log("action="+line.number+" start");
            line.node.runAction(cc.sequence(
                cc.moveTo(10,cc.p(line.pos.x,line.pos.y)),
                cc.callFunc(function () {
                    cc.log("action="+line.number+" end");
                    callback();
                })
            ));
        }, function(err) {
            // if any of the file processing produced an error, err would equal that error
            if( err ) {
                // One of the iterations produced an error.
                // All processing will now stop.
                cc.log('failed');
            } else {
                cc.log('successfully');
            }
        });


猜你喜欢

转载自blog.csdn.net/mingyuanlove/article/details/81005738