因為習慣了Unity Input的使用方式所以自己做了個類似的東西
使用起來感覺還不錯不過還是有些地方沒有完善的
使用方法:掛載到Node上然後設定Node的大小為自己想要處發交互的區域
(有想說做成單例可以直接在呼叫的時候實體化出來但是恕小地不才沒辦法做到,煩請指教)
如果有任何可以改進或有相似的東西可以替代的話歡迎交流
//目前鼠標狀態
var TouchState = cc.Enum({
Start: 0,
Move: 1,
End: 2,
Cancel: 3
});
var Input = cc.Class({
extends: cc.Component,
properties: {
isTouchedDown: false,
touchUpDelay: false,
touchPoint: new cc.Vec2(0, 0), //鼠標位置
touchDelta: new cc.Vec2(0, 0), //上一偵和當前偵之間的鼠標移動量
touchStart: new cc.Vec2(0, 0), //點擊開始的鼠標位置
touchEnd: new cc.Vec2(0, 0), //點擊結束的鼠標位置
touchStartEnd: new cc.Vec2(0, 0),//從點擊開始到結束的移動量
preTouchDelta: new cc.Vec2(0, 0),
touchState: {
default: TouchState.Cancel,
type: TouchState,
visible: true
},
hover: {
default: false,
visible: true
}
},
statics: {
//紀錄當前實例
Get: null
},
// use this for initialization
onLoad: function () {
Input.Get = this;
if (Input.instance === null)
Input.instance = this;
this.node.on(cc.Node.EventType.TOUCH_START, function (event) {
Input.Get.touchPoint = cc.p(event.touch.getLocation().x, event.touch.getLocation().y);
Input.Get.touchStart = cc.p(event.touch.getLocation().x, event.touch.getLocation().y);
Input.Get.touchState = TouchState.Start;
Input.Get.touchStartEnd = cc.p(0, 0);
}, this.node);
this.node.on(cc.Node.EventType.TOUCH_END, function (event) {
Input.Get.touchPoint = cc.p(event.touch.getLocation().x, event.touch.getLocation().y);
Input.Get.touchEnd = cc.p(event.touch.getLocation().x, event.touch.getLocation().y);
Input.Get.touchStartEnd =cc.p(Input.Get.touchEnd.x - Input.Get.touchStart.x,Input.Get.touchEnd.y - Input.Get.touchStart.y);
Input.Get.touchState = TouchState.End;
Input.Get.touchUpDelay = true;
Input.Get.isTouchedDown = false
}, this.node);
this.node.on(cc.Node.EventType.TOUCH_MOVE, function (event) {
if (!this.hover) {
return;
}
Input.Get.touchPoint = cc.p(event.touch.getLocation().x, event.touch.getLocation().y);
Input.Get.touchEnd = cc.p(event.touch.getLocation().x, event.touch.getLocation().y);
Input.Get.touchStartEnd =cc.p(Input.Get.touchEnd.x - Input.Get.touchStart.x,Input.Get.touchEnd.y - Input.Get.touchStart.y);
Input.Get.touchDelta = event.getDelta();
Input.Get.touchState = TouchState.Move;
}, this.node);
this.node.on(cc.Node.EventType.MOUSE_LEAVE, function () {
Input.Get.touchState = TouchState.End;
Input.Get.touchStartEnd =cc.p(Input.Get.touchEnd.x - Input.Get.touchStart.x,Input.Get.touchEnd.y - Input.Get.touchStart.y);
Input.Get.isTouchedDown = false;
Input.Get.touchUpDelay = true;
this.hover = false;
}, this.node);
this.node.on(cc.Node.EventType.MOUSE_ENTER, function () {
this.hover = true;
}, this.node);
},
//按下
onTouchDown: function () {
if (Input.Get.touchState == TouchState.Start && !Input.Get.isTouchedDown) {
Input.Get.isTouchedDown = true;
return true;
}
return false;
},
//放開
onTouchUp: function () {
if (Input.Get.touchState == TouchState.End && Input.Get.touchUpDelay) {
Input.Get.touchUpDelay = false;
return true;
}
return false;
},
//按著
onTouch: function () {
if (Input.Get.touchState == TouchState.Move) {
return true;
}
return false;
},
// called every frame, uncomment this function to activate update callback
update: function (dt) {
if (this.touchDelta == this.preTouchDelta) {
this.touchDelta = cc.p(0, 0);
}
this.preTouchDelta = this.touchDelta;
},
});