Actionscript-静态函数和 UI 元素有问题吗?

发布于 2024-10-11 19:02:21 字数 983 浏览 3 评论 0原文

我对 ActionScript 中的 OOP 有点不知所措。我有一个捕获视频流的 Display 类。我正在尝试创建一组基本的停止/录制按钮来控制相机。显然,我无法声明访问 this 的函数或任何允许我识别和停止剪辑的变量。编译器(我使用 Haxe)抛出错误:

video/Webcam.hx:96: characters 10-14 : Cannot access this from a static function

我可能以错误的方式处理这个问题。下面是一些(缩写的)代码:

class Webcam extends Display {

  var nc : flash.net.NetConnection;
  ...

  private function addControls(){
    var stopIcon = new StopIcon();
    var b = new flash.display.MovieClip();      
    b.addChild(stopIcon);
    b.useHandCursor = true;
    b.addEventListener(flash.events.MouseEvent.CLICK,function() { 
      trace(this);
      this.stopStream()
    });
    b.x = 210;
    b.y = 20;
  }

  ...
}

我正在使用 Haxe 编译为 AS3。这里有一个增量列表 http://haxe.org/doc/flash/as2_compare似乎没有涵盖这个问题,所以我相信这是我在 AS 上遇到的问题。这可能与编译器有关,但我希望不是,因为到目前为止我真的很喜欢 Haxe。

如果 ActionScript 编译器将这些函数视为静态函数,如何创建与对象实例关联的 UI 元素?

I'm in a little over my head here with OOP in actionscript. I've got a Display class that captures a video stream. I'm trying to create a set of basic stop / record buttons to control the camera. Apparently I can't declare functions which access this or any variables that would allow me to identify and stop the clip. The compiler (I'm using Haxe) throws the error :

video/Webcam.hx:96: characters 10-14 : Cannot access this from a static function

I might be approaching this the wrong way. Here's some (abbreviated) code:

class Webcam extends Display {

  var nc : flash.net.NetConnection;
  ...

  private function addControls(){
    var stopIcon = new StopIcon();
    var b = new flash.display.MovieClip();      
    b.addChild(stopIcon);
    b.useHandCursor = true;
    b.addEventListener(flash.events.MouseEvent.CLICK,function() { 
      trace(this);
      this.stopStream()
    });
    b.x = 210;
    b.y = 20;
  }

  ...
}

I'm using Haxe to compile to AS3. There's a list of deltas here http://haxe.org/doc/flash/as2_compare that doesn't seem to cover this issue, so I believe this is a problem I have with AS. It's possible that it's compiler related, but I hope not because I've really liked Haxe so far.

How do you create UI elements associated with an object instance if the actionscript compiler treats these functions as static?

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

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

发布评论

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

评论(2

不爱素颜 2024-10-18 19:02:21

相信这是由于在 MouseEvent.CLICK 处理程序中使用了匿名函数而没有使用事件本身。事件处理程序接受一个参数,即 MouseEvent 本身。因此,您必须执行以下操作之一:

b.addEventListener(flash.events.MouseEvent.CLICK, function($evt:MouseEvent) {
    trace($evt.target.parent);
    $evt.target.parent.stopStream();  // May require casting, but probably not
}

b.addEventListener(flash.events.MouseEvent.CLICK, __handleStopClick);

private function __handleStopClick($evt:MouseEvent):void {
    this.stopStream();
}

I believe this is due to the use of an anonymous function in your MouseEvent.CLICK handler without using the Event itself. The event handler takes an argument, which is the MouseEvent itself. So, you have a to do one of the following:

b.addEventListener(flash.events.MouseEvent.CLICK, function($evt:MouseEvent) {
    trace($evt.target.parent);
    $evt.target.parent.stopStream();  // May require casting, but probably not
}

OR

b.addEventListener(flash.events.MouseEvent.CLICK, __handleStopClick);

private function __handleStopClick($evt:MouseEvent):void {
    this.stopStream();
}
清风不识月 2024-10-18 19:02:21

另一种常见的方法如下:

private function addControls(){
  ...
  var self = this;
  b.addEventListener(flash.events.MouseEvent.CLICK,function() { 
    self.stopStream()
  });
  ...
}

优点是“self”输入正确并且不需要强制转换。我们正在考虑在这种情况下添加“this”作为默认范围,这将使“self”技巧变得不必要。

Another common way to do it is the following:

private function addControls(){
  ...
  var self = this;
  b.addEventListener(flash.events.MouseEvent.CLICK,function() { 
    self.stopStream()
  });
  ...
}

The advantage is that "self" is correctly typed and doesn't require casting. We are considering to add "this" as the default scope in such cases which will make the "self" trick unnecessary.

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