Calling functions
MaineMathMan

Hi Guys,

Got another newbie question.  When I create functions/variables in init, how do I access them from the online interface?  If I don't use the interface and just write the app.js code I can get them to work, but calling them from the interface is eluding me.  Accessing variables from init..... ??? How would I update the score and scoreText from the following in one of my objects mouse down events (etc)

	this.init = function()
	{
	    var score = 0;
	    var scoreText= new TextSprite(score.toString(), '40px Arial', 'yellow');
	    var scoreObject= new SceneObject(scoreText);
	    wade.addSceneObject(scoreObject);
	    scoreObject.setPosition(-365, 270);

 

All 3 Comments
MaineMathMan

I sort of answered my own question.  It was a matter of scope.  This works...

doThisThing = function() { do stuff..... }

but this doesn't work

function doThisThing() { do stuff..... }

or this 

var doThisStuff = function (){ do stuff... )

Javascript isn't my first language, but shouldn't these functions be accessible to all others since they are the parents..... grrr

Gio

In the first case you are declaring a global variable called doThisThing. As such, this will be accessible everywhere.

In the second and 3rd cases, doThisThing will only be accessible inside the function where it's defined, and all its "child" functions, so to speak.

Using global variables works, but it's not very good practice in general. If you need to define a function, or a generally a variable, that is accessible from anywhere in your app, I would recommend storing it in the wade.app object, so:

wade.app.doThisThing = function() {...}

So you can call it from anywhere with wade.app.doThisThing() without polluting the global namespace.

MaineMathMan

Thanks.  I had to define it as wade.app.doThisThing  or wade.doThisThing and call it by the same in order for it to work.  Things are a little different in the C and PHP world. I appreciate the help.  Got things cooking now....

 

 

Post a reply
Add Attachment
Submit Reply
Login to Reply