CocosCreator 源码.CCAction详解

/****************************************************************************
 Copyright (c) 2008-2010 Ricardo Quesada
 Copyright (c) 2011-2012 cocos2d-x.org
 Copyright (c) 2013-2016 Chukong Technologies Inc.
 Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.

 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.
 ****************************************************************************/

require('../core/platform/CCClass');
const misc = require('../core/utils/misc');

/**
 * @module cc
 */

/**
 * !#en Base class cc.Action for action classes.
 * !#zh Action 类是所有动作类型的基类。
 * @class Action
 */
cc.Action = cc.Class({
    name: 'cc.Action',

    //**************Public Functions***********

    ctor: function () {
        /* 构造函数-初始化属性 */
        /* target--node */
        this.originalTarget = null;
        this.target = null;
        this.tag = cc.Action.TAG_INVALID;//-1
    },

    /**
     * !#en
     * to copy object with deep copy.
     * returns a clone of action.
     * !#zh 返回一个克隆的动作。
     * @method clone
     * @return {Action}
     */
    clone: function () {
        /* new 一个action */
        var action = new cc.Action();
        action.originalTarget = null;
        action.target = null;
        action.tag = this.tag;
        return action;
    },

    /**
     * !#en
     * return true if the action has finished.
     * !#zh 如果动作已完成就返回 true。
     * @method isDone
     * @return {Boolean}
     */
    isDone: function () {
        return true;
    },

    // called before the action start. It will also set the target.
    /* 在操作开始之前调用。它还将设定目标。 */
    startWithTarget: function (target) {
        this.originalTarget = target;
        this.target = target;
    },

    // called after the action has finished. It will set the 'target' to nil.
    // 操作完成后调用。它将把“目标”设置为null。
    stop: function () {
        this.target = null;
    },

    // called every frame with it's delta time. <br />
    // 每一帧都调用,通过它的增量时间。 <
    step: function (dt) {
        cc.logID(1006);
    },

    // Called once per frame. Time is the number of seconds of a frame interval.
    /* 每帧调用一次。时间是帧间隔的秒数。 */
    update: function (dt) {
        cc.logID(1007);
    },

    /**
     * !#en get the target.
     * !#zh 获取当前目标节点。
     * @method getTarget
     * @return {Node}
     */
    /* 获取当前目标节点。 */
    getTarget: function () {
        return this.target;
    },

    /**
     * !#en The action will modify the target properties.
     * !#zh 设置目标节点。
     * @method setTarget
     * @param {Node} target
     */
    /* 设置目标节点。 */
    setTarget: function (target) {
        this.target = target;
    },

    /**
     * !#en get the original target.
     * !#zh 获取原始目标节点。
     * @method getOriginalTarget
     * @return {Node}
     */
    /* 获取原始目标节点。 */
    getOriginalTarget: function () {
        return this.originalTarget;
    },

    // Set the original target, since target can be nil.
    // Is the target that were used to run the action.
    // Unless you are doing something complex, like cc.ActionManager, you should NOT call this method.
    /* // 设置原始目标,因为目标可以为null。
        // 是用于运行操作的目标。
        // 除非您正在做一些复杂的事情,例如 cc.ActionManager,否则您不应该调用此方法。 */
    setOriginalTarget: function (originalTarget) {
        this.originalTarget = originalTarget;
    },

    /**
     * !#en get tag number.
     * !#zh 获取用于识别动作的标签。
     * @method getTag
     * @return {Number}
     */
    /* 获取用于识别动作的标签。 */
    getTag: function () {
        return this.tag;
    },

    /**
     * !#en set tag number.
     * !#zh 设置标签,用于识别动作。
     * @method setTag
     * @param {Number} tag
     */
    /* 设置标签,用于识别动作。 */
    setTag: function (tag) {
        this.tag = tag;
    },

    // Currently JavaScript Bindigns (JSB), in some cases, needs to use retain and release. This is a bug in JSB,
    // and the ugly workaround is to use retain/release. So, these 2 methods were added to be compatible with JSB.
    // This is a hack, and should be removed once JSB fixes the retain/release bug.

    /* // 目前JavaScript Bindigns(JSB)在某些情况下需要使用retain和release。这是 JSB 中的一个错误,
    // 不好的的解决方法是使用保留/释放。因此,添加这两个方法是为了兼容 JSB。
    // 这是一个 hack,一旦 JSB 修复了保留/释放错误,就应该将其删除。 */
    retain: function () {
    },

    // Currently JavaScript Bindigns (JSB), in some cases, needs to use retain and release. This is a bug in JSB,
    // and the ugly workaround is to use retain/release. So, these 2 methods were added to be compatible with JSB.
    // This is a hack, and should be removed once JSB fixes the retain/release bug.
    release: function () {
    }
});

/**
 * !#en Default Action tag.
 * !#zh 默认动作标签。
 * @property TAG_INVALID
 * @constant
 * @static
 * @type {Number}
 * @default -1
 */
/* 默认动作标签。 */
cc.Action.TAG_INVALID = -1;


/**
 * !#en
 * Base class actions that do have a finite time duration. <br/>
 * Possible actions: <br/>
 * - An action with a duration of 0 seconds. <br/>
 * - An action with a duration of 35.5 seconds.
 *
 * Infinite time actions are valid
 * !#zh 有限时间动作,这种动作拥有时长 duration 属性。
 * @class FiniteTimeAction
 * @extends Action
 */
cc.FiniteTimeAction = cc.Class({
    name: 'cc.FiniteTimeAction',
    extends: cc.Action,

    /* 构造函数 */
    ctor: function () {
        //! duration in seconds
        this._duration = 0;
    },

    /**
     * !#en get duration of the action. (seconds).
     * !#zh 获取动作以秒为单位的持续时间。
     * @method getDuration
     * @return {Number}
     */
    /* 动作的总时间 = 设置的时间*循环次数 */
    getDuration: function () {
        return this._duration * (this._timesForRepeat || 1);
    },

    /**
     * !#en set duration of the action. (seconds).
     * !#zh 设置动作以秒为单位的持续时间。
     * @method setDuration
     * @param {Number} duration
     */
    /* 设置动作以秒为单位的持续时间。 */
    setDuration: function (duration) {
        this._duration = duration;
    },

    /**
     * !#en
     * Returns a reversed action. <br />
     * For example: <br />
     * - The action will be x coordinates of 0 move to 100. <br />
     * - The reversed action will be x of 100 move to 0.
     * - Will be rewritten
     * !#zh 返回一个新的动作,执行与原动作完全相反的动作。
     * @method reverse
     * @return {Null}
     */
    /* 返回一个新的动作,执行与原动作完全相反的动作。 */
    reverse: function () {
        cc.logID(1008);
        return null;
    },

    /**
     * !#en
     * to copy object with deep copy.
     * returns a clone of action.
     * !#zh 返回一个克隆的动作。
     * @method clone
     * @return {FiniteTimeAction}
     */
    /* 返回一个克隆的动作。 */
    clone: function () {
        return new cc.FiniteTimeAction();
    }
});

/**
 * @module cc
 */

/*
 * 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.
 *
 * @warning This action can't be Sequenceable because it is not an cc.IntervalAction
 * @class Speed
 * @extends Action
 *
 * @param {ActionInterval} action
 * @param {Number} speed
 */
/* /*
 * 改变动作的速度,使其花费更长的时间(速度> 1)
 * 或更短(速度<1)时间。 <br/>
 * 可用于模拟“慢动作”或“快进”效果。
 *
 * @警告此操作不能进行 Sequenceable,因为它不是 cc.IntervalAction
 * @等级速度
 * @extends 动作
 * */
cc.Speed = cc.Class({
    name: 'cc.Speed',
    extends: cc.Action,

    /* 构造函数 */
    ctor: function (action, speed) {
        this._speed = 0;
        this._innerAction = null;
        /* 确认action参数不为空,初始化action 和参数 速度 */
        action && this.initWithAction(action, speed);
    },

    /*
     * Gets the current running speed. <br />
     * Will get a percentage number, compared to the original speed.
     *
     * @method getSpeed
     * @return {Number}
     */
    /* 在initWithAction的时候初始化了_speed参数,直接获取 */
    getSpeed: function () {
        return this._speed;
    },

    /*
     * alter the speed of the inner function in runtime.
     * @method setSpeed
     * @param {Number} speed
     */
    /* 设置speed */
    setSpeed: function (speed) {
        this._speed = speed;
    },

    /*
     * initializes the action.
     * @method initWithAction
     * @param {ActionInterval} action
     * @param {Number} speed
     * @return {Boolean}
     */
    initWithAction: function (action, speed) {
        if (!action) {
            cc.errorID(1021);
            return false;
        }

        /* 初始化参数 action 和speed */
        this._innerAction = action;
        this._speed = speed;
        return true;
    },

    /* 复制ccSpeed */
    clone: function () {
        var action = new cc.Speed();
        action.initWithAction(this._innerAction.clone(), this._speed);
        return action;
    },
    /* 调用startWithTarget 设置target */
    startWithTarget: function (target) {
        cc.Action.prototype.startWithTarget.call(this, target);
        /* 已经初始化的action执行set target */
        this._innerAction.startWithTarget(target);
    },

    stop: function () {
        /* 执行action的stop函数 */
        this._innerAction.stop();
        /* 父类Action执行stop */
        cc.Action.prototype.stop.call(this);
    },

    step: function (dt) {
        /* 执行每一步,调用step函数,参数为是先间隔 * speed */
        this._innerAction.step(dt * this._speed);
    },

    isDone: function () {
        /* 是否播放完毕 */
        return this._innerAction.isDone();
    },
    /* 返回一个新的 action 反向的action */
    reverse: function () {
        return new cc.Speed(this._innerAction.reverse(), this._speed);
    },

    /*
     * Set inner Action.
     * @method setInnerAction
     * @param {ActionInterval} action
     */
    /* 设置当前class的global参数 _innderAction */
    setInnerAction: function (action) {
        if (this._innerAction !== action) {
            this._innerAction = action;
        }
    },

    /*
     * Get inner Action.
     * @method getInnerAction
     * @return {ActionInterval}
     */
    /* 获取当前action */
    getInnerAction: function () {
        return this._innerAction;
    }
});

/**
 * @module cc
 */

/**
 * !#en
 * Creates the speed action which 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.
 * !#zh 修改目标动作的速率。
 * @warning This action can't be Sequenceable because it is not an cc.IntervalAction
 *
 * @method speed
 * @param {ActionInterval} action
 * @param {Number} speed
 * @return {Action}
 * @example
 * // change the target action speed;
 * var action = cc.scaleTo(0.2, 1, 0.6);
 * var newAction = cc.speed(action, 0.5);
 */
cc.speed = function (action, speed) {
    return new cc.Speed(action, speed);
};

/*
 * cc.Follow is a follow action which makes its target follows another node.
 *
 * @example
 * //example
 * //Instead of using cc.Camera as a "follower", use this action instead.
 * layer.runAction(cc.follow(hero));
 *
 * @property {Number}  leftBoundary - world leftBoundary.
 * @property {Number}  rightBoundary - world rightBoundary.
 * @property {Number}  topBoundary - world topBoundary.
 * @property {Number}  bottomBoundary - world bottomBoundary.
 *
 * @param {cc.Node} followedNode
 * @param {Rect} rect
 * @example
 * // creates the action with a set boundary
 * var followAction = new cc.Follow(node, cc.rect(0, 0, s.width * 2 - 100, s.height));
 * this.runAction(followAction);
 *
 * // creates the action with no boundary set
 * var followAction = new cc.Follow(node);
 * this.runAction(followAction);
 *
 * @class
 * @extends Action
 */
/* 是一个跟随操作,使其目标跟随另一个节点 */
cc.Follow = cc.Class({
    name: 'cc.Follow',
    extends: cc.Action,

    /*
     * Constructor function, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function. <br />
     * creates the action with a set boundary. <br/>
     * creates the action with no boundary set.
     * @param {cc.Node} followedNode
     * @param {Rect} rect
     */
    /* 构造函数
    * 构造函数,重写它来扩展构造行为,记得在扩展的“ctor”函数中调用“this._super()”。 <br/>
     * 创建具有设定边界的动作。 <br/>
     * 创建没有设置边界的动作。
     * @param {cc.Node} followNode
     * @param {Rect} 矩形
     *  */
    ctor: function (followedNode, rect) {
        // node to follow
        /* 要跟随的节点 */
        this._followedNode = null;
        // whether camera should be limited to certain area
        /* 相机是否应限制在特定区域 */
        this._boundarySet = false;
        // if screen size is bigger than the boundary - update not needed
        /* 如果屏幕尺寸大于边界 -不需要更新 */
        this._boundaryFullyCovered = false;

        // fast access to the screen dimensions
        /* 快速访问屏幕尺寸 */
        this._halfScreenSize = null;
        this._fullScreenSize = null;

        /* 左边界 */
        this.leftBoundary = 0.0;
        /* 右边界 */
        this.rightBoundary = 0.0;
        this.topBoundary = 0.0;
        this.bottomBoundary = 0.0;
        this._worldRect = cc.rect(0, 0, 0, 0);

        /* 存在要跟随的node的时候,进行初始化,node和react */
        if (followedNode)
            rect ? this.initWithTarget(followedNode, rect)
                : this.initWithTarget(followedNode);
    },

    /* 复制action */
    clone: function () {
        var action = new cc.Follow();
        var locRect = this._worldRect;
        /* 创建一个矩形区域 */
        var rect = new cc.Rect(locRect.x, locRect.y, locRect.width, locRect.height);
        /* 初始化action对应的参数 */
        action.initWithTarget(this._followedNode, rect);
        return action;
    },

    /*
     * Get whether camera should be limited to certain area.
     *
     * @return {Boolean}
     */
    /*      * 获取相机是否应限制在特定区域。 */
    isBoundarySet: function () {
        return this._boundarySet;
    },

    /*
     * alter behavior - turn on/off boundary.
     *
     * @param {Boolean} value
     */
    /* -打开/关闭边界 */
    setBoudarySet: function (value) {
        this._boundarySet = value;
    },

    /*
     * initializes the action with a set boundary.
     *
     * @param {cc.Node} followedNode
     * @param {Rect} [rect=]
     * @return {Boolean}
     */
    /* 使用设定的边界初始化操作。 */
    initWithTarget: function (followedNode, rect) {
        /* follow node不能为空 */
        if (!followedNode) {
            cc.errorID(1022);
            return false;
        }

        var _this = this;
        rect = rect || cc.rect(0, 0, 0, 0);
        _this._followedNode = followedNode;
        _this._worldRect = rect;

        /* 只要有一个参数不为0,就是true */
        _this._boundarySet = !(rect.width === 0 && rect.height === 0);

        _this._boundaryFullyCovered = false;

        /* 屏幕尺寸 */
        var winSize = cc.winSize;
        /* 整屏幕size */
        _this._fullScreenSize = cc.v2(winSize.width, winSize.height);
        /* 逐元素向量乘法,获取半屏size */
        _this._halfScreenSize = _this._fullScreenSize.mul(0.5);

        if (_this._boundarySet) {
            _this.leftBoundary = -((rect.x + rect.width) - _this._fullScreenSize.x);
            _this.rightBoundary = -rect.x;
            _this.topBoundary = -rect.y;
            _this.bottomBoundary = -((rect.y + rect.height) - _this._fullScreenSize.y);

            if (_this.rightBoundary < _this.leftBoundary) {
                // screen width is larger than world's boundary width
                //set both in the middle of the world
                /* // 屏幕宽度大于世界边界宽度
                    //将两者都设置在世界的中间 */
                _this.rightBoundary = _this.leftBoundary = (_this.leftBoundary + _this.rightBoundary) / 2;
            }
            if (_this.topBoundary < _this.bottomBoundary) {
                // screen width is larger than world's boundary width
                //set both in the middle of the world
                /* // 屏幕宽度大于世界边界宽度
                    //将两者都设置在世界的中间 */
                _this.topBoundary = _this.bottomBoundary = (_this.topBoundary + _this.bottomBoundary) / 2;
            }

            if ((_this.topBoundary === _this.bottomBoundary) && (_this.leftBoundary === _this.rightBoundary))
                _this._boundaryFullyCovered = true;
        }
        return true;
    },

    step: function (dt) {
        var targetWorldPos = this.target.convertToWorldSpaceAR(cc.Vec2.ZERO);
        var followedWorldPos = this._followedNode.convertToWorldSpaceAR(cc.Vec2.ZERO);
        // compute the offset between followed and target node
        var delta = targetWorldPos.sub(followedWorldPos);
        var tempPos = this.target.parent.convertToNodeSpaceAR(delta.add(this._halfScreenSize));

        if (this._boundarySet) {
            // whole map fits inside a single screen, no need to modify the position - unless map boundaries are increased
            if (this._boundaryFullyCovered)
                return;

            this.target.setPosition(misc.clampf(tempPos.x, this.leftBoundary, this.rightBoundary), misc.clampf(tempPos.y, this.bottomBoundary, this.topBoundary));
        } else {
            this.target.setPosition(tempPos.x, tempPos.y);
        }
    },

    /* 是否播放完成 */
    isDone: function () {
        return (!this._followedNode.activeInHierarchy);
    },
    /* 停止播放,target设置为null */
    stop: function () {
        this.target = null;
        cc.Action.prototype.stop.call(this);
    }
});

/**
 * !#en Create a follow action which makes its target follows another node.
 * !#zh 追踪目标节点的位置。
 * @method follow
 * @param {Node} followedNode
 * @param {Rect} rect
 * @return {Action|Null} returns the cc.Follow object on success
 * @example
 * // example
 * // creates the action with a set boundary
 * var followAction = cc.follow(targetNode, cc.rect(0, 0, screenWidth * 2 - 100, screenHeight));
 * node.runAction(followAction);
 *
 * // creates the action with no boundary set
 * var followAction = cc.follow(targetNode);
 * node.runAction(followAction);
 */
/* cc.Follow实现镜头跟随的功能 */
cc.follow = function (followedNode, rect) {
    return new cc.Follow(followedNode, rect);
};

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值