Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is more of an architecture problem than a code problem. Personally, I create a <code>reset()</code> function on all custom classes. Inside this function, I do whatever's needed to set the object in it's default state; set position, alpha, visible, custom props etc.</p> <p>As for when to call it, it's really up to you, but a good design pattern for you would be the State design pattern. </p> <p>Basically, you have a <code>StateManager</code> in your game that holds and controls different <code>State</code> objects. These <code>State</code> objects can represent the different states of your game; <code>MainMenu</code>, <code>Play</code>, <code>GameOver</code>, <code>Reset</code>, etc.</p> <p>Each <code>State</code> would have a <code>begin()</code>, an <code>end()</code> and possibly an <code>update()</code>. When your <code>StateManager</code> switches states, it would call <code>end()</code> on the <code>State</code> leaving, and <code>start()</code> on the <code>State</code> coming in. You can only be in one <code>State</code> at a time, so it lets you easily encapsulate your logic depending on where you are in your game.</p> <p>Inside the <code>begin()</code> function, you set up everything you need for that particular state. For example, the <code>begin()</code> function for your <code>Play</code> state can add all the keyboard/mouse event listeners that you need to control your game. Inside the <code>end()</code> function, you clear everything that you set up. In the <code>end()</code> function for your <code>Play</code> state, you would remove all the keyboard/mouse event listeners for example. This would mean that it's impossible for the player to do any play logic unless they're in the <code>Play</code> state. If you had an <code>update()</code> function (that's called every frame) in your <code>State</code>, then you could, in the <code>Play</code> example, check if the player has no more lives yet, or has reached the score for the next level.</p> <p>For the reset logic, in your <code>Reset</code> state, you could call the <code>reset()</code> function on all your objects, or set them manually. The path through your game with states would look like this:</p> <p><code>MainMenu</code> (play) -> <code>Reset</code> (or an <code>Init</code> state) -> <code>Play</code> -> <code>GameOver</code> (replay) -> <code>Reset</code> -> <code>Play</code></p> <p>There's no built-in logic to reset object, you'll need to take care of it yourself. Adopting a pattern such as this can help with that.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload