I'm having problems seeing how to solve the particular issue of returning the tile size to the caller. I have my own namespace called "goon" where i've added different functions, for example Tile below which i use to build my level. If i try:
console.log(goon.Tile.size); // This returns undefined.
console.log(goon.Tile.getTileSize()); // Throws the error: goon.Tile.getTileSize is not a function
[source lang="jscript"]goon.Tile = function(type, col, row) {
this.type = type;
this.size = 30;
this.top = row * this.size;
this.left = col * this.size;
this.bottom = row * this.size + this.size;
this.right = col * this.size + this.size;
this.getTileSize = function() {
return this.size;
};
}[/source]
So i tried adding a prototype and removed the above but this till doesn't work so obviously i'm doing something wrong here. I just need to get the size value from the function, is this even possible?
[source lang="jscript"]goon.Tile = function(type, col, row) {
this.type = type;
this.size = 30;
this.top = row * this.size;
this.left = col * this.size;
this.bottom = row * this.size + this.size;
this.right = col * this.size + this.size;
}
goon.Tile.prototype.getTileSize = function() {
return this.size;
};[/source]
Is the problem that i need to invoke or call the function first? Maybe you can't have a function within a function. Anyway, any help would be apprecieated.
[JS] Confused how to retrieve variable value from within a function
goon.Tile is a class, not an object.
"this" is applied on object, as far as I understand.
You need to write as,
function TileClass(blah) {
this.type = type;
// blah
}
goon.Tile = new TileClass(blah).
"this" is applied on object, as far as I understand.
You need to write as,
function TileClass(blah) {
this.type = type;
// blah
}
goon.Tile = new TileClass(blah).
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement