0

我对操作脚本中的 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。这里有一个 deltas 列表http://haxe.org/doc/flash/as2_compare似乎没有涵盖这个问题,所以我相信这是我在 AS 上遇到的问题。它可能与编译器有关,但我希望不是因为到目前为止我真的很喜欢 Haxe。

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

4

2 回答 2

2

相信这是由于在 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();
}
于 2011-01-14T21:00:08.077 回答
1

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.

于 2011-01-14T23:31:47.417 回答