// 注册地图touch事件监听
// TOUCH_START
this._touchLayer.on(cc.Node.EventType.TOUCH_START,
(event) => {
lc.GameLogger.debug('TOUCH_START');
// 因为有滑动助力,所以在 TOUCH_START 时结束_mapLayer的action。
this.gamerCamera.node.stopAllActions();
// 设置是否是第一次Touch,在地图缩放时,判断是否设置 _firstTouchDistance
this._isFirstTouch = true;
// 获取touch数量
let touches = event.getTouches();
// 判断出发类型 (单点/两点)
if (touches.length == 1) {
// 保存Touch的起始坐标,在地图滑动结束时,用来设置滑动助力的大小。
this._touchStartPos = touches[0].getLocation();
}
},
this
);
// TOUCH_MOVE
this._touchLayer.on(cc.Node.EventType.TOUCH_MOVE,
(event) => {
lc.GameLogger.debug('TOUCH_MOVE');
// 获取touch数量
let touches = event.getTouches();
// 判断出发类型 (单点/两点)
if (touches.length == 1) {
// 单点滑动 -- 地图滚动
let currentPos = touches[0].getLocation();
let previousPos = touches[0].getPreviousLocation();
// 如果连续两次的滑动距离过小的话,视为滑动停止,之后会取消滑动助力。
if (lc.GameUtil.getDistanceBetweenTwoPoints(previousPos, currentPos) < 5) {
this._isTouchStay = true;
} else {
this._isTouchStay = false;
}
// 落点位置坐标
let dropPointPos = {
x: this.gamerCamera.node.getPosition().x - (currentPos.x - previousPos.x),
y: this.gamerCamera.node.getPosition().y - (currentPos.y - previousPos.y),
};
// 检查滚动边界
dropPointPos = this._rectifyRollingBoundary(dropPointPos)
this.gamerCamera.node.setPosition(dropPointPos);
} else if (touches.length == 2) {
// 两点滑动 -- 地图缩放
let touchPos0 = touches[0].getLocation();
let touchPos1 = touches[1].getLocation();
// 计算当前两点间的距离
let distance = lc.GameUtil.getDistanceBetweenTwoPoints(touchPos0, touchPos1);
lc.GameLogger.debug('distance = ' + distance);
// 如果是第一次滑动的话保存当前的两点间距离,之后用来计算缩放率。
if (this._isFirstTouch) {
this._isFirstTouch = false;
this._firstTouchDistance = distance;
}
// 计算当前缩放比率, 分子分母加入一个站位数值,冲淡缩放比例,使得操作可以更自然。
this._cameraCurZoomRatio *= (5000 + distance) / (5000 + this._firstTouchDistance);
// 缩放的边界值
if (this._cameraCurZoomRatio > 4.0) {
this._cameraCurZoomRatio = 4.0;
}
if (this._cameraCurZoomRatio < 0.8) {
this._cameraCurZoomRatio = 0.8
}
// 执行缩放
this.gamerCamera.zoomRatio = this._cameraCurZoomRatio;
} else {
// noting
}
},
this
);
// TOUCH_END
this._touchLayer.on(cc.Node.EventType.TOUCH_END,
(event) => {
lc.GameLogger.debug('TOUCH_END');
// 获取touch数量
let touches = event.getTouches();
// 判断出发类型 (单点/两点)
if (touches.length == 1) {
let touchEndPos = touches[0].getLocation();
// 转换为世界坐标
let touchC2WPos = this.gamerCamera.getCameraToWorldPoint(touchEndPos);
// 获取点击的cell
this._touchSite = this._getSiteOnChessboard(touchC2WPos);
// ------------ 滚动助力 ------------
// 计算滑动距离
let tPos = cc.v2(
(this._touchStartPos.x - touchEndPos.x) / 10,
(this._touchStartPos.y - touchEndPos.y) / 10
);
// 在非滑动静止的场合执行滚动助力
if (!this._isTouchStay) {
this.gamerCamera.node.runAction(cc.moveBy(0.6, tPos));
}
// ------------ 显示选中光标 ------------
this._drawCellSelectedSign(this._touchSite);
// ------------ 业务响应 ------------
// 根据玩家和角色状态处理点击事件,以玩家、当前角色、点击位置作为判断条件。
this._doTouchWork(this._touchSite);
}
},
this
);
this._touchLayer.on(cc.Node.EventType.TOUCH_CANCEL,
(event) => {
lc.GameLogger.debug('TOUCH_CANCEL');
},
this
);
this.node.emit('initTouchLayerFinishEvent', {});
网友评论