megabyterain Posted February 8, 2017 Share Posted February 8, 2017 Hello, So I am working on my game: https://megabyterain.itch.io/firewall and it works pretty well, but I wanted to add some features to it. Mainly, I wanted to add powerups to help keep things interesting. Anyway, while I was doing so, all the sudden I noticed my code got super messed up. No console errors pop up, but my game will just stop spawning blocks after you get a power-up. In my original game, the only timer was the one that controlled when new blocks should be created. However, when I added another timer to stop the powerups (simply changing a variable back to its original state) it seems like the timer that controls the creation of new blocks stops working. Here is some sample code createPackets: function(){ //creating the square/packets... game.time.events.add(800-(this.score*1.15),function(){ this.createPackets(); //recursive function. Worked fine before },this); } //other code and stuff //on collision if(daBlock.type == "slow"){ //if the box is a slowing powerup this.speedModifier = currentSettings.powerups.slow.amount; //set speed mofifier game.time.events.add(currentSettings.powerups.slow.time, function(){ console.log("stop slow powerup"); this.speedModifier = 1; },this); } //when I remove the second timer everything works fine, but otherwise the packets/boxes/squares stop getting created after getting a powerup Link to comment Share on other sites More sharing options...
ldd Posted February 8, 2017 Share Posted February 8, 2017 First of all, why would you ever want to recursively call a function inside a timer event? You are putting yourself at risk of so many errors. There's game.time.events.loop to do something similar. Just change that, and hope for the best. Link to comment Share on other sites More sharing options...
megabyterain Posted February 8, 2017 Author Share Posted February 8, 2017 18 minutes ago, ldd said: First of all, why would you ever want to recursively call a function inside a timer event? You are putting yourself at risk of so many errors. It's within a conditional statement, so it is only called if the state of the game is "play" as opposed to "fail". Also, the reason I did that is so the rate at which the blocks appear (along with there speed) increases as time goes on. 20 minutes ago, ldd said: There's game.time.events.loop to do something similar. Just change that, and hope for the best. That would not achieve my desired result of making the blocks appear faster as time goes on. Link to comment Share on other sites More sharing options...
mattstyles Posted February 8, 2017 Share Posted February 8, 2017 Recursively calling timeouts is fine in my view, I just checked the Phaser code and I'm pretty sure time.events.add is akin to setTimeout whereas loop calls the same function internally but never pops the function off the event stack, hence akin to a setInterval. I'd always prefer the recursive setTimeout method as its generally more controllable and easier to cancel and it makes sense here as you want to decrease the timeout based on some conditions. I don't think you've shown the problematic piece of code, just to make sure try adding a console log to both of your event callbacks (createPackets and the anonymous one) just to make sure they're not being called too frequently and being called when you expect. Can `800-(this.score*1.15)` ever return a negative number or is score always quite low? Not sure how Phaser handles negative time outs but I'd imagine its invalid or won't ever be called, you've probably checked this already but that would most likely stop the blocks from spawning. But check if your createPackets function is being called as you expect, if it is then no problem with the event timeout shizzle, something else is screwing with it. Link to comment Share on other sites More sharing options...
Recommended Posts