Referring to "this" in Dart JS interop

I want to implement the following code in Dart:

var HelloWorldScene = cc.Scene.extend({ onEnter:function () { this._super(); } }); 

The implementation of My Dart is as follows:

 class HelloWorldScene { HelloWorldScene() { var sceneCollectionJS = new JsObject.jsify({ "onEnter": _onEnter}); context["HelloWorldScene"] = context["cc"]["Scene"].callMethod("extend", [sceneCollectionJS]); } void _onEnter() { context["this"].callMethod("_super"); } } 

Unfortunately, when I run the code, I get the following error:

The null object does not have a 'callMethod' method

in the next line:

context ["this"]. callMethod ("_ super", []);

context ["this"] seems null, so my question is: how can I refer to the "this" variable from Dart?

UPDATE 1: A complete code example can be found on github: https://github.com/uldall/DartCocos2dTest

+5
source share
1 answer

You can capture Js this with JsFunction.withThis (f) . With this definition, an additional argument will be added as the first argument. So your code should be:

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

All Articles