轉自:https://blog.csdn.net/foupwang/article/details/80474072node
Cocos Creator的 cc.Node 有一套完整的事件監聽和分發機制。在這套機制之上,引擎提供了一些基礎的節點相關的系統事件。函數
Cocos Creator支持的系統事件包含鼠標、觸摸、鍵盤、重力傳感等四種,其中鼠標和觸摸事件是被直接觸發在相關節點上的,因此稱爲節點系統事件。與之對應的,鍵盤和重力傳感事件被稱爲全局系統事件。this
本文主要講述如何處理節點系統中的鼠標事件。過程很是簡單,只要註冊該事件類型,並編寫相應的處理函數便可,以下:spa
this.node.on(cc.Node.EventType.MOUSE_DOWN, this.onMouseDown, this);
this.node.on(cc.Node.EventType.MOUSE_UP, this.onMouseUp, this
);
this.node.on(cc.Node.EventType.MOUSE_WHEEL, this.onMouseWheel, this);
},
this.node.off(cc.Node.EventType.MOUSE_DOWN, this.onMouseDown, this);
this.node.off(cc.Node.EventType.MOUSE_UP, this.onMouseUp, this);
this.node.off(cc.Node.EventType.MOUSE_WHEEL, this.onMouseWheel, this);
},
onMouseDown: function(event) { let mouseType = event.getButton(); if (mouseType === cc.Event.EventMouse.BUTTON_LEFT) { // 鼠標左鍵按下 let mousePoint = event.getLocation(); let localPoint = this.node.convertToNodeSpace(mousePoint); ... } else if (mouseType === cc.Event.EventMouse.BUTTON_MIDDLE) { // 鼠標中鍵按下 ... } else if (mouseType === cc.Event.EventMouse.BUTTON_RIGHT) { // 鼠標右鍵按下 ... } },
onMouseUp: function(event) { let mouseType = event.getButton(); if (mouseType === cc.Event.EventMouse.BUTTON_LEFT) { // 鼠標左鍵釋放 let mousePoint = event.getLocation(); let localPoint = this.node.convertToNodeSpace(mousePoint); ... } else if (mouseType === cc.Event.EventMouse.BUTTON_MIDDLE) { // 鼠標中鍵釋放 ... } else if (mouseType === cc.Event.EventMouse.BUTTON_RIGHT) { // 鼠標右鍵釋放 ... } },
onMouseWheel: function(event) { let scrollY = event.getScrollY(); ... },