jwfurness Posted March 2, 2014 Share Posted March 2, 2014 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 More sharing options...
rich Posted March 2, 2014 Share Posted March 2, 2014 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 More sharing options...
jwfurness Posted March 2, 2014 Author Share Posted March 2, 2014 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 More sharing options...
rich Posted March 3, 2014 Share Posted March 3, 2014 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 More sharing options...
jwfurness Posted March 3, 2014 Author Share Posted March 3, 2014 Thanks, Rich. I wasn't aware of the phaser.timer. Will give it a whirl later on today. Link to comment Share on other sites More sharing options...
jwfurness Posted March 3, 2014 Author Share Posted March 3, 2014 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 More sharing options...
rich Posted March 3, 2014 Share Posted March 3, 2014 That's because it's not on that domain any more http://docs.phaser.io Link to comment Share on other sites More sharing options...
jwfurness Posted March 3, 2014 Author Share Posted March 3, 2014 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 More sharing options...
rich Posted March 3, 2014 Share Posted March 3, 2014 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 More sharing options...
jwfurness Posted March 3, 2014 Author Share Posted March 3, 2014 Thanks for bearing with me. Will give it a shot. Link to comment Share on other sites More sharing options...
rich Posted March 3, 2014 Share Posted March 3, 2014 No worries. JavaScript is really quite fussy about variable scope, and not being explicit with it can often have highly undesirable (and hard to debug) outcomes. Link to comment Share on other sites More sharing options...
jwfurness Posted March 4, 2014 Author Share Posted March 4, 2014 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 More sharing options...
rich Posted March 4, 2014 Share Posted March 4, 2014 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 More sharing options...
jwfurness Posted March 4, 2014 Author Share Posted March 4, 2014 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 More sharing options...
rich Posted March 4, 2014 Share Posted March 4, 2014 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 More sharing options...
Recommended Posts