我不确定为什么它在渲染视图时正确添加了事件侦听器,但是如果我尝试在另一个事件函数中添加一个,则它是未定义的并抛出错误。如何才能正确添加活动?
解决方法是添加一个存储鼠标是否已关闭的变量,但我觉得只需添加和删除事件监听器就可以清除。它也消除了对另一个混乱变量的需要。
代码:
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)
答案 0 :(得分:1)
您需要在_.bindAll(this)
中执行initialize
,如此:
initialize: function () {_.bindAll(this);}
您可能还应该将事件绑定代码移动到初始化,因为每个实例只调用一次,而render
可能会重复调用,导致重复的事件处理程序绑定。