214 lines
4.2 KiB
JavaScript
214 lines
4.2 KiB
JavaScript
var Common = require('JoystickCommon');
|
|
var JoystickBG = require('JoystickBG');
|
|
|
|
cc.Class({
|
|
extends: cc.Component,
|
|
|
|
properties: {
|
|
dot: {
|
|
default: null,
|
|
type: cc.Node,
|
|
displayName: 'joycon',
|
|
},
|
|
ring: {
|
|
default: null,
|
|
type: JoystickBG,
|
|
displayName: 'bg',
|
|
},
|
|
stickX: {
|
|
default: 0,
|
|
displayName: 'xpos',
|
|
},
|
|
|
|
stickY: {
|
|
default: 0,
|
|
displayName: 'ypos',
|
|
},
|
|
touchType: {
|
|
default: Common.TouchType.DEFAULT,
|
|
type: Common.TouchType,
|
|
displayName: 'type',
|
|
},
|
|
directionType: {
|
|
default: Common.DirectionType.ALL,
|
|
type: Common.DirectionType,
|
|
displayName: 'dirtype',
|
|
},
|
|
|
|
|
|
_stickPos: {
|
|
default: null,
|
|
type: cc.Node,
|
|
displayName: 'curpos',
|
|
},
|
|
|
|
_touchLocation: {
|
|
default: null,
|
|
type: cc.Node,
|
|
displayName: 'curpos',
|
|
},
|
|
},
|
|
|
|
onLoad: function () {
|
|
this.cd = 0;
|
|
this._createStickSprite();
|
|
//
|
|
if (this.touchType == Common.TouchType.FOLLOW) {
|
|
this._initTouchEvent();
|
|
}
|
|
},
|
|
bindnewbie(v) {
|
|
this.isnewbie = v;
|
|
cc.Notifier.on('newbiestart', this, this.newbiestart.bind(this));
|
|
cc.Notifier.on('newbieend', this, this.newbieend.bind(this));
|
|
},
|
|
_createStickSprite: function () {
|
|
//
|
|
this.ring.node.setPosition(this.stickX, this.stickY);
|
|
this.dot.setPosition(this.stickX, this.stickY);
|
|
},
|
|
|
|
newbiestart: function () {
|
|
this._touchEndEvent();
|
|
this.needend = true;
|
|
},
|
|
newbieend: function () {
|
|
this.needend = false;
|
|
},
|
|
|
|
onDestroy() {
|
|
var self = this;
|
|
|
|
self.node.off(
|
|
cc.Node.EventType.TOUCH_START,
|
|
self._touchStartEvent,
|
|
self
|
|
);
|
|
|
|
self.node.off(cc.Node.EventType.TOUCH_MOVE, self._touchMoveEvent, self);
|
|
|
|
//
|
|
self.node.off(cc.Node.EventType.TOUCH_END, self._touchEndEvent, self);
|
|
self.node.off(
|
|
cc.Node.EventType.TOUCH_CANCEL,
|
|
self._touchEndEvent,
|
|
self
|
|
);
|
|
|
|
cc.Notifier.off('newbiestart', this);
|
|
cc.Notifier.off('newbieend', this);
|
|
},
|
|
_initTouchEvent: function () {
|
|
var self = this;
|
|
|
|
self.node.on(
|
|
cc.Node.EventType.TOUCH_START,
|
|
self._touchStartEvent,
|
|
self
|
|
);
|
|
|
|
self.node.on(cc.Node.EventType.TOUCH_MOVE, self._touchMoveEvent, self);
|
|
|
|
//
|
|
self.node.on(cc.Node.EventType.TOUCH_END, self._touchEndEvent, self);
|
|
self.node.on(cc.Node.EventType.TOUCH_CANCEL, self._touchEndEvent, self);
|
|
},
|
|
|
|
_touchStartEvent: function (event) {
|
|
if (this.cd > 0) {
|
|
return;
|
|
}
|
|
// move
|
|
this._touchLocation = event.getLocation();
|
|
var touchPos = this.node.convertToNodeSpaceAR(event.getLocation());
|
|
//
|
|
if (this.mode == true) {
|
|
this.ring.node.setPosition(touchPos);
|
|
this.dot.setPosition(touchPos);
|
|
// move
|
|
this._stickPos = touchPos;
|
|
} else {
|
|
this._createStickSprite();
|
|
this._stickPos = cc.v2(this.stickX, this.stickY);
|
|
}
|
|
this.startCb && this.startCb(event.getLocation());
|
|
},
|
|
|
|
_touchMoveEvent: function (event) {
|
|
if (this.cd > 0) {
|
|
return false;
|
|
}
|
|
if (this.needend) {
|
|
return false;
|
|
}
|
|
//
|
|
if (
|
|
this._touchLocation.x == event.getLocation().x &&
|
|
this._touchLocation.y == event.getLocation().y
|
|
) {
|
|
return false;
|
|
}
|
|
|
|
var touchPos = this.ring.node.convertToNodeSpaceAR(event.getLocation());
|
|
var distance = this.ring._getDistance(touchPos, cc.v2(0, 0));
|
|
var radius = this.ring.node.width / 2;
|
|
|
|
|
|
var posX = this._stickPos.x + touchPos.x;
|
|
var posY = this._stickPos.y + touchPos.y;
|
|
|
|
if (radius > distance) {
|
|
this.dot.setPosition(cc.v2(posX, posY));
|
|
} else {
|
|
|
|
var x =
|
|
this._stickPos.x +
|
|
Math.cos(this.ring._getRadian(cc.v2(posX, posY))) * radius;
|
|
var y =
|
|
this._stickPos.y +
|
|
Math.sin(this.ring._getRadian(cc.v2(posX, posY))) * radius;
|
|
this.dot.setPosition(cc.v2(x, y));
|
|
}
|
|
|
|
var ag = this.ring._getAngle(cc.v2(posX, posY));
|
|
|
|
this.moveCb && this.moveCb(ag, distance, event.getLocation());
|
|
|
|
|
|
},
|
|
|
|
_touchEndEvent: function () {
|
|
if (this.needend) {
|
|
return;
|
|
}
|
|
if (this.needcd) {
|
|
this.cd = 0.5;
|
|
}
|
|
|
|
this.dot.setPosition(this.ring.node.getPosition());
|
|
this.ring._speed = 0;
|
|
this.endCb && this.endCb();
|
|
this._createStickSprite();
|
|
},
|
|
|
|
bindMoveCb: function (cb) {
|
|
this.moveCb = cb;
|
|
},
|
|
bindStartCb: function (cb) {
|
|
this.startCb = cb;
|
|
},
|
|
bindEndCb: function (cb) {
|
|
this.endCb = cb;
|
|
},
|
|
update: function (dt) {
|
|
if (this.cd > 0) {
|
|
this.cd -= dt;
|
|
}
|
|
},
|
|
onDisable() {
|
|
if (!this.isnewbie) {
|
|
this._touchEndEvent();
|
|
}
|
|
},
|
|
});
|