Jump to content

Understanding why a "fix" worked for bug involving state switching


tomorrowdog
 Share

Recommended Posts

I had a state serving as a level in my game. In the middle of one of the functions, it would check in an if statement to see if the criterion for beating the stage had been met and would call a function to change state back to main menu. If I then went from the main menu back to the stage after doing that state change, it'd be a buggy mess. I noticed that the "Quit" button that called a barebones function returning to the main menu didn't give me the same problem. What I tried is that after I did the "win" conditional check, I put the rest of the function in an else statement. And it worked! But I don't understand why and I'm concerned for the future. I thought the problem may have been global variables but I'm almost positive that that isn't true here. The way I understood changing states is that any code beneath it was irrelevant. Am I wrong in that assumption?

spawnRoom: function(){
    this.screen += 1;
    if(this.screen == 9){
        this.completeGame();
    }
    else{
        ....
    }
}

completeGame: function(){
    this.game.sound.stopAll();
    this.state.start('Menu');
}

 

Link to comment
Share on other sites

  • 2 weeks later...
On 12/21/2017 at 7:07 PM, tomorrowdog said:

The way I understood changing states is that any code beneath it was irrelevant. Am I wrong in that assumption?

state.start() works like any other function call — after it completes, control returns to the calling function, and any remaining statements are executed too.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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