在 Animate CC HTML5 Canvas 中的帧之间跳转时如何删除 addEventListener?

发布于 2025-01-15 01:25:45 字数 1566 浏览 0 评论 0原文

当我在帧之间跳转时,我似乎无法删除 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:

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝眼睛不忧郁 2025-01-22 01:25:45

您的更新更好,但它可能有助于解释一些事情:

  • 当您使用bind()时,它会生成一个新的唯一闭包。每次。
  • 当您删除事件时,您需要使用这个精确闭包。即使使用相同的参数,生成另一个也是行不通的。

请注意,evt.remove() 应该适合您,但在第一个示例(第 1 帧代码)中,事件处理程序没有 evt 参数。

CreateJS 中的一些内容可能会有所帮助:

  1. on() 方法是 addEventListener 的快捷方式,它可以帮助您执行诸如传递作用域之类的操作。
this.arrowForward_btn.on("click", arrow_btnClickForward, this);
  1. 它还有一个 once 参数,因此如果您知道您的事件将触发一次,它可以被自动删除。
this.arrowForward_btn.on("click", arrow_btnClickForward, this, true);
  1. 如果您仍然想手动管理,on() 返回生成的闭包。您可以使用 off() 轻松删除。
var handler = this.arrowForward_btn.on("click", arrow_btnClickForward, this);
// later
this.arrowForward.off("click", handler);

希望这能让您更好地理解 CreateJS 中的事件处理程序,并且它们的某种组合将会起作用。

--

了解您可以在第一帧中编写所有代码也可能会有所帮助,因为 CreateJS 在动画剪辑初始化时会在每一帧中引导实例。因此 arrowBack_btn 也将存在于第 1 帧中。您只需添加代码一次,并确保它只运行

if (this.inited == false) {
  this.inited = true;

  this.arrowForward_btn.on("click", arrow_btnClickForward, this);
    this.arrowBack_btn.on("click", arrow_btnClickBack, this);

    function arrow_btnClickForward() {
        console.log("went forward");
        alert("alert: went forward!");
        this.gotoAndStop(1);
    }

    function arrow_btnClickBack(evt) {
        console.log("went back!");
        alert("alert: went back!");
        this.gotoAndStop(0);
    }

}

我没有对此进行测试,但它应该可以工作:D

Your update is better, but it might help to explain a few things:

  • When you use bind(), it generates a new unique closure. Every time.
  • You need to use this exact closure when you remove the event. Generating another one, even with the same parameters, will not work.

Note that evt.remove() should work for you, but in your first example (frame 1 code), the event handler doesn't have an evt param.

A few things in CreateJS might help:

  1. The on() method is a shortcut for addEventListener, which helps you do things like pass scope
this.arrowForward_btn.on("click", arrow_btnClickForward, this);
  1. It also has a once parameter, so if you know your event will fire one time, it can get auto-removed.
this.arrowForward_btn.on("click", arrow_btnClickForward, this, true);
  1. If you still want to manually manage, on() returns the generated closure. You can use off() to remove easily.
var handler = this.arrowForward_btn.on("click", arrow_btnClickForward, this);
// later
this.arrowForward.off("click", handler);

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 run

if (this.inited == false) {
  this.inited = true;

  this.arrowForward_btn.on("click", arrow_btnClickForward, this);
    this.arrowBack_btn.on("click", arrow_btnClickBack, this);

    function arrow_btnClickForward() {
        console.log("went forward");
        alert("alert: went forward!");
        this.gotoAndStop(1);
    }

    function arrow_btnClickBack(evt) {
        console.log("went back!");
        alert("alert: went back!");
        this.gotoAndStop(0);
    }

}

I didn't test this, but it should work :D

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文