Actionscript-静态函数和 UI 元素有问题吗?
我对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信这是由于在 MouseEvent.CLICK 处理程序中使用了匿名函数而没有使用事件本身。事件处理程序接受一个参数,即 MouseEvent 本身。因此,您必须执行以下操作之一:
或
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:
OR
另一种常见的方法如下:
优点是“self”输入正确并且不需要强制转换。我们正在考虑在这种情况下添加“this”作为默认范围,这将使“self”技巧变得不必要。
Another common way to do it is the following:
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.