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);
};

  • 10
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值