[英]Referring to “this” in Dart JS interop
我想在 Dart 中实现以下代码:
var HelloWorldScene = cc.Scene.extend({
onEnter:function () {
this._super();
}
});
我的 Dart 实现如下所示:
class HelloWorldScene {
HelloWorldScene() {
var sceneCollectionJS = new JsObject.jsify({ "onEnter": _onEnter});
context["HelloWorldScene"] = context["cc"]["Scene"].callMethod("extend", [sceneCollectionJS]);
}
void _onEnter() {
context["this"].callMethod("_super");
}
}
不幸的是,我在运行代码时收到以下错误:
null 对象没有方法“callMethod”
在以下行:
context["this"].callMethod("_super", []);
context["this"] 似乎为空,所以我的问题是:如何从 Dart 引用“this”变量?
更新 1:完整的示例代码可以在 github 上找到: https : //github.com/uldall/DartCocos2dTest
您可以使用JsFunction.withThis(f)捕获 Js this
。 根据该定义,将添加一个附加参数作为第一个参数。 因此你的代码应该是:
import 'dart:js';
class HelloWorldScene {
HelloWorldScene() {
var sceneCollectionJS =
new JsObject.jsify({"onEnter": new JsFunction.withThis(_onEnter)});
context["HelloWorldScene"] =
context["cc"]["Scene"].callMethod("extend", [sceneCollectionJS]);
}
void _onEnter(jsThis) {
jsThis.callMethod("_super");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.