/*
* Moves a Node object simulating a parabolic jump movement by modifying its position property.
* Relative to its movement.
* @class JumpBy
* @extends ActionInterval
* @param {Number} duration
* @param {Vec2|Number} position
* @param {Number} [y]
* @param {Number} height
* @param {Number} jumps
* @example
* var actionBy = new cc.JumpBy(2, cc.v2(300, 0), 50, 4);
* var actionBy = new cc.JumpBy(2, 300, 0, 50, 4);
*/
/*
* 通过修改 Node 对象的位置属性来移动模拟抛物线跳跃运动的 Node 对象。
* 相对于它的运动。
* @class JumpBy
* @extends ActionInterval
* @param {Number} 持续时间
* @param {Vec2|Number} 位置
* @param {数字} [y]
* @param {Number} 高度
* @param {Number} 跳转
* @例子
* var actionBy = new cc.JumpBy(2, cc.v2(300, 0), 50, 4);
* var actionBy = new cc.JumpBy(2, 300, 0, 50, 4);
*/
cc.JumpBy = cc.Class({
name: 'cc.JumpBy',
extends: cc.ActionInterval,
ctor: function (duration, position, y, height, jumps) {
/* 构造函数,初始化,开始位置,上个jump前的位置 */
this._startPosition = cc.v2(0, 0);
this._previousPosition = cc.v2(0, 0);
this._delta = cc.v2(0, 0);
this._height = 0;
this._jumps = 0;
height !== undefined && cc.JumpBy.prototype.initWithDuration.call(this, duration, position, y, height, jumps);
},
/*
* Initializes the action.
* @param {Number} duration
* @param {Vec2|Number} position
* @param {Number} [y]
* @param {Number} height
* @param {Number} jumps
* @return {Boolean}
* @example
* actionBy.initWithDuration(2, cc.v2(300, 0), 50, 4);
* actionBy.initWithDuration(2, 300, 0, 50, 4);
*/
/*
* 初始化操作。
* @param {Number} 持续时间
* @param {Vec2|Number} 位置
* @param {数字} [y]
* @param {Number} 高度
* @param {Number} 跳转
* @return {布尔值}
* @例子
* actionBy.initWithDuration(2, cc.v2(300, 0), 50, 4);
* actionBy.initWithDuration(2, 300, 0, 50, 4);
*/
initWithDuration: function (duration, position, y, height, jumps) {
if (cc.ActionInterval.prototype.initWithDuration.call(this, duration)) {
if (jumps === undefined) {
jumps = height;
height = y;
y = position.y;
position = position.x;
}
this._delta.x = position;
this._delta.y = y;
this._height = height;
this._jumps = jumps;
return true;
}
return false;
},
/* 复制action */
clone: function () {
var action = new cc.JumpBy();
this._cloneDecoration(action);
action.initWithDuration(this._duration, this._delta, this._height, this._jumps);
return action;
},
/* 设置node的target */
startWithTarget: function (target) {
cc.ActionInterval.prototype.startWithTarget.call(this, target);
var locPosX = target.x;
var locPosY = target.y;
this._previousPosition.x = locPosX;
this._previousPosition.y = locPosY;
this._startPosition.x = locPosX;
this._startPosition.y = locPosY;
},
/* 时时更新 */
update: function (dt) {
dt = this._computeEaseTime(dt);
if (this.target) {
var frac = dt * this._jumps % 1.0;
var y = this._height * 4 * frac * (1 - frac);
y += this._delta.y * dt;
var x = this._delta.x * dt;
var locStartPosition = this._startPosition;
if (cc.macro.ENABLE_STACKABLE_ACTIONS) {
var targetX = this.target.x;
var targetY = this.target.y;
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);
}
}
},
/* 反向action */
reverse: function () {
var action = new cc.JumpBy(this._duration, cc.v2(-this._delta.x, -this._delta.y), this._height, this._jumps);
this._cloneDecoration(action);
this._reverseEaseList(action);
return action;
}
});
/**
* !#en
* Moves a Node object simulating a parabolic jump movement by modifying it's position property.
* Relative to its movement.
* !#zh 用跳跃的方式移动指定的距离。
* @method jumpBy
* @param {Number} duration
* @param {Vec2|Number} position
* @param {Number} [y]
* @param {Number} [height]
* @param {Number} [jumps]
* @return {ActionInterval}
* @example
* // example
* var actionBy = cc.jumpBy(2, cc.v2(300, 0), 50, 4);
* var actionBy = cc.jumpBy(2, 300, 0, 50, 4);
*/
/**
* !#en
* Moves a Node object simulating a parabolic jump movement by modifying it's position property.
* Relative to its movement.
* !#zh 用跳跃的方式移动指定的距离。
* @method jumpBy
* @param {Number} duration
* @param {Vec2|Number} position
* @param {Number} [y]
* @param {Number} [height]
* @param {Number} [jumps]
* @return {ActionInterval}
* @example
*
*
*
*/
/* 创建一个 jumpBy 对象*/
cc.jumpBy = function (duration, position, y, height, jumps) {
return new cc.JumpBy(duration, position, y, height, jumps);
};