Closure&Scope concepts in JS is pretty confusing, different than what I’m used to, I also wonder if there is way to declare private variables for the entire class instead of keep using this
its confusing at start but youll get use to it… patience and its not like youll do that every part of your code
in cocos creator u can use es6… its now almost looks like native code… try to google it
I’m also a newbie with JavaScript, but I think you should consider using onLoad instead of update, which is called at every frame!
What about this (please note I created and used a ‘local variable’, _myself, maybe the example you are looking for):
onLoad: function() {
var self = this;
self.node._myself = self; // store it to be able to access it later
self.node.on('touchstart', self.onTouchStart, self.node);
},
onTouchStart: function (event) {
var node = event.getCurrentTarget();
var self = node._myself; // retrieve the previously saved value
console.log("TOUCH");
self.spawnEnemy();
},
spawnEnemy: function () {
...
},
You can check which ‘node’ that your javascript file bind.
I meet same bug.
when i bind the ‘js’ file to a ‘layerout’ node , there’s the same error of ‘this.btn.on is not a function…’ .
After I changed the file binding to a ‘sprite’ , that’s OK !