import { EventMgr } from "../../Script/Core/Base/EventMgr"; import { Roker_Move, Roker_End } from "./eventDef"; const {ccclass, property} = cc._decorator; interface iRockerTemp { bgOriginPos: cc.Vec2; id: number; } @ccclass export default class Rocker extends cc.Component { /*===========================自动绑定组件开始===========================*/ /*自动生成*/ @property({type:cc.Node, displayName:""}) private $bg_node:cc.Node = null; /*自动生成*/ @property({type:cc.Node, displayName:""}) private $point_node:cc.Node = null; /*自动生成*/ @property({type:cc.Node, displayName:""}) private $arrow_node:cc.Node = null; /*===========================自动绑定组件结束===========================*/ /*===========================自动生成按钮事件开始==========================*/ /*===========================自动生成按钮事件结束==========================*/ temp: iRockerTemp = { bgOriginPos:null, id:-1, } protected onLoad(): void { this.temp.bgOriginPos = this.$bg_node.getPosition(); } protected onEnable(): void { this.node.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this); this.node.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this); this.node.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this); this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.onTouchEnd, this); } protected onDisable(): void { this.node.off(cc.Node.EventType.TOUCH_START, this.onTouchStart, this); 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); } onTouchStart(event: cc.Event.EventTouch) { if(this.temp.id != -1) return; let pos = event.getLocation(); pos = this.node.convertToNodeSpaceAR(pos); this.$bg_node.setPosition(pos); this.temp.id = event.getID(); } onTouchMove(event: cc.Event.EventTouch) { if(this.temp.id != event.getID()) return; let offset = event.getLocation().sub(event.getStartLocation()); let angle = 0; if(!offset.equals(cc.Vec2.ZERO)) { angle = cc.v2(0,1).signAngle(offset) / Math.PI * 180; } this.$arrow_node.angle = Math.round(angle * 100) / 100; let nOffset = offset.normalize(); if(offset.mag() > 85) { this.$point_node.setPosition(nOffset.x * 85, nOffset.y * 85); }else{ this.$point_node.setPosition(offset.x, offset.y); } EventMgr.inst.emit(Roker_Move, nOffset); } onTouchEnd(event: cc.Event.EventTouch) { if(this.temp.id != event.getID()) return; this.$bg_node.setPosition(this.temp.bgOriginPos); this.$arrow_node.angle = 0; this.$point_node.setPosition(0, 0); this.temp.id = -1; EventMgr.inst.emit(Roker_End); } }