var Common = require('JoystickCommon'); cc.Class({ extends: cc.Component, properties: { dot: { default: null, type: cc.Node, displayName: 'point', }, _joyCom: { default: null, displayName: 'joy Node', }, _angle: { default: null, displayName: 'toujaodu', }, _radian: { default: null, displayName: 'hudu', }, _speed: 0, // _speed1: 1, // _speed2: 2, // _opacity: 0, // }, onLoad: function() { this._joyCom = this.node.parent.getComponent('Joystick'); if(this._joyCom.touchType == Common.TouchType.DEFAULT){ this._initTouchEvent(); } }, onDestroy(){ var self = this; self.node.off(cc.Node.EventType.TOUCH_START, this._touchStartEvent, self); self.node.off(cc.Node.EventType.TOUCH_MOVE, this._touchMoveEvent, self); self.node.off(cc.Node.EventType.TOUCH_END, this._touchEndEvent, self); self.node.off(cc.Node.EventType.TOUCH_CANCEL, this._touchEndEvent, self); }, _initTouchEvent: function() { var self = this; self.node.on(cc.Node.EventType.TOUCH_START, this._touchStartEvent, self); self.node.on(cc.Node.EventType.TOUCH_MOVE, this._touchMoveEvent, self); self.node.on(cc.Node.EventType.TOUCH_END, this._touchEndEvent, self); self.node.on(cc.Node.EventType.TOUCH_CANCEL, this._touchEndEvent, self); }, update: function(dt) { // switch (this._joyCom.directionType) // { // case Common.DirectionType.ALL: // this._allDirectionsMove(); // break; // default : // break; // } }, _allDirectionsMove: function() { // this._playerNode.x += Math.cos(this._angle * (Math.PI/180)) * this._speed; // this._playerNode.y += Math.sin(this._angle * (Math.PI/180)) * this._speed; }, _getDistance: function(pos1, pos2) { return Math.sqrt(Math.pow(pos1.x - pos2.x, 2) + Math.pow(pos1.y - pos2.y, 2)); }, _getRadian: function(point) { this._radian = Math.PI / 180 * this._getAngle(point); return this._radian; }, _getAngle: function(point) { var pos = this.node.getPosition(); this._angle = Math.atan2(point.y - pos.y, point.x - pos.x) * (180/Math.PI); return this._angle; }, _setSpeed: function(point) { var distance = this._getDistance(point, this.node.getPosition()); if(distance < this._radius) { this._speed = this._speed1; } else { this._speed = this._speed2; } }, _touchStartEvent: function(event) { var touchPos = this.node.convertToNodeSpaceAR(event.getLocation()); var distance = this._getDistance(touchPos,cc.v2(0,0)); var radius = this.node.width / 2; this._stickPos = touchPos; var posX = this.node.getPosition().x + touchPos.x; var posY = this.node.getPosition().y + touchPos.y; this._joyCom.startCb&&this._joyCom.startCb(event.getLocation()); if(radius > distance) { this.dot.setPosition(cc.v2(posX, posY)); return true; } return false; }, _touchMoveEvent: function(event){ var touchPos = this.node.convertToNodeSpaceAR(event.getLocation()); var distance = this._getDistance(touchPos,cc.v2(0,0)); var radius = this.node.width / 2; var posX = this.node.getPosition().x + touchPos.x; var posY = this.node.getPosition().y + touchPos.y; if(radius > distance) { this.dot.setPosition(cc.v2(posX, posY)); } else { var x = this.node.getPosition().x + Math.cos(this._getRadian(cc.v2(posX,posY))) * radius; var y = this.node.getPosition().y + Math.sin(this._getRadian(cc.v2(posX,posY))) * radius; this.dot.setPosition(cc.v2(x, y)); } this._getAngle(cc.v2(posX,posY)); this._joyCom.moveCb&&this._joyCom.moveCb(this._angle,distance,event.getLocation()); }, _touchEndEvent: function(){ this.dot.setPosition(this.node.getPosition()); this._speed = 0; this._joyCom.endCb&&this._joyCom.endCb(); }, });