Ok, folks, getting there with learning my JS. I have come across a singular one. Here's the code:
hangar = function(game){
}
hangar.prototype = {
loadImages: function(graphicAssets){
...
},
writeTerminal: function(timer, str, destination){
},
writeStats: function(){
var writeTerminal = this.writeTerminal;
console.log('wt', this.writeTerminal);
console.log('wt2', writeTerminal);
//ZOMG This does not work!
},
handleHangarInput: function(layerShips, layerBg){
... does some variable declarations of which one is:
var writeStats = this.writeStats;
function viewHangarPosition() {
writeStats(); // This works
}
keyleft.onDown.add(function(){
if (currentship > 0) {
currentship--;
viewHangarPosition();
}
});
keyright.onDown.add(function(){
if (currentship < shipNumber-1) {
currentship++;
viewHangarPosition();
}
});
}
create: function(){
this.handleHangarInput(layerShips, layerBg);
}
}
here is where it's all called (index.html):
window.onload = function() {
var game = new Phaser.Game(window.innerWidth, window.innerHeight, Phaser.AUTO, '', { preload: preload, create: create, update:update });
function preload() {
}
function create() {
game.state.add('menu', menu);
game.state.add('hangar', hangar);
game.state.start('hangar');
}
function update() {
}
}
I am trying to get hold of writeTerminal from within writeStats, which is not working for me.
- How is this done?
- Is there a smarter way to use writeStats instead of declaring it inside handleHangarInput?
Still not a pro in closures and Scoping. As always appreciate your help!
Edited with further code
Here's the revised code:
hangar = function(game){
}
hangar.prototype = {
loadImages: function(graphicAssets){
...
},
writeTerminal: function(timer, str, destination){
},
writeStats: function(){
console.log(this); // returns undefined
console.log(this.writeTerminal); // errors out!
},
handleHangarInput: function(layerShips, layerBg){
... does some variable declarations of which one is:
var writeStats = this.writeStats;
function viewHangarPosition() {
writeStats.call(this);
}
keyleft.onDown.add(function(){
if (currentship > 0) {
currentship--;
viewHangarPosition();
}
});
keyright.onDown.add(function(){
if (currentship < shipNumber-1) {
currentship++;
viewHangarPosition();
}
});
}
create: function(){
this.handleHangarInput(layerShips, layerBg);
}
}