0
移除事件監聽我不知道爲什麼它正確地添加事件偵聽器,當我渲染視圖,但如果我嘗試添加另一事件的一個內部功能是不確定的,會引發錯誤。我如何才能正確添加事件?添加和從事件
解決方法是添加存儲鼠標是否按下一個變量,但我覺得只是添加和刪除事件偵聽器看起來清零。它也否定了另一個混亂變量的需要。
代碼:
render: function() {
//sets up a new Easeljs stage called context
Canvas.context.addEventListener("stagemousedown", this.handleMouseDown);
Canvas.context.addEventListener("stagemouseup", this.handleMouseUp);
},
handleMouseDown: function() {
console.log('down');
Canvas.context.addEventListener("stagemousemove", this.handleMouseMove);
},
handleMouseMove: function() {
console.log('moving');
},
handleMouseUp: function() {
console.log('up');
Canvas.context.removeEventListener("stagemousemove", this.handleMouseMove);
}
錯誤:
Uncaught TypeError: Cannot read property 'handleEvent' of undefined (09:32:29:386 | error, javascript)
at b.dispatchEvent (public_html/js/libs/easeljs-0.6.1.min.js:14:447)
at b._handlePointerMove (public_html/js/libs/easeljs-0.6.1.min.js:88:490)
at b._handleMouseMove (public_html/js/libs/easeljs-0.6.1.min.js:88:69)
at c.mousemove.f (public_html/js/libs/easeljs-0.6.1.min.js:87:34)
謝謝你,似乎解決了我的問題。我沒有將事件綁定到初始化函數,以保持渲染功能的清潔和快速。 – Josh