在 Animate CC HTML5 Canvas 中的帧之间跳转时如何删除 addEventListener?
当我在帧之间跳转时,我似乎无法删除 addEventListeners 。当我来回跳转时,addEventListeners似乎仍然堆积起来:
第 1 帧代码:
this.stop();
this.arrowForward_btn.addEventListener("click", arrow_btnClickForward.bind(this));
function arrow_btnClickForward() {
this.arrowForward_btn.removeEventListener("click", arrow_btnClickForward.bind(this));
console.log("went forward");
alert("alert: went forward!");
this.gotoAndStop(1);
}
第 2 帧代码:
this.arrowBack_btn.addEventListener("click", arrow_btnClickBack.bind(this));
function arrow_btnClickBack() {
this.arrowBack_btn.removeEventListener("click", arrow_btnClickBack.bind(this));
console.log("went back!");
alert("alert: went back!");
this.gotoAndStop(0);
}
如何删除 addEventListeners 以便它们不会堆叠起来?
更新:被告知有关 evt.remove();所以我在我的代码中使用了它并且它有效:
第 1 帧代码:
this.stop();
this.arrowForward_btn.addEventListener("click", arrow_btnClickForward.bind(this));
function arrow_btnClickForward() {
console.log("went forward");
alert("alert: went forward!");
this.gotoAndStop(1);
evt.remove();
}
第 2 帧代码:
this.arrowBack_btn.addEventListener("click", arrow_btnClickBack.bind(this));
function arrow_btnClickBack(evt) {
console.log("went back!");
alert("alert: went back!");
this.gotoAndStop(0);
evt.remove();
}
I seem to have trouble removing addEventListeners when I jump between frames. When I jump back and forth, the addEventListeners still seem to stack up:
Frame 1 code:
this.stop();
this.arrowForward_btn.addEventListener("click", arrow_btnClickForward.bind(this));
function arrow_btnClickForward() {
this.arrowForward_btn.removeEventListener("click", arrow_btnClickForward.bind(this));
console.log("went forward");
alert("alert: went forward!");
this.gotoAndStop(1);
}
Frame 2 code:
this.arrowBack_btn.addEventListener("click", arrow_btnClickBack.bind(this));
function arrow_btnClickBack() {
this.arrowBack_btn.removeEventListener("click", arrow_btnClickBack.bind(this));
console.log("went back!");
alert("alert: went back!");
this.gotoAndStop(0);
}
How do I remove the addEventListeners so that they don't stack up?
Update: Was told about evt.remove(); so I used that in my codes and it works:
Frame 1 code:
this.stop();
this.arrowForward_btn.addEventListener("click", arrow_btnClickForward.bind(this));
function arrow_btnClickForward() {
console.log("went forward");
alert("alert: went forward!");
this.gotoAndStop(1);
evt.remove();
}
Frame 2 code:
this.arrowBack_btn.addEventListener("click", arrow_btnClickBack.bind(this));
function arrow_btnClickBack(evt) {
console.log("went back!");
alert("alert: went back!");
this.gotoAndStop(0);
evt.remove();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的更新更好,但它可能有助于解释一些事情:
bind()
时,它会生成一个新的唯一闭包。每次。请注意,
evt.remove()
应该适合您,但在第一个示例(第 1 帧代码)中,事件处理程序没有evt
参数。CreateJS 中的一些内容可能会有所帮助:
on()
方法是 addEventListener 的快捷方式,它可以帮助您执行诸如传递作用域之类的操作。once
参数,因此如果您知道您的事件将触发一次,它可以被自动删除。on()
返回生成的闭包。您可以使用off()
轻松删除。希望这能让您更好地理解 CreateJS 中的事件处理程序,并且它们的某种组合将会起作用。
--
了解您可以在第一帧中编写所有代码也可能会有所帮助,因为 CreateJS 在动画剪辑初始化时会在每一帧中引导实例。因此
arrowBack_btn
也将存在于第 1 帧中。您只需添加代码一次,并确保它只运行我没有对此进行测试,但它应该可以工作:D
Your update is better, but it might help to explain a few things:
bind()
, it generates a new unique closure. Every time.Note that
evt.remove()
should work for you, but in your first example (frame 1 code), the event handler doesn't have anevt
param.A few things in CreateJS might help:
on()
method is a shortcut for addEventListener, which helps you do things like pass scopeonce
parameter, so if you know your event will fire one time, it can get auto-removed.on()
returns the generated closure. You can useoff()
to remove easily.Hopefully that gives you a better understanding of event handlers in CreateJS, and some combination of that will work.
--
It might also be helpful to know that you can write all your code in the first frame, since CreateJS bootstraps instances in every frame when the movieclip is initialized. So
arrowBack_btn
will exist in frame 1 as well. You could just add the code once, and ensure it only runI didn't test this, but it should work :D