CocosCreator 源码​CCTouch.js详解

/**
 * !#en The touch event class
 * !#zh 封装了触摸相关的信息。
 * @class Touch
 *
 * @param {Number} x
 * @param {Number} y
 * @param {Number} id
 */
cc.Touch = function (x, y, id) {
    this._lastModified = 0;
    this.setTouchInfo(id, x, y);
};
cc.Touch.prototype = {
    constructor: cc.Touch,
    /**
     * !#en Returns the current touch location in OpenGL coordinates.、
     * !#zh 获取当前触点位置。
     * @method getLocation
     * @return {Vec2}
     */
    getLocation:function () {
        return cc.v2(this._point.x, this._point.y);
    },

  /**
   * !#en Returns X axis location value.
     * !#zh 获取当前触点 X 轴位置。
     * @method getLocationX
   * @returns {Number}
   */
  getLocationX: function () {
    return this._point.x;
  },

  /**
     * !#en Returns Y axis location value.
     * !#zh 获取当前触点 Y 轴位置。
     * @method getLocationY
   * @returns {Number}
   */
  getLocationY: function () {
    return this._point.y;
  },

    /**
     * !#en Returns the previous touch location in OpenGL coordinates.
     * !#zh 获取触点在上一次事件时的位置对象,对象包含 x 和 y 属性。
     * @method getPreviousLocation
     * @return {Vec2}
     */
    getPreviousLocation:function () {
        return cc.v2(this._prevPoint.x, this._prevPoint.y);
    },

    /**
     * !#en Returns the start touch location in OpenGL coordinates.
     * !#zh 获取触点落下时的位置对象,对象包含 x 和 y 属性。
     * @method getStartLocation
     * @returns {Vec2}
     */
    getStartLocation: function() {
        return cc.v2(this._startPoint.x, this._startPoint.y);
    },

    /**
     * !#en Returns the delta distance from the previous touche to the current one in screen coordinates.
     * !#zh 获取触点距离上一次事件移动的距离对象,对象包含 x 和 y 属性。
     * @method getDelta
     * @return {Vec2}
     */
    getDelta:function () {
        return this._point.sub(this._prevPoint);
    },

    /**
     * !#en Returns the current touch location in screen coordinates.
     * !#zh 获取当前事件在游戏窗口内的坐标位置对象,对象包含 x 和 y 属性。
     * @method getLocationInView
     * @return {Vec2}
     */
    getLocationInView: function() {
        return cc.v2(this._point.x, cc.view._designResolutionSize.height - this._point.y);
    },

    /**
     * !#en Returns the previous touch location in screen coordinates.
     * !#zh 获取触点在上一次事件时在游戏窗口中的位置对象,对象包含 x 和 y 属性。
     * @method getPreviousLocationInView
     * @return {Vec2}
     */
    getPreviousLocationInView: function(){
        return cc.v2(this._prevPoint.x, cc.view._designResolutionSize.height - this._prevPoint.y);
    },

    /**
     * !#en Returns the start touch location in screen coordinates.
     * !#zh 获取触点落下时在游戏窗口中的位置对象,对象包含 x 和 y 属性。
     * @method getStartLocationInView
     * @return {Vec2}
     */
    getStartLocationInView: function(){
        return cc.v2(this._startPoint.x, cc.view._designResolutionSize.height - this._startPoint.y);
    },

    /**
     * !#en Returns the id of cc.Touch.
     * !#zh 触点的标识 ID,可以用来在多点触摸中跟踪触点。
     * @method getID
     * @return {Number}
     */
    getID:function () {
        return this._id;
    },

    /**
     * !#en Sets information to touch.
     * !#zh 设置触摸相关的信息。用于监控触摸事件。
     * @method setTouchInfo
     * @param {Number} id
     * @param  {Number} x
     * @param  {Number} y
     */
    setTouchInfo:function (id, x, y) {
        this._prevPoint = this._point;
        this._point = cc.v2(x || 0, y || 0);
        this._id = id;
        /* 开启坐标记录,开关开启后,记录当前坐标位置 */
        if(!this._startPointCaptured){
            this._startPoint = cc.v2(this._point);
            cc.view._convertPointWithScale(this._startPoint);
            this._startPointCaptured = true;
        }
    },
/* 设置坐标的x和y值,假如y参数没有传,x就是一个point属性 */
    _setPoint: function(x, y){
        if(y === undefined){
            this._point.x = x.x;
            this._point.y = x.y;
        }else{
            this._point.x = x;
            this._point.y = y;
        }
    },
/* 设置上一个坐标的x和y值,假如y参数没有传,x就是一个point属性 */
    _setPrevPoint:function (x, y) {
        if(y === undefined)
            this._prevPoint = cc.v2(x.x, x.y);
        else
            this._prevPoint = cc.v2(x || 0, y || 0);
    }
};

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值