shadow-function
Version:
ioing lib - shadow Function, worker Function
1,605 lines (1,446 loc) • 110 kB
JavaScript
/****************************************************************************
Copyright (c) 2008-2010 Ricardo Quesada
Copyright (c) 2011-2012 cocos2d-x.org
Copyright (c) 2013-2014 Chukong Technologies Inc.
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
/**
* <p> An interval action is an action that takes place within a certain period of time. <br/>
* It has an start time, and a finish time. The finish time is the parameter<br/>
* duration plus the start time.</p>
*
* <p>These CCActionInterval actions have some interesting properties, like:<br/>
* - They can run normally (default) <br/>
* - They can run reversed with the reverse method <br/>
* - They can run with the time altered with the Accelerate, AccelDeccel and Speed actions. </p>
*
* <p>For example, you can simulate a Ping Pong effect running the action normally and<br/>
* then running it again in Reverse mode. </p>
*
* @class
* @extends cc.FiniteTimeAction
* @param {Number} d duration in seconds
* @example
* var actionInterval = new cc.ActionInterval(3);
*/
cc.ActionInterval = cc.FiniteTimeAction.extend(/** @lends cc.ActionInterval# */{
_elapsed: 0,
_firstTick: false,
_easeList: null,
_timesForRepeat: 1,
_repeatForever: false,
_repeatMethod: false,//Compatible with repeat class, Discard after can be deleted
_speed: 1,
_speedMethod: false,//Compatible with speed class, Discard after can be deleted
/**
* Constructor function, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function.
* @param {Number} d duration in seconds
*/
ctor: function (d) {
this._speed = 1;
this._timesForRepeat = 1;
this._repeatForever = false;
this.MAX_VALUE = 2;
this._repeatMethod = false;//Compatible with repeat class, Discard after can be deleted
this._speedMethod = false;//Compatible with repeat class, Discard after can be deleted
cc.FiniteTimeAction.prototype.ctor.call(this);
d !== undefined && this.initWithDuration(d);
},
/**
* How many seconds had elapsed since the actions started to run.
* @return {Number}
*/
getElapsed: function () {
return this._elapsed;
},
/**
* Initializes the action.
* @param {Number} d duration in seconds
* @return {Boolean}
*/
initWithDuration: function (d) {
this._duration = (d === 0) ? cc.FLT_EPSILON : d;
// prevent division by 0
// This comparison could be in step:, but it might decrease the performance
// by 3% in heavy based action games.
this._elapsed = 0;
this._firstTick = true;
return true;
},
/**
* Returns true if the action has finished.
* @return {Boolean}
*/
isDone: function () {
return (this._elapsed >= this._duration);
},
/**
* Some additional parameters of cloning.
* @param {cc.Action} action
* @private
*/
_cloneDecoration: function (action) {
action._repeatForever = this._repeatForever;
action._speed = this._speed;
action._timesForRepeat = this._timesForRepeat;
action._easeList = this._easeList;
action._speedMethod = this._speedMethod;
action._repeatMethod = this._repeatMethod;
},
_reverseEaseList: function (action) {
if (this._easeList) {
action._easeList = [];
for (var i = 0; i < this._easeList.length; i++) {
action._easeList.push(this._easeList[i].reverse());
}
}
},
/**
* Returns a new clone of the action.
* @returns {cc.ActionInterval}
*/
clone: function () {
var action = new cc.ActionInterval(this._duration);
this._cloneDecoration(action);
return action;
},
/**
* Implementation of ease motion.
*
* @example
* //example
* action.easing(cc.easeIn(3.0));
* @param {Object} easeObj
* @returns {cc.ActionInterval}
*/
easing: function (easeObj) {
if (this._easeList)
this._easeList.length = 0;
else
this._easeList = [];
for (var i = 0; i < arguments.length; i++)
this._easeList.push(arguments[i]);
return this;
},
_computeEaseTime: function (dt) {
var locList = this._easeList;
if ((!locList) || (locList.length === 0))
return dt;
for (var i = 0, n = locList.length; i < n; i++)
dt = locList[i].easing(dt);
return dt;
},
/**
* called every frame with it's delta time. <br />
* DON'T override unless you know what you are doing.
*
* @param {Number} dt
*/
step: function (dt) {
if (this._firstTick) {
this._firstTick = false;
this._elapsed = 0;
} else
this._elapsed += dt;
//this.update((1 > (this._elapsed / this._duration)) ? this._elapsed / this._duration : 1);
//this.update(Math.max(0, Math.min(1, this._elapsed / Math.max(this._duration, cc.FLT_EPSILON))));
var t = this._elapsed / (this._duration > 0.0000001192092896 ? this._duration : 0.0000001192092896);
t = (1 > t ? t : 1);
this.update(t > 0 ? t : 0);
//Compatible with repeat class, Discard after can be deleted (this._repeatMethod)
if (this._repeatMethod && this._timesForRepeat > 1 && this.isDone()) {
if (!this._repeatForever) {
this._timesForRepeat--;
}
//var diff = locInnerAction.getElapsed() - locInnerAction._duration;
this.startWithTarget(this.target);
// to prevent jerk. issue #390 ,1247
//this._innerAction.step(0);
//this._innerAction.step(diff);
this.step(this._elapsed - this._duration);
}
},
/**
* Start this action with target.
* @param {cc.Node} target
*/
startWithTarget: function (target) {
cc.Action.prototype.startWithTarget.call(this, target);
this._elapsed = 0;
this._firstTick = true;
},
/**
* returns a reversed action. <br />
* Will be overwrite.
*
* @return {?cc.Action}
*/
reverse: function () {
cc.log("cc.IntervalAction: reverse not implemented.");
return null;
},
/**
* Set amplitude rate.
* @warning It should be overridden in subclass.
* @param {Number} amp
*/
setAmplitudeRate: function (amp) {
// Abstract class needs implementation
cc.log("cc.ActionInterval.setAmplitudeRate(): it should be overridden in subclass.");
},
/**
* Get amplitude rate.
* @warning It should be overridden in subclass.
* @return {Number} 0
*/
getAmplitudeRate: function () {
// Abstract class needs implementation
cc.log("cc.ActionInterval.getAmplitudeRate(): it should be overridden in subclass.");
return 0;
},
/**
* Changes the speed of an action, making it take longer (speed>1)
* or less (speed<1) time. <br/>
* Useful to simulate 'slow motion' or 'fast forward' effect.
*
* @param speed
* @returns {cc.Action}
*/
speed: function (speed) {
if (speed <= 0) {
cc.log("The speed parameter error");
return this;
}
this._speedMethod = true;//Compatible with repeat class, Discard after can be deleted
this._speed *= speed;
return this;
},
/**
* Get this action speed.
* @return {Number}
*/
getSpeed: function () {
return this._speed;
},
/**
* Set this action speed.
* @param {Number} speed
* @returns {cc.ActionInterval}
*/
setSpeed: function (speed) {
this._speed = speed;
return this;
},
/**
* Repeats an action a number of times.
* To repeat an action forever use the CCRepeatForever action.
* @param times
* @returns {cc.ActionInterval}
*/
repeat: function (times) {
times = Math.round(times);
if (isNaN(times) || times < 1) {
cc.log("The repeat parameter error");
return this;
}
this._repeatMethod = true;//Compatible with repeat class, Discard after can be deleted
this._timesForRepeat *= times;
return this;
},
/**
* Repeats an action for ever. <br/>
* To repeat the an action for a limited number of times use the Repeat action. <br/>
* @returns {cc.ActionInterval}
*/
repeatForever: function () {
this._repeatMethod = true;//Compatible with repeat class, Discard after can be deleted
this._timesForRepeat = this.MAX_VALUE;
this._repeatForever = true;
return this;
}
});
/**
* An interval action is an action that takes place within a certain period of time.
* @function
* @param {Number} d duration in seconds
* @return {cc.ActionInterval}
* @example
* // example
* var actionInterval = cc.actionInterval(3);
*/
cc.actionInterval = function (d) {
return new cc.ActionInterval(d);
};
/**
* Please use cc.actionInterval instead.
* An interval action is an action that takes place within a certain period of time.
* @static
* @deprecated since v3.0 <br /> Please use cc.actionInterval instead.
* @param {Number} d duration in seconds
* @return {cc.ActionInterval}
*/
cc.ActionInterval.create = cc.actionInterval;
/**
* Runs actions sequentially, one after another.
* @class
* @extends cc.ActionInterval
* @param {Array|cc.FiniteTimeAction} tempArray
* @example
* // create sequence with actions
* var seq = new cc.Sequence(act1, act2);
*
* // create sequence with array
* var seq = new cc.Sequence(actArray);
*/
cc.Sequence = cc.ActionInterval.extend(/** @lends cc.Sequence# */{
_actions: null,
_split: null,
_last: 0,
/**
* Constructor function, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function. <br />
* Create an array of sequenceable actions.
* @param {Array|cc.FiniteTimeAction} tempArray
*/
ctor: function (tempArray) {
cc.ActionInterval.prototype.ctor.call(this);
this._actions = [];
var paramArray = (tempArray instanceof Array) ? tempArray : arguments;
var last = paramArray.length - 1;
if ((last >= 0) && (paramArray[last] == null))
cc.log("parameters should not be ending with null in Javascript");
if (last >= 0) {
var prev = paramArray[0], action1;
for (var i = 1; i < last; i++) {
if (paramArray[i]) {
action1 = prev;
prev = cc.Sequence._actionOneTwo(action1, paramArray[i]);
}
}
this.initWithTwoActions(prev, paramArray[last]);
}
},
/**
* Initializes the action <br/>
* @param {cc.FiniteTimeAction} actionOne
* @param {cc.FiniteTimeAction} actionTwo
* @return {Boolean}
*/
initWithTwoActions: function (actionOne, actionTwo) {
if (!actionOne || !actionTwo)
throw new Error("cc.Sequence.initWithTwoActions(): arguments must all be non nil");
var d = actionOne._duration + actionTwo._duration;
this.initWithDuration(d);
this._actions[0] = actionOne;
this._actions[1] = actionTwo;
return true;
},
/**
* returns a new clone of the action
* @returns {cc.Sequence}
*/
clone: function () {
var action = new cc.Sequence();
this._cloneDecoration(action);
action.initWithTwoActions(this._actions[0].clone(), this._actions[1].clone());
return action;
},
/**
* Start the action with target.
* @param {cc.Node} target
*/
startWithTarget: function (target) {
cc.ActionInterval.prototype.startWithTarget.call(this, target);
this._split = this._actions[0]._duration / this._duration;
this._last = -1;
},
/**
* stop the action.
*/
stop: function () {
// Issue #1305
if (this._last !== -1)
this._actions[this._last].stop();
cc.Action.prototype.stop.call(this);
},
/**
* Called once per frame. Time is the number of seconds of a frame interval.
* @param {Number} dt
*/
update: function (dt) {
var new_t, found = 0;
var locSplit = this._split, locActions = this._actions, locLast = this._last, actionFound;
dt = this._computeEaseTime(dt);
if (dt < locSplit) {
// action[0]
new_t = (locSplit !== 0) ? dt / locSplit : 1;
if (found === 0 && locLast === 1) {
// Reverse mode ?
// XXX: Bug. this case doesn't contemplate when _last==-1, found=0 and in "reverse mode"
// since it will require a hack to know if an action is on reverse mode or not.
// "step" should be overriden, and the "reverseMode" value propagated to inner Sequences.
locActions[1].update(0);
locActions[1].stop();
}
} else {
// action[1]
found = 1;
new_t = (locSplit === 1) ? 1 : (dt - locSplit) / (1 - locSplit);
if (locLast === -1) {
// action[0] was skipped, execute it.
locActions[0].startWithTarget(this.target);
locActions[0].update(1);
locActions[0].stop();
}
if (!locLast) {
// switching to action 1. stop action 0.
locActions[0].update(1);
locActions[0].stop();
}
}
actionFound = locActions[found];
// Last action found and it is done.
if (locLast === found && actionFound.isDone())
return;
// Last action found and it is done
if (locLast !== found)
actionFound.startWithTarget(this.target);
new_t = new_t * actionFound._timesForRepeat;
actionFound.update(new_t > 1 ? new_t % 1 : new_t);
this._last = found;
},
/**
* Returns a reversed action.
* @return {cc.Sequence}
*/
reverse: function () {
var action = cc.Sequence._actionOneTwo(this._actions[1].reverse(), this._actions[0].reverse());
this._cloneDecoration(action);
this._reverseEaseList(action);
return action;
}
});
/** helper constructor to create an array of sequenceable actions
* @function
* @param {Array|cc.FiniteTimeAction} tempArray
* @return {cc.Sequence}
* @example
* // example
* // create sequence with actions
* var seq = cc.sequence(act1, act2);
*
* // create sequence with array
* var seq = cc.sequence(actArray);
* todo: It should be use new
*/
cc.sequence = function (/*Multiple Arguments*/tempArray) {
var paramArray = (tempArray instanceof Array) ? tempArray : arguments;
if ((paramArray.length > 0) && (paramArray[paramArray.length - 1] == null))
cc.log("parameters should not be ending with null in Javascript");
var result, current, i, repeat;
while (paramArray && paramArray.length > 0) {
current = Array.prototype.shift.call(paramArray);
repeat = current._timesForRepeat || 1;
current._repeatMethod = false;
current._timesForRepeat = 1;
i = 0;
if (!result) {
result = current;
i = 1;
}
for (i; i < repeat; i++) {
result = cc.Sequence._actionOneTwo(result, current);
}
}
return result;
};
/**
* Please use cc.sequence instead.
* helper constructor to create an array of sequenceable actions
* @static
* @deprecated since v3.0 <br /> Please use cc.sequence instead.
* @param {Array|cc.FiniteTimeAction} tempArray
* @return {cc.Sequence}
*/
cc.Sequence.create = cc.sequence;
/** creates the action
* @param {cc.FiniteTimeAction} actionOne
* @param {cc.FiniteTimeAction} actionTwo
* @return {cc.Sequence}
* @private
*/
cc.Sequence._actionOneTwo = function (actionOne, actionTwo) {
var sequence = new cc.Sequence();
sequence.initWithTwoActions(actionOne, actionTwo);
return sequence;
};
/**
* Repeats an action a number of times.
* To repeat an action forever use the CCRepeatForever action.
* @class
* @extends cc.ActionInterval
* @param {cc.FiniteTimeAction} action
* @param {Number} times
* @example
* var rep = new cc.Repeat(cc.sequence(jump2, jump1), 5);
*/
cc.Repeat = cc.ActionInterval.extend(/** @lends cc.Repeat# */{
_times: 0,
_total: 0,
_nextDt: 0,
_actionInstant: false,
_innerAction: null, //CCFiniteTimeAction
/**
* Constructor function, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function. <br />
* Creates a Repeat action. Times is an unsigned integer between 1 and pow(2,30).
* @param {cc.FiniteTimeAction} action
* @param {Number} times
*/
ctor: function (action, times) {
cc.ActionInterval.prototype.ctor.call(this);
times !== undefined && this.initWithAction(action, times);
},
/**
* @param {cc.FiniteTimeAction} action
* @param {Number} times
* @return {Boolean}
*/
initWithAction: function (action, times) {
var duration = action._duration * times;
if (this.initWithDuration(duration)) {
this._times = times;
this._innerAction = action;
if (action instanceof cc.ActionInstant) {
this._actionInstant = true;
this._times -= 1;
}
this._total = 0;
return true;
}
return false;
},
/**
* returns a new clone of the action
* @returns {cc.Repeat}
*/
clone: function () {
var action = new cc.Repeat();
this._cloneDecoration(action);
action.initWithAction(this._innerAction.clone(), this._times);
return action;
},
/**
* Start the action with target.
* @param {cc.Node} target
*/
startWithTarget: function (target) {
this._total = 0;
this._nextDt = this._innerAction._duration / this._duration;
cc.ActionInterval.prototype.startWithTarget.call(this, target);
this._innerAction.startWithTarget(target);
},
/**
* stop the action
*/
stop: function () {
this._innerAction.stop();
cc.Action.prototype.stop.call(this);
},
/**
* Called once per frame. Time is the number of seconds of a frame interval.
* @param {Number} dt
*/
update: function (dt) {
dt = this._computeEaseTime(dt);
var locInnerAction = this._innerAction;
var locDuration = this._duration;
var locTimes = this._times;
var locNextDt = this._nextDt;
if (dt >= locNextDt) {
while (dt > locNextDt && this._total < locTimes) {
locInnerAction.update(1);
this._total++;
locInnerAction.stop();
locInnerAction.startWithTarget(this.target);
locNextDt += locInnerAction._duration / locDuration;
this._nextDt = locNextDt;
}
// fix for issue #1288, incorrect end value of repeat
if (dt >= 1.0 && this._total < locTimes)
this._total++;
// don't set a instant action back or update it, it has no use because it has no duration
if (!this._actionInstant) {
if (this._total === locTimes) {
locInnerAction.update(1);
locInnerAction.stop();
} else {
// issue #390 prevent jerk, use right update
locInnerAction.update(dt - (locNextDt - locInnerAction._duration / locDuration));
}
}
} else {
locInnerAction.update((dt * locTimes) % 1.0);
}
},
/**
* Return true if the action has finished.
* @return {Boolean}
*/
isDone: function () {
return this._total === this._times;
},
/**
* returns a reversed action.
* @return {cc.Repeat}
*/
reverse: function () {
var action = new cc.Repeat(this._innerAction.reverse(), this._times);
this._cloneDecoration(action);
this._reverseEaseList(action);
return action;
},
/**
* Set inner Action.
* @param {cc.FiniteTimeAction} action
*/
setInnerAction: function (action) {
if (this._innerAction !== action) {
this._innerAction = action;
}
},
/**
* Get inner Action.
* @return {cc.FiniteTimeAction}
*/
getInnerAction: function () {
return this._innerAction;
}
});
/**
* Creates a Repeat action. Times is an unsigned integer between 1 and pow(2,30)
* @function
* @param {cc.FiniteTimeAction} action
* @param {Number} times
* @return {cc.Repeat}
* @example
* // example
* var rep = cc.repeat(cc.sequence(jump2, jump1), 5);
*/
cc.repeat = function (action, times) {
return new cc.Repeat(action, times);
};
/**
* Please use cc.repeat instead
* Creates a Repeat action. Times is an unsigned integer between 1 and pow(2,30)
* @static
* @deprecated since v3.0 <br /> Please use cc.repeat instead.
* @param {cc.FiniteTimeAction} action
* @param {Number} times
* @return {cc.Repeat}
*/
cc.Repeat.create = cc.repeat;
/** Repeats an action for ever. <br/>
* To repeat the an action for a limited number of times use the Repeat action. <br/>
* @warning This action can't be Sequenceable because it is not an IntervalAction
* @class
* @extends cc.ActionInterval
* @param {cc.FiniteTimeAction} action
* @example
* var rep = new cc.RepeatForever(cc.sequence(jump2, jump1), 5);
*/
cc.RepeatForever = cc.ActionInterval.extend(/** @lends cc.RepeatForever# */{
_innerAction: null, //CCActionInterval
/**
* Constructor function, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function. <br />
* Create a acton which repeat forever.
* @param {cc.FiniteTimeAction} action
*/
ctor: function (action) {
cc.ActionInterval.prototype.ctor.call(this);
this._innerAction = null;
action && this.initWithAction(action);
},
/**
* @param {cc.ActionInterval} action
* @return {Boolean}
*/
initWithAction: function (action) {
if (!action)
throw new Error("cc.RepeatForever.initWithAction(): action must be non null");
this._innerAction = action;
return true;
},
/**
* returns a new clone of the action
* @returns {cc.RepeatForever}
*/
clone: function () {
var action = new cc.RepeatForever();
this._cloneDecoration(action);
action.initWithAction(this._innerAction.clone());
return action;
},
/**
* Start the action with target.
* @param {cc.Node} target
*/
startWithTarget: function (target) {
cc.ActionInterval.prototype.startWithTarget.call(this, target);
this._innerAction.startWithTarget(target);
},
/**
* called every frame with it's delta time. <br />
* DON'T override unless you know what you are doing.
* @param dt delta time in seconds
*/
step: function (dt) {
var locInnerAction = this._innerAction;
locInnerAction.step(dt);
if (locInnerAction.isDone()) {
//var diff = locInnerAction.getElapsed() - locInnerAction._duration;
locInnerAction.startWithTarget(this.target);
// to prevent jerk. issue #390 ,1247
//this._innerAction.step(0);
//this._innerAction.step(diff);
locInnerAction.step(locInnerAction.getElapsed() - locInnerAction._duration);
}
},
/**
* Return true if the action has finished.
* @return {Boolean}
*/
isDone: function () {
return false;
},
/**
* Returns a reversed action.
* @return {cc.RepeatForever}
*/
reverse: function () {
var action = new cc.RepeatForever(this._innerAction.reverse());
this._cloneDecoration(action);
this._reverseEaseList(action);
return action;
},
/**
* Set inner action.
* @param {cc.ActionInterval} action
*/
setInnerAction: function (action) {
if (this._innerAction !== action) {
this._innerAction = action;
}
},
/**
* Get inner action.
* @return {cc.ActionInterval}
*/
getInnerAction: function () {
return this._innerAction;
}
});
/**
* Create a acton which repeat forever
* @function
* @param {cc.FiniteTimeAction} action
* @return {cc.RepeatForever}
* @example
* // example
* var repeat = cc.repeatForever(cc.rotateBy(1.0, 360));
*/
cc.repeatForever = function (action) {
return new cc.RepeatForever(action);
};
/**
* Please use cc.repeatForever instead
* Create a acton which repeat forever
* @static
* @deprecated since v3.0 <br /> Please use cc.repeatForever instead.
* @param {cc.FiniteTimeAction} action
* @return {cc.RepeatForever}
* @param {Array|cc.FiniteTimeAction} tempArray
* @example
* var action = new cc.Spawn(cc.jumpBy(2, cc.p(300, 0), 50, 4), cc.rotateBy(2, 720));
*/
cc.RepeatForever.create = cc.repeatForever;
/** Spawn a new action immediately
* @class
* @extends cc.ActionInterval
*/
cc.Spawn = cc.ActionInterval.extend(/** @lends cc.Spawn# */{
_one: null,
_two: null,
/**
* Constructor function, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function.
* @param {Array|cc.FiniteTimeAction} tempArray
*/
ctor: function (tempArray) {
cc.ActionInterval.prototype.ctor.call(this);
this._one = null;
this._two = null;
var i, paramArray, last;
if (tempArray instanceof Array) {
paramArray = tempArray;
}
else {
paramArray = new Array(arguments.length);
for (i = 0; i < arguments.length; ++i) {
paramArray[i] = arguments[i];
}
}
last = paramArray.length - 1;
if ((last >= 0) && (paramArray[last] == null))
cc.log("parameters should not be ending with null in Javascript");
if (last >= 0) {
var prev = paramArray[0], action1;
for (i = 1; i < last; i++) {
if (paramArray[i]) {
action1 = prev;
prev = cc.Spawn._actionOneTwo(action1, paramArray[i]);
}
}
this.initWithTwoActions(prev, paramArray[last]);
}
},
/** initializes the Spawn action with the 2 actions to spawn
* @param {cc.FiniteTimeAction} action1
* @param {cc.FiniteTimeAction} action2
* @return {Boolean}
*/
initWithTwoActions: function (action1, action2) {
if (!action1 || !action2)
throw new Error("cc.Spawn.initWithTwoActions(): arguments must all be non null");
var ret = false;
var d1 = action1._duration;
var d2 = action2._duration;
if (this.initWithDuration(Math.max(d1, d2))) {
this._one = action1;
this._two = action2;
if (d1 > d2) {
this._two = cc.Sequence._actionOneTwo(action2, cc.delayTime(d1 - d2));
} else if (d1 < d2) {
this._one = cc.Sequence._actionOneTwo(action1, cc.delayTime(d2 - d1));
}
ret = true;
}
return ret;
},
/**
* returns a new clone of the action
* @returns {cc.Spawn}
*/
clone: function () {
var action = new cc.Spawn();
this._cloneDecoration(action);
action.initWithTwoActions(this._one.clone(), this._two.clone());
return action;
},
/**
* Start the action with target.
* @param {cc.Node} target
*/
startWithTarget: function (target) {
cc.ActionInterval.prototype.startWithTarget.call(this, target);
this._one.startWithTarget(target);
this._two.startWithTarget(target);
},
/**
* Stop the action
*/
stop: function () {
this._one.stop();
this._two.stop();
cc.Action.prototype.stop.call(this);
},
/**
* Called once per frame. Time is the number of seconds of a frame interval.
* @param {Number} dt
*/
update: function (dt) {
dt = this._computeEaseTime(dt);
if (this._one)
this._one.update(dt);
if (this._two)
this._two.update(dt);
},
/**
* Returns a reversed action.
* @return {cc.Spawn}
*/
reverse: function () {
var action = cc.Spawn._actionOneTwo(this._one.reverse(), this._two.reverse());
this._cloneDecoration(action);
this._reverseEaseList(action);
return action;
}
});
/**
* Create a spawn action which runs several actions in parallel.
* @function
* @param {Array|cc.FiniteTimeAction}tempArray
* @return {cc.Spawn}
* @example
* // example
* var action = cc.spawn(cc.jumpBy(2, cc.p(300, 0), 50, 4), cc.rotateBy(2, 720));
* todo:It should be the direct use new
*/
cc.spawn = function (/*Multiple Arguments*/tempArray) {
var paramArray = (tempArray instanceof Array) ? tempArray : arguments;
if ((paramArray.length > 0) && (paramArray[paramArray.length - 1] == null))
cc.log("parameters should not be ending with null in Javascript");
var prev = paramArray[0];
for (var i = 1; i < paramArray.length; i++) {
if (paramArray[i] != null)
prev = cc.Spawn._actionOneTwo(prev, paramArray[i]);
}
return prev;
};
/**
* Please use cc.spawn instead.
* Create a spawn action which runs several actions in parallel.
* @static
* @deprecated since v3.0 <br /> Please use cc.spawn instead.
* @param {Array|cc.FiniteTimeAction}tempArray
* @return {cc.Spawn}
*/
cc.Spawn.create = cc.spawn;
/**
* @param {cc.FiniteTimeAction} action1
* @param {cc.FiniteTimeAction} action2
* @return {cc.Spawn}
* @private
*/
cc.Spawn._actionOneTwo = function (action1, action2) {
var pSpawn = new cc.Spawn();
pSpawn.initWithTwoActions(action1, action2);
return pSpawn;
};
/**
* Rotates a cc.Node object to a certain angle by modifying it's.
* rotation attribute. <br/>
* The direction will be decided by the shortest angle.
* @class
* @extends cc.ActionInterval
* @param {Number} duration duration in seconds
* @param {Number} deltaAngleX deltaAngleX in degrees.
* @param {Number} [deltaAngleY] deltaAngleY in degrees.
* @example
* var rotateTo = new cc.RotateTo(2, 61.0);
*/
cc.RotateTo = cc.ActionInterval.extend(/** @lends cc.RotateTo# */{
_dstAngleX: 0,
_startAngleX: 0,
_diffAngleX: 0,
_dstAngleY: 0,
_startAngleY: 0,
_diffAngleY: 0,
/**
* Constructor function, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function. <br />
* Creates a RotateTo action with x and y rotation angles.
* @param {Number} duration duration in seconds
* @param {Number} deltaAngleX deltaAngleX in degrees.
* @param {Number} [deltaAngleY] deltaAngleY in degrees.
*/
ctor: function (duration, deltaAngleX, deltaAngleY) {
cc.ActionInterval.prototype.ctor.call(this);
deltaAngleX !== undefined && this.initWithDuration(duration, deltaAngleX, deltaAngleY);
},
/**
* Initializes the action.
* @param {Number} duration
* @param {Number} deltaAngleX
* @param {Number} deltaAngleY
* @return {Boolean}
*/
initWithDuration: function (duration, deltaAngleX, deltaAngleY) {
if (cc.ActionInterval.prototype.initWithDuration.call(this, duration)) {
this._dstAngleX = deltaAngleX || 0;
this._dstAngleY = deltaAngleY !== undefined ? deltaAngleY : this._dstAngleX;
return true;
}
return false;
},
/**
* returns a new clone of the action
* @returns {cc.RotateTo}
*/
clone: function () {
var action = new cc.RotateTo();
this._cloneDecoration(action);
action.initWithDuration(this._duration, this._dstAngleX, this._dstAngleY);
return action;
},
/**
* Start the action with target.
* @param {cc.Node} target
*/
startWithTarget: function (target) {
cc.ActionInterval.prototype.startWithTarget.call(this, target);
// Calculate X
var locStartAngleX = target.rotationX % 360.0;
var locDiffAngleX = this._dstAngleX - locStartAngleX;
if (locDiffAngleX > 180)
locDiffAngleX -= 360;
if (locDiffAngleX < -180)
locDiffAngleX += 360;
this._startAngleX = locStartAngleX;
this._diffAngleX = locDiffAngleX;
// Calculate Y It's duplicated from calculating X since the rotation wrap should be the same
this._startAngleY = target.rotationY % 360.0;
var locDiffAngleY = this._dstAngleY - this._startAngleY;
if (locDiffAngleY > 180)
locDiffAngleY -= 360;
if (locDiffAngleY < -180)
locDiffAngleY += 360;
this._diffAngleY = locDiffAngleY;
},
/**
* RotateTo reverse not implemented.
* Will be overridden.
* @returns {cc.Action}
*/
reverse: function () {
cc.log("cc.RotateTo.reverse(): it should be overridden in subclass.");
},
/**
* Called once per frame. Time is the number of seconds of a frame interval.
* @param {Number} dt
*/
update: function (dt) {
dt = this._computeEaseTime(dt);
if (this.target) {
this.target.rotationX = this._startAngleX + this._diffAngleX * dt;
this.target.rotationY = this._startAngleY + this._diffAngleY * dt;
}
}
});
/**
* Creates a RotateTo action with separate rotation angles.
* To specify the angle of rotation.
* @function
* @param {Number} duration duration in seconds
* @param {Number} deltaAngleX deltaAngleX in degrees.
* @param {Number} [deltaAngleY] deltaAngleY in degrees.
* @return {cc.RotateTo}
* @example
* // example
* var rotateTo = cc.rotateTo(2, 61.0);
*/
cc.rotateTo = function (duration, deltaAngleX, deltaAngleY) {
return new cc.RotateTo(duration, deltaAngleX, deltaAngleY);
};
/**
* Please use cc.rotateTo instead
* Creates a RotateTo action with separate rotation angles.
* To specify the angle of rotation.
* @static
* @deprecated since v3.0 <br /> Please use cc.rotateTo instead.
* @param {Number} duration duration in seconds
* @param {Number} deltaAngleX deltaAngleX in degrees.
* @param {Number} [deltaAngleY] deltaAngleY in degrees.
* @return {cc.RotateTo}
*/
cc.RotateTo.create = cc.rotateTo;
/**
* Rotates a cc.Node object clockwise a number of degrees by modifying it's rotation attribute.
* Relative to its properties to modify.
* @class
* @extends cc.ActionInterval
* @param {Number} duration duration in seconds
* @param {Number} deltaAngleX deltaAngleX in degrees
* @param {Number} [deltaAngleY] deltaAngleY in degrees
* @example
* var actionBy = new cc.RotateBy(2, 360);
*/
cc.RotateBy = cc.ActionInterval.extend(/** @lends cc.RotateBy# */{
_angleX: 0,
_startAngleX: 0,
_angleY: 0,
_startAngleY: 0,
/**
* Constructor function, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function.
* @param {Number} duration duration in seconds
* @param {Number} deltaAngleX deltaAngleX in degrees
* @param {Number} [deltaAngleY] deltaAngleY in degrees
*/
ctor: function (duration, deltaAngleX, deltaAngleY) {
cc.ActionInterval.prototype.ctor.call(this);
deltaAngleX !== undefined && this.initWithDuration(duration, deltaAngleX, deltaAngleY);
},
/**
* Initializes the action.
* @param {Number} duration duration in seconds
* @param {Number} deltaAngleX deltaAngleX in degrees
* @param {Number} [deltaAngleY=] deltaAngleY in degrees
* @return {Boolean}
*/
initWithDuration: function (duration, deltaAngleX, deltaAngleY) {
if (cc.ActionInterval.prototype.initWithDuration.call(this, duration)) {
this._angleX = deltaAngleX || 0;
this._angleY = deltaAngleY || this._angleX;
return true;
}
return false;
},
/**
* returns a new clone of the action
* @returns {cc.RotateBy}
*/
clone: function () {
var action = new cc.RotateBy();
this._cloneDecoration(action);
action.initWithDuration(this._duration, this._angleX, this._angleY);
return action;
},
/**
* Start the action with target.
* @param {cc.Node} target
*/
startWithTarget: function (target) {
cc.ActionInterval.prototype.startWithTarget.call(this, target);
this._startAngleX = target.rotationX;
this._startAngleY = target.rotationY;
},
/**
* Called once per frame. Time is the number of seconds of a frame interval.
* @param {Number} dt
*/
update: function (dt) {
dt = this._computeEaseTime(dt);
if (this.target) {
this.target.rotationX = this._startAngleX + this._angleX * dt;
this.target.rotationY = this._startAngleY + this._angleY * dt;
}
},
/**
* Returns a reversed action.
* @return {cc.RotateBy}
*/
reverse: function () {
var action = new cc.RotateBy(this._duration, -this._angleX, -this._angleY);
this._cloneDecoration(action);
this._reverseEaseList(action);
return action;
}
});
/**
* Rotates a cc.Node object clockwise a number of degrees by modifying it's rotation attribute.
* Relative to its properties to modify.
* @function
* @param {Number} duration duration in seconds
* @param {Number} deltaAngleX deltaAngleX in degrees
* @param {Number} [deltaAngleY] deltaAngleY in degrees
* @return {cc.RotateBy}
* @example
* // example
* var actionBy = cc.rotateBy(2, 360);
*/
cc.rotateBy = function (duration, deltaAngleX, deltaAngleY) {
return new cc.RotateBy(duration, deltaAngleX, deltaAngleY);
};
/**
* Please use cc.rotateBy instead.
* Rotates a cc.Node object clockwise a number of degrees by modifying it's rotation attribute.
* Relative to its properties to modify.
* @static
* @deprecated since v3.0 <br /> Please use cc.rotateBy instead.
* @param {Number} duration duration in seconds
* @param {Number} deltaAngleX deltaAngleX in degrees
* @param {Number} [deltaAngleY] deltaAngleY in degrees
* @return {cc.RotateBy}
*/
cc.RotateBy.create = cc.rotateBy;
/**
* <p>
* Moves a CCNode object x,y pixels by modifying it's position attribute. <br/>
* x and y are relative to the position of the object. <br/>
* Several CCMoveBy actions can be concurrently called, and the resulting <br/>
* movement will be the sum of individual movements.
* </p>
* @class
* @extends cc.ActionInterval
* @param {Number} duration duration in seconds
* @param {cc.Point|Number} deltaPos
* @param {Number} [deltaY]
* @example
* var actionBy = cc.moveBy(2, cc.p(windowSize.width - 40, windowSize.height - 40));
*/
cc.MoveBy = cc.ActionInterval.extend(/** @lends cc.MoveBy# */{
_positionDelta: null,
_startPosition: null,
_previousPosition: null,
/**
* Constructor function, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function.
* @param {Number} duration duration in seconds
* @param {cc.Point|Number} deltaPos
* @param {Number} [deltaY]
*/
ctor: function (duration, deltaPos, deltaY) {
cc.ActionInterval.prototype.ctor.call(this);
this._positionDelta = cc.p(0, 0);
this._startPosition = cc.p(0, 0);
this._previousPosition = cc.p(0, 0);
deltaPos !== undefined && this.initWithDuration(duration, deltaPos, deltaY);
},
/**
* Initializes the action.
* @param {Number} duration duration in seconds
* @param {cc.Point} position
* @param {Number} [y]
* @return {Boolean}
*/
initWithDuration: function (duration, position, y) {
if (cc.ActionInterval.prototype.initWithDuration.call(this, duration)) {
if (position.x !== undefined) {
y = position.y;
position = position.x;
}
this._positionDelta.x = position;
this._positionDelta.y = y;
return true;
}
return false;
},
/**
* returns a new clone of the action
* @returns {cc.MoveBy}
*/
clone: function () {
var action = new cc.MoveBy();
this._cloneDecoration(action);
action.initWithDuration(this._duration, this._positionDelta);
return action;
},
/**
* Start the action with target.
* @param {cc.Node} target
*/
startWithTarget: function (target) {
cc.ActionInterval.prototype.startWithTarget.call(this, target);
var locPosX = target.getPositionX();
var locPosY = target.getPositionY();
this._previousPosition.x = locPosX;
this._previousPosition.y = locPosY;
this._startPosition.x = locPosX;
this._startPosition.y = locPosY;
},
/**
* Called once per frame. Time is the number of seconds of a frame interval.
* @param {Number} dt
*/
update: function (dt) {
dt = this._computeEaseTime(dt);
if (this.target) {
var x = this._positionDelta.x * dt;
var y = this._positionDelta.y * dt;
var locStartPosition = this._startPosition;
if (cc.ENABLE_STACKABLE_ACTIONS) {
var targetX = this.target.getPositionX();
var targetY = this.target.getPositionY();
var locPreviousPosition = this._previousPosition;
locStartPosition.x = locStartPosition.x + targetX - locPreviousPosition.x;
locStartPosition.y = locStartPosition.y + targetY - locPreviousPosition.y;
x = x + locStartPosition.x;
y = y + locStartPosition.y;
locPreviousPosition.x = x;
locPreviousPosition.y = y;
this.target.setPosition(x, y);
} else {
this.target.setPosition(locStartPosition.x + x, locStartPosition.y + y);
}
}
},
/**
* MoveTo reverse is not implemented
* @return {cc.MoveBy}
*/
reverse: function () {
var action = new cc.MoveBy(this._duration, cc.p(-this._positionDelta.x, -this._positionDelta.y));
this._cloneDecoration(action);
this._reverseEaseList(action);
return action;
}
});
/**
* Create the action.
* Relative to its coordinate moves a certain distance.
* @function
* @param {Number} duration duration in seconds
* @param {cc.Point|Number} deltaPos
* @param {Number} deltaY
* @return {cc.MoveBy}
* @example
* // example
* var actionBy = cc.moveBy(2, cc.p(windowSize.width - 40, windowSize.height - 40));
*/
cc.moveBy = function (duration, deltaPos, deltaY) {
return new cc.MoveBy(duration, deltaPos, deltaY);
};
/**
* Please use cc.moveBy instead.
* Relative to its coordinate moves a certain distance.
* @static
* @deprecated since v3.0 please use cc.moveBy instead.
* @param {Number} duration duration in seconds
* @param {cc.Point|Number} deltaPos
* @param {Number} deltaY
* @return {cc.MoveBy}
*/
cc.MoveBy.create = cc.moveBy;
/**
* Moves a CCNode object to the position x,y. x and y are absolute coordinates by modifying it's position attribute. <br/>
* Several CCMoveTo actions can be concurrently called, and the resulting <br/>
* movement will be the sum of individual movements.
* @class
* @extends cc.MoveBy
* @param {Number} duration duration in seconds
* @param {cc.Point|Number} position
* @param {Number} y
* @example
* var actionTo = new cc.MoveTo(2, cc.p(80, 80));
*/
cc.MoveTo = cc.MoveBy.extend(/** @lends cc.MoveTo# */{
_endPosition: null,
/**
* Constructor function, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function.
* @param {Number} duration duration in seconds
* @param {cc.Point|Number} position
* @param {Number} y
*/
ctor: function (duration, position, y) {
cc.MoveBy.prototype.ctor.call(this);
this._endPosition = cc.p(0, 0);
position !== undefined && this.initWithDuration(duration, position, y);
},
/**
* Initializes the action.
* @param {Number} duration duration in seconds
* @param {cc.Point} position
* @param {Number} y
* @return {Boolean}
*/
initWithDuration: function (duration, position, y) {
if (cc.MoveBy.prototype.initWithDuration.call(this, duration, position, y)) {
if (position.x !== undefined) {
y = position.y;
position = position.x;
}
this._endPosition.x = position;
this._endPosition.y = y;
return true;
}
return false;
},
/**
* returns a new clone of the action
* @returns {cc.MoveTo}
*/
clone: function () {
var action = new cc.MoveTo();
this._cloneDecoration(action);
action.initWithDuration(this._duration, this._endPosition);
return action;
},
/**
* Start the action with target.
* @param {cc.Node} target
*/
startWithTarget: function (target) {
cc.MoveBy.prototype.startWithTarget.call(this, target);
this._positionDelta.x = this._endPosition.x - target.getPositionX();
this._positionDelta.y = this._endPosition.y - target.getPositionY();
}
});
/**
* Create new action.
* Moving to the specified coordinates.
* @function
* @param {Number} duration duration in seconds
* @param {cc.Point|Number} position
* @param {Number} y
* @return {cc.MoveTo}
* @example
* // example
* var actionTo = cc.moveTo(2, cc.p(80, 80));
*/
cc.moveTo = function (duration, position, y) {
return new cc.MoveTo(duration, position, y);
};
/**
* Please use cc.moveTo instead.
* Moving to the specified coordinates.
* @static
* @deprecated since v3.0 <br /> Please use cc.moveTo instead.
* @param {Number} duration duration in seconds
* @param {cc.Point|Number} position
* @param {Number} y
* @return {cc.MoveTo}
*/
cc.MoveTo.create = cc.moveTo;
/**
* Skews a cc.Node object to given angles by modifying it's skewX and skewY attributes
* @class
* @extends cc.ActionInterval
* @param {Number} t time in seconds
* @param {Number} sx
* @param {Number} sy
* @example
* var actionTo = new cc.SkewTo(2, 37.2, -37.2);
*/
cc.SkewTo = cc.ActionInterval.extend(/** @lends cc.SkewTo# */{
_skewX: 0,
_skewY: 0,
_startSkewX: 0,
_startSkewY: 0,
_endSkewX: 0,
_endSkewY: 0,
_deltaX: 0,
_deltaY: 0,
/**
* Constructor function, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function.
* @param {Number} t time in seconds
* @param {Number} sx
* @param {Number} sy
*/
ctor: function (t, sx, sy) {
cc.ActionInterval.prototype.ctor.call(this);
sy !== undefined && this.initWithDuration(t, sx, sy);
},
/**
* Initializes the action.
* @param {Number} t time in seconds
* @param {Number} sx
* @param {Number} sy
* @return {Boolean}
*/
initWithDuration: function (t, sx, sy) {
var ret = false;
if (cc.ActionInterval.prototype.initWithDuration.call(this, t)) {
this._endSkewX = sx;
this._endSkewY = sy;
ret = true;
}
return ret;
},
/**
* returns a new clone of the action
* @returns {cc.SkewTo}
*/
clone: function () {
var action = new cc.SkewTo();
this._cloneDecoration(action);
action.initWithDuration(this._duration, this