123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- import { EventMgr } from "../Script/Core/Base/EventMgr";
- import { Roker_End, Roker_Move } 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.x != 0 || offset.y != 0) {
- 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);
- }
- }
|