问题:
在使用cocos2d html编写一系列的战斗动画时经常遇到一个精灵动作完成时需要继续播放另一个精灵动作 如下:
//动作1
actionSetp1: function() {
...
var move = cc.MoveTo.create(1, cc.p(100,100));
//移动完后执行,actionStep2函数
var call = cc.CallFunc.create(this.onSetp2, this);
var seq = cc.Sequence.create([move, call]);
this._sprite1.runAction(seq );
}
//动作2actionSetp1:
function() {
....
var scale= cc.ScaleTo.create(1, 1.5);
//移动完后执行,actionStep3函数
var call = cc.CallFunc.create(this.onSetp3, this);
var seq = cc.Sequence.create([scale, call]);
this._sprite2.runAction(seq );
}
//动作3
actionSetp3: function() {
....
//没有后继动作了,没有用cc.CallFunc
var move= cc.MoveTo.create(1, cc.p(100,100));
this._sprite.runAction(seq );
}
playAction: function() {
//这里需要将当前this对象传入
//动作按actionSetp1, actionSetp2, actionSetp3 顺序执行
this.actionSetp1(this)
.then(this.actionSetp2)
.then(this.actionSetp3);
}
//动作1
actionSetp1: function(self) {
...
//使用q.js创建defer对象
var defer = Q.defer();
var move = cc.MoveTo.create(1, cc.p(100,100));
var call = cc.CallFunc.create(function() {
//执行defer.resolve()函数, 表示当前动作已经完成
//不需要知道下一个动作函数是什么
defer.resolve(self);
}, self);
var seq = cc.Sequence.create([move, call]);
self._sprite1.runAction(seq );
return defer .promise; //这里返回promise, 用于形成promise.then的调用链}
//动作2
actionSetp1: function(self) {
....
var defer = Q.defer(); //创建defervar
var scale= cc.ScaleTo.create(1, 1.5);
var call = cc.CallFunc.create(function() {
defer.resolve(self);
}, self);
var seq = cc.Sequence.create([scale, call]);
self._sprite2.runAction(seq );
//这里返回promise, 用于形成promise.then的调用链
return defer .promise;
}
//动作3
actionSetp3: function(self) {
....
//同上
var defer = Q.defer();
var move= cc.MoveTo.create(1, cc.p(100,100));
var call = cc.CallFunc.create(function() {
defer.resolve(self);
}, self);
self._sprite.runAction(seq );
//这里返回promise, 用于形成promise.then的调用链
return defer .promise;
}
我们复制两行动作, 动作链如下:
this.actionSetp1(this)
.then(this.actionSetp2)
.then(this.actionSetp3); //复制
.then(this.actionSetp2); //复制
.then(this.actionSetp3);
我们修改一下,添加新动作, 动作链如下:
this.actionSetp1(this)
.then(this.actionSetp2)
.then(this.actionSetp3);
.then(this.actionSetp4); //新增
.then(this.actionSetp5); //新增
mySetTimeout = function(callback, interval) {
var director = cc.Director.getInstance();
director.getScheduler().scheduleCallbackForTarget(director, callback, 0, 1, interval, false);
};
var setTimeout = setTimeout || mySetTimeout ;