Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


[JS] Confused how to retrieve variable value from within a function


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 hdnine   Members   -  Reputation: 136

Like
0Likes
Like

Posted 09 November 2012 - 03:57 AM

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.

Sponsor:

#2 wqking   Members   -  Reputation: 756

Like
0Likes
Like

Posted 09 November 2012 - 04:21 AM

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).

http://www.cpgf.org/
cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.
v1.5.5 was released. Now supports tween and timeline for ease animation.


#3 hdnine   Members   -  Reputation: 136

Like
0Likes
Like

Posted 09 November 2012 - 04:29 AM

Right... will give it a try. Thanks!




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS