View on GitHub

MeiYuchen's blog on Git

I am a coder in China, skilled in C language, JavaScript, HTML, CSS, cocos2d-x...

Home git c cocos2dx js common

Use loop run Action in cocos2dx

If you want to use loop run many actions and clean them up in the last loop, or you want to call a function at the end in cocos2dx, you can try the code, instead of closure for the reason that the closure will make your code more complex.

		var action = xxx;
		var sprite = xxx;

		var seq = null;
		for (var i = 0; i < n; i++)
		{
			var delay = cc.DelayTime.create(i * 0.5);
			if (i == n - 1)
			{
				//add a callback
				seq = cc.Sequence.create(
					delay,
					action,
					cc.CallFunc.create(function () {
						//what you want to do at the end
					});
				);
			}
			else
			{
				seq = cc.Sequence.create(
					cc.DelayTime.create(i * 0.5),
					action
				);
			}
		}
	

comments powered by Disqus