5

我想在 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");
  }
}

不幸的是,运行代码时出现以下错误:

空对象没有方法“callMethod”

在以下行:

上下文["this"].callMethod("_super", []);

context["this"] 似乎为空,所以我的问题是:如何引用 Dart 中的“this”变量?

更新 1:完整的示例代码可以在 github 上找到: https ://github.com/uldall/DartCocos2dTest

4

1 回答 1

1

this您可以使用JsFunction.withThis(f)捕获 Js 。使用该定义,将添加一个附加参数作为第一个参数。因此你的代码应该是:

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");
  }
}
于 2015-05-11T11:07:28.543 回答