I want to implement the following code in Dart:
var HelloWorldScene = cc.Scene.extend({
onEnter:function () {
this._super();
}
});
My Dart implementation looks like this:
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 I get the following error when running the code:
The null object does not have a method 'callMethod'
on the following line:
context["this"].callMethod("_super", []);
context["this"] seems to be null, so my question is: How do I refer to the "this" variable from Dart?
UPDATE 1: The full example code can be found on github: https://github.com/uldall/DartCocos2dTest
You can capture the Js this
with JsFunction.withThis(f). With that definition an additionnal argument will be added as first argument. Thus 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");
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With