play
Enable Game Interaction
play() is the last key command in lifecycle management. It promotes the mini-game from the background running state (RUNNING) to the foreground interactive state (PLAYING).
In the RUNNING state, although the game logic is running, the engine does not dispatch user input such as touch and sensor (gravity) data by default, for safety and performance reasons. After calling play(), the engine enables event dispatch, and the player can officially start operating the game.
Method Declaration
java
/**
* Enables mini-game interaction.
* On success, the mini-game instance enters the `PLAYING` state.
* This interface can only be called when the mini-game is in the `RUNNING` state.
*/
void play();Runtime Behavior and State Transitions
1. Preconditions
- The current state must be
GAME_STATE_RUNNING. - Ensure the game view (GameView) has been added to the current window layout.
2. Execution Process
- The engine enables listening for and dispatching of Touch events.
- Restores callbacks from various sensors (such as the gyroscope and accelerometer).
- Internally marks the game as being in the active interactive state.
3. Result
- On success, the state changes to
GAME_STATE_PLAYING. - The player can tap buttons and control characters, and all interactive APIs within the game officially take effect.
Example Code
java
// Handle the play logic in the state listener
gameHandle.setGameStateListener(new SUDRTGameHandle.GameStateChangeListener() {
@Override
public void onStateChanged(int fromState, int toState) {
if (toState == SUDRTGameHandle.GAME_STATE_RUNNING) {
// The script started successfully and the screen is displayed; now allow player interaction
gameHandle.play();
}
}
});Notes
- Timing judgment: Do not call
play()immediately aftercreate(). You must wait forstart()to complete and trigger theRUNNINGstate callback before calling it. - Relationship with pause: Usually
play()is used to resume from pause or the background. If the user taps a native dialog that pauses the game, callplay()again after the dialog closes. - Input interception: If you find that the game screen is moving but tapping the screen has no response, it is usually because the
play()call was missed, or the current state has not yet successfully switched toPLAYING.
