AS3 - 关于符号绑定的问题

发布于 2024-11-02 17:16:44 字数 471 浏览 0 评论 0原文

我是 AS3 的新手,无法弄清楚为什么这个循环没有按照它“应该”的方式运行。

for each (var s in [_set, _set.otherSet]) {
  for each (var f in [s.frame_top_mc, s.frame_bottom_mc]) {
    f.addEventListener(MouseEvent.CLICK, function( ):void {
      _score[f.category] += 1;
      madeSelection(f);
    });
  }
}

如何为每个匿名函数提供对 f 表示的每个对象的引用,而不是每次对 f 的简单引用?

具体来说,为什么匿名函数的每个副本都绑定到对 f 的单个引用?在这方面,AS3 与 JavaScript 到底有何不同(我应该说为什么)?

I'm new to AS3, and can't figure out why this loop isn't behaving the way it "should."

for each (var s in [_set, _set.otherSet]) {
  for each (var f in [s.frame_top_mc, s.frame_bottom_mc]) {
    f.addEventListener(MouseEvent.CLICK, function( ):void {
      _score[f.category] += 1;
      madeSelection(f);
    });
  }
}

How can I give each anonymous function a reference to each object represented by f, rather than a simple reference to f each time?

Specifically, why is it that each copy of the anonymous function gets bound to a single reference to f? How (I should say why) exactly does AS3 differ from JavaScript in this regard?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

混浊又暗下来 2024-11-09 17:16:44

它需要像这样:

for each (var s in [_set, _set.otherSet]) {
  for each (var f in [s.frame_top_mc, s.frame_bottom_mc]) {
    f.addEventListener(MouseEvent.CLICK, function( e:MouseEvent ):void {
      _score[e.currentTarget.category] += 1;
      madeSelection(e.currentTarget);
    });
  }
}

问题是您的闭包正在关闭循环变量 f 本身,而不是循环中使用 f 引用的每个事物。循环完成后,f 将作为循环列表中最后一项的引用。 f 在创建闭包时不会取消引用,而是在执行时取消引用。

It needs to be like this:

for each (var s in [_set, _set.otherSet]) {
  for each (var f in [s.frame_top_mc, s.frame_bottom_mc]) {
    f.addEventListener(MouseEvent.CLICK, function( e:MouseEvent ):void {
      _score[e.currentTarget.category] += 1;
      madeSelection(e.currentTarget);
    });
  }
}

The problem is that your closure is closing over the loop variable f itself rather than each thing f is being used to reference within the loop. After the loop completes, f is left as reference to the last thing in the list you looped over. f is not being de-referenced when the closure is created, but when it's executed.

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