Scene Management - State Variables
Last updated
Last updated
In the Khan Academy code examples, they provide an example of how to design logic for showing game-type scenes. The logic is the same as the Finite State Machine logic that we've used for our character.
Finite State Machine structure can be used to design a system if we have a finite number of states, such that we can list them in a table or diagram. In addition, we have a finite set of events that can cause the system to change state. Finally, an FSM must have one memory-variable that keeps track of the current state of the system.
We can use an FSM structure if we have a finite set of possible states for the system. For this example, we have 3 scenes, so we can say we have 3 states. In addition, we need a variable to keep track of the active State, here we'll use var currentState
and we need to initialize it to a valid and meaningful value.
var currentState = 1; // can have values 1, 2, 3
We need to define events that can change the state of the system. We can use the keypress event, we can say that the RIGHT arrow key is an event that will change to the next scene. We can also say that the LEFT arrow key can change the scene back to the previous scene.
The diagram below shows an example for scene management state-event logic where 3 events and 3 states are defined. The FSM diagram captures the logic to control scene management, however, this is defined at an abstract level which is independent of any specific programming language. The events listed: Start, Right, Left, are concept that could be implemented in a number of ways such as using keypress events or custom buttons, so the FSM specification gives a conceptual model of the event-driven system, where this system could be implemented using a wide variety of technology.
Example Project: https://www.khanacademy.org/computer-programming/scene-management-nov6/5124079971139584
In the program below, we define currentState to keep track of the current state, We define KeyPress events that listen for LEFT and RIGHT arrow keys to be pressed, if these buttons are pressed, the nextScene( ) or prevScene() functions are called to determine which is the next scene to be set as the currentState and which scene to draw. The nextScene( ) function provides one way to implement the left event from the FSM diagram above.