CocosCreator 源码cc.moveBy详解
欢迎关注公众号:“Cocos Creator 源码讲解”,一起学习。
/** <p>* Moves a Node object x,y pixels by modifying its position property. <br/>* x and y are relative to the position of the object. <br/>* Several MoveBy actions can be concurrently called, and the resulting <br/>* movement will be the sum of individual movements.* </p>* @class MoveBy* @extends ActionInterval* @param {Number} duration duration in seconds* @param {Vec2|Number} deltaPos* @param {Number} [deltaY]* @example* var actionTo = cc.moveBy(2, cc.v2(windowSize.width - 40, windowSize.height - 40));*/
/** <p>* 通过修改 Node 对象的位置属性来移动 Node 对象 x,y 像素。 <br/>* x 和 y 相对于对象的位置。 <br/>* 可以同时调用多个 MoveBy 操作,结果<br/>* 运动将是个体运动的总和。* </p>* @类MoveBy* @extends ActionInterval* @param {Number} 持续时间 持续时间(以秒为单位)* @param {Vec2|Number} deltaPos* @param {数字} [deltaY]* @例子* var actionTo = cc.moveBy(2, cc.v2(windowSize.width -40, windowSize.height -40));*/
cc.MoveBy = cc.Class({name: 'cc.MoveBy',extends: cc.ActionInterval,ctor: function (duration, deltaPos, deltaY) {this._positionDelta = cc.v2(0, 0);this._startPosition = cc.v2(0, 0);this._previousPosition = cc.v2(0, 0);deltaPos !== undefined && cc.MoveBy.prototype.initWithDuration.call(this, duration, deltaPos, deltaY);},/** Initializes the action.* @param {Number} duration duration in seconds* @param {Vec2} position* @param {Number} [y]* @return {Boolean}*//* /** 初始化操作。* @param {Number} 持续时间 持续时间(以秒为单位)* @param {Vec2} 位置* @param {数字} [y]* @return {布尔值}*/ */initWithDuration: function (duration, position, y) {if (cc.ActionInterval.prototype.initWithDuration.call(this, duration)) {/* 当position参数包含x属性,证明是一个position,重置两个参数,第二个参数设置为位置的x,第三个参数设置为位置的y */if (position.x !== undefined) {y = position.y;position = position.x;}this._positionDelta.x = position;this._positionDelta.y = y;return true;}return false;},/* 复制moveBy */clone: function () {var action = new cc.MoveBy();this._cloneDecoration(action);action.initWithDuration(this._duration, this._positionDelta);return action;},/* 初始化target参数传入 */startWithTarget: function (target) {cc.ActionInterval.prototype.startWithTarget.call(this, target);/* 设置开始的位置,x ,y */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) {/* 总计划移动位置的x值,乘以dt,y同理 */var x = this._positionDelta.x * dt;var y = this._positionDelta.y * dt;/* 获取target的位置 */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);}}},/* 反向move */reverse: function () {var action = new cc.MoveBy(this._duration, cc.v2(-this._positionDelta.x, -this._positionDelta.y));this._cloneDecoration(action);this._reverseEaseList(action);return action;}
});/*** !#en* Moves a Node object x,y pixels by modifying its position property. <br/>* x and y are relative to the position of the object. <br/>* Several MoveBy actions can be concurrently called, and the resulting <br/>* movement will be the sum of individual movements.* !#zh 移动指定的距离。* @method moveBy* @param {Number} duration duration in seconds* @param {Vec2|Number} deltaPos* @param {Number} [deltaY]* @return {ActionInterval}* @example* // example* var actionTo = cc.moveBy(2, cc.v2(windowSize.width - 40, windowSize.height - 40));*/
/* 创建一个 moveBy对象 */
cc.moveBy = function (duration, deltaPos, deltaY) {return new cc.MoveBy(duration, deltaPos, deltaY);
};