|
|
@@ -8,11 +8,11 @@ const { ccclass, property } = cc._decorator;
|
|
|
@ccclass
|
|
|
export class MapMove extends cc.Component {
|
|
|
@property({ type: cc.Node, tooltip: "地图容器" }) map: cc.Node = null;
|
|
|
-
|
|
|
+
|
|
|
private canvas: cc.Node = null;
|
|
|
private startPoint: cc.Vec2;
|
|
|
start() {
|
|
|
- this.canvas = cc.find('canvas');
|
|
|
+ this.canvas = cc.find('Canvas');
|
|
|
this.node.on(cc.Node.EventType.TOUCH_START, this.onMapTouchBegin, this);
|
|
|
}
|
|
|
|
|
|
@@ -28,13 +28,13 @@ export class MapMove extends cc.Component {
|
|
|
this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.onTouchEnd, this);
|
|
|
}
|
|
|
|
|
|
- private onTouchMove(e): void {
|
|
|
- let touches = e.getAllTouches();
|
|
|
+ private onTouchMove(e: cc.Event.EventTouch): void {
|
|
|
+ let touches = e.getTouches();
|
|
|
if (touches.length == 1) {
|
|
|
if (this.isDoubleTouch) {
|
|
|
return;
|
|
|
}
|
|
|
- let point = e.touch._point;
|
|
|
+ let point = e.touch.getLocation();
|
|
|
let offsetX = point.x - this.startPoint.x;
|
|
|
let offsetY = point.y - this.startPoint.y;
|
|
|
this.move(new cc.Vec2(offsetX, offsetY));
|
|
|
@@ -54,12 +54,12 @@ export class MapMove extends cc.Component {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private onTouchEnd(e): void {
|
|
|
+ private onTouchEnd(e: cc.Event.EventTouch): void {
|
|
|
this.startPoint = cc.Vec2.ZERO;
|
|
|
this.node.off(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
|
|
|
this.node.off(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
|
|
|
this.node.off(cc.Node.EventType.TOUCH_CANCEL, this.onTouchEnd, this);
|
|
|
- if (e.getAllTouches().length == 0) {
|
|
|
+ if (e.getTouches().length == 0) {
|
|
|
this.isDoubleTouch = false;
|
|
|
}
|
|
|
}
|
|
|
@@ -80,8 +80,8 @@ export class MapMove extends cc.Component {
|
|
|
}
|
|
|
/**确认移动到的坐标是否合法 */
|
|
|
private confirmPosition(pos: cc.Vec2): cc.Vec3 {
|
|
|
- let clampX = Util.clamp(pos.x, -(this.canvas.width * (this.map.scaleX - 0.2) / 2 - this.canvas.width / 2), this.canvas.width * (this.map.scaleX - 0.2) / 2 - this.canvas.width / 2);
|
|
|
- let clampY = Util.clamp(pos.y, -(this.canvas.height * (this.map.scaleY - 0.2) / 2 - this.canvas.height / 2), this.canvas.height * (this.map.scaleY - 0.2) / 2 - this.canvas.height / 2);
|
|
|
+ let clampX = Util.clamp(pos.x, -(this.map.width * (this.map.scaleX - 0.2) / 2 - this.canvas.width / 2), this.map.width * (this.map.scaleX - 0.2) / 2 - this.canvas.width / 2);
|
|
|
+ let clampY = Util.clamp(pos.y, -(this.map.height * (this.map.scaleY - 0.2) / 2 - this.canvas.height / 2), this.map.height * (this.map.scaleY - 0.2) / 2 - this.canvas.height / 2);
|
|
|
return new cc.Vec3(clampX, clampY, 0);
|
|
|
}
|
|
|
|
|
|
@@ -90,6 +90,6 @@ export class MapMove extends cc.Component {
|
|
|
let toPoint = new cc.Vec2(-pos.x * this.map.scaleX, -pos.y * this.map.scaleY);
|
|
|
cc.tween(this.node).to(0.5, { position: this.confirmPosition(toPoint) }, { easing: "sineOut" }).start();
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
}
|
|
|
|