Skip to content

Commit

Permalink
add Tween.stopAll and Tween.stopAllByTag (cocos#6265)
Browse files Browse the repository at this point in the history
* add Tween.stopAll and Tween.stopAllByTag

* add Tween.stopAllByTarget
  • Loading branch information
2youyou2 committed Mar 8, 2020
1 parent 03543b4 commit 5a3b738
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 13 deletions.
38 changes: 25 additions & 13 deletions cocos2d/actions/CCActionManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,29 +202,41 @@ cc.ActionManager.prototype = {
}
},

_removeActionByTag (tag, element, target) {
for (var i = 0, l = element.actions.length; i < l; ++i) {
var action = element.actions[i];
if (action && action.getTag() === tag) {
if (target && action.getOriginalTarget() !== target) {
continue;
}
this._removeActionAtIndex(i, element);
break;
}
}
},

/**
* !#en Removes an action given its tag and the target.
* !#zh 删除指定对象下特定标签的一个动作,将删除首个匹配到的动作。
* @method removeActionByTag
* @param {Number} tag
* @param {Node} target
* @param {Node} [target]
*/
removeActionByTag:function (tag, target) {
if(tag === cc.Action.TAG_INVALID)
cc.logID(1002);

cc.assertID(target, 1003);

var element = this._hashTargets[target._id];

if (element) {
var limit = element.actions.length;
for (var i = 0; i < limit; ++i) {
var action = element.actions[i];
if (action && action.getTag() === tag && action.getOriginalTarget() === target) {
this._removeActionAtIndex(i, element);
break;
}
let hashTargets = this._hashTargets;
if (target) {
var element = hashTargets[target._id];
if (element) {
this._removeActionByTag(tag, element, target);
}
}
else {
for (let name in hashTargets) {
let element = hashTargets[name];
this._removeActionByTag(tag, element);
}
}
},
Expand Down
45 changes: 45 additions & 0 deletions cocos2d/actions/tween.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,38 @@ function Tween (target) {
this._actions = [];
this._finalAction = null;
this._target = target;
this._tag = cc.Action.TAG_INVALID;
}


/**
* !#en Stop all tweens
* !#zh 停止所有缓动
* @method stopAll
* @static
*/
Tween.stopAll = function () {
cc.director.getActionManager().removeAllActions();
}
/**
* !#en Stop all tweens by tag
* !#zh 停止所有指定标签的缓动
* @method stopAllByTag
* @static
* @param {number} tag
*/
Tween.stopAllByTag = function (tag) {
cc.director.getActionManager().removeActionByTag(tag);
}
/**
* !#en Stop all tweens by target
* !#zh 停止所有指定对象的缓动
* @method stopAllByTarget
* @static
* @param {Object} target
*/
Tween.stopAllByTarget = function (target) {
cc.director.getActionManager().removeAllActionsFromTarget(target);
}

/**
Expand Down Expand Up @@ -223,6 +255,7 @@ Tween.prototype.start = function () {
cc.director.getActionManager().removeAction(this._finalAction);
}
this._finalAction = this._union();
this._finalAction.setTag(this._tag);
cc.director.getActionManager().addAction(this._finalAction, this._target, false);
return this;
};
Expand All @@ -243,6 +276,18 @@ Tween.prototype.stop = function () {
};


/**
* !#en Sets tween tag
* !#zh 设置缓动的标签
* @method tag
* @param {number} tag
* @return {Tween}
*/
Tween.prototype.tag = function (tag) {
this._tag = tag;
return this;
};


/**
* !#en
Expand Down

0 comments on commit 5a3b738

Please sign in to comment.