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

残念ながら、コードを実行すると次のエラーが発生します。

null オブジェクトにはメソッド「callMethod」がありません

次の行で:

context["this"].callMethod("_super", []);

context["this"] は null のように見えるので、私の質問は次のとおりです。Dart から「this」変数を参照するにはどうすればよいですか?

更新 1: 完全なサンプル コードは github にあります: https://github.com/uldall/DartCocos2dTest

4

1 に答える 1

1

JsFunction.withThis(f)でJsthisをキャプチャできます。その定義では、追加の引数が最初の引数として追加されます。したがって、コードは次のようになります。

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 に答える