Jump to content

Setting global var from within "update" function


jwfurness
 Share

Recommended Posts

Hello all,

 

I'm having trouble updating 'diamondDropSpeed' from within the update function. I've included the relevant code below. 'diamondTotal' is being calculated elsewhere and is counting properly, and the code in the if statement fires properly... I just can't get the diamondDropSpeed value in the if statement to update globally. Any advice would be greatly appreciated. Thanks!

BasicGame.Game = function (game) {		diamondDropSpeed = 1000;	};BasicGame.Game.prototype = {		create: function () {		    diamonds = this.game.add.group();			    diamondCreate = window.setInterval(function(){			        diamond = diamonds.create(200, 0, 'diamond');				    }, diamondDropSpeed);			},		update: function () {				if ( diamondTotal >= 1){			diamondDropSpeed = 200;		}	}};
Link to comment
Share on other sites

Looks like a scope / variable hoisting issue to me. Try this:

BasicGame.Game = function (game) {		this.diamondDropSpeed = 1000;	};BasicGame.Game.prototype = {		create: function () {		    diamonds = this.game.add.group();			    diamondCreate = window.setInterval(function(){			        diamond = diamonds.create(200, 0, 'diamond');				    }, this.diamondDropSpeed);			},		update: function () {				if ( diamondTotal >= 1){			this.diamondDropSpeed = 200;		}	}};
Link to comment
Share on other sites

Rich, thanks for your quick reply. Unfortunately, it's still not working. Perhaps you could take a look at the full code... I have the game up at thegospel.info/html5-games/manna. 

 

The setInterval duration I'm trying to change is at line 73, and the if statement with which I'm trying to change the var is line 117.

 

Appreciate your help, and I'm really enjoying the framework.

Link to comment
Share on other sites

Ah wait, you've got it inside a setInterval, no wonder it doesn't work (again it's a scope issue). I would suggest you use a Phaser.Timer instead, then you won't get any scope issues. Otherwise you need to either bind the setInterval function locally, or redefine 'this' before you use it inside the function.

Link to comment
Share on other sites

Well, I was able to implement the timer successfully, but am still not able to set the global var from within the update function. If you wouldn't mind taking a look again, the timer is lines 93-96, and the var update is line 126.

 

thegospel.info/html5-games/manna

 

Thanks.

Link to comment
Share on other sites

Sorry, I edited a previous post and am thinking this didn't go through. Any help appreciated, thanks.

 

Well, I was able to implement the timer successfully, but am still not able to set the global var from within the update function. If you wouldn't mind taking a look again, the timer is lines 93-96, and the var update is line 126.

 

thegospel.info/html5-games/manna

 

Thanks.

Link to comment
Share on other sites

I'm not sure how many more ways I can say the same thing, sorry. It's a variable scope issue - you should define ALL of your vars locally, so this:

this.combo = 0;

and not this:

combo = 0;

.. and then EVERYWHERE you reference that variable in your functions you should refer to it as this.combo.

 

You need to do this for every single variable. Otherwise all your vars get hoisted into the global (window level) scope and most likely over-write / conflict with each other. Get your scope under control and you'll be working from a level playing field and able to debug this properly (or as I suspect it will probably just start working as expected)

Link to comment
Share on other sites

I stripped it all the way back and started adding everything back in following your advice. However, I'm still having trouble getting the if statement at line 191 to update the 'diamondDropSpeed' variable in the timer at line 50-52.

 

thegospel.info/html5-games/manna

 

I guess the fundamental issue is that I don't know how to pull a variable up out of the function to make it globally available. I know this is more a basic Javascript question than a Phaser question, and I really appreciate your patience and advice. Thanks!

Link to comment
Share on other sites

Looking at your console.log the value IS getting set to 200 correctly (line 196), so your var is now being assigned properly and the scope is fine.

 

Are you expecting the Timer to update automatically to the new value? The problem is when you created the repeat Timer you gave it a repeat value of 10000000. So it won't change the repeat rate until all of those repeats are finished. Will you want the rate to change every time you collect a diamond? If so then use a "single fire" event not a repeating one, and just create a new one, using the new diamondDropSpeed value when you do so.

Link to comment
Share on other sites

What I wanted to do was change the value after the player had collected a certain number of diamonds. I'm keeping track of that number in diamondTotal. So, when diamondTotal hit 20, for example, I would change the speed. 

 

What I ended up doing was starting the initial timer, and then when the diamondTotal hit the limit, pausing the old timer and initializing a new one. Works for me.

 

Thanks for the great support.

Link to comment
Share on other sites

diamondTotal was the total number you had collected, right? In which case what you had there already looks like it should work fine instead of using a timer - you just need to reset the diamondTotal each time it hits the limit (or do a modulus check on it) otherwise the speed would only ever change once. So the more you collect, the faster it gets.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...