Time for action — detailing the OnUpdate method

During the OnUpdate method, we will do different things depending on the gameMode field. If the mode is gmPlay, we will update all objects and do collision checks. In other modes, we mostly check for certain keys that are hit on the keyboard to switch the game into a different mode.

  1. Add a Select statement with the gameMode field as a parameter.
    Method OnUpdate:Int()
    Local d:Float = Float(eng.CalcDeltaTime())/60.0
    If isSuspended = False Then
    Select gameMode
    
  2. Check against the gmPlay constant.
    Case gmPlay
    eng.Update(Float(d))
    

    The call to the Update method of the engine is now part of the gmPlay segment.

  3. Now, check for any collisions happening on the game layer. The response for this will be detailed inside the engine class.
    eng.CollisionCheck(layerGame)
    
  4. Check if the Esc key was hit.
    If KeyHit(KEY_ESCAPE) Then
    
  5. If yes, activate the title screen and stop the plane engine sound.
    layerTitle.SetActive(True)
    sndEngine.Stop()
    
  6. Also, set the game mode to gmMenu.
    gameMode = gmMenu
    Endif
    
  7. Update the score info text objects with the current score values.
    txtScore.SetText("Player: "+score)
    txtScoreC.SetText("Computer: "+scoreC)
    
  8. Check whether gameMode is equal to gmMenu.
    Case gmMenu
    
  9. And then, check whether the P key was hit. If yes, start a new game.
    If KeyHit(KEY_P) Then
    StartNewGame()
    Endif
    
  10. Now, check whether the Esc key was hit. If yes, stop the game via an Error call.
    If KeyHit(KEY_ESCAPE) Then
    Error ("")
    Endif
    
  11. Lastly, check whether gameMode is equal to gmGameOver.
    Case gmGameOver
    
  12. During this mode, check whether the Esc key was hit.
    If KeyHit(KEY_ESCAPE) Then
    
  13. If yes, set gameMode to gmMenu and activate the title layer.
    gameMode = gmMenu
    layerTitle.SetActive(True)
    
  14. Close the last IF check and the Select statement.
    Endif
    End
    Endif
    Return 0
    End
    

What just happened?

By detailing the OnUpdate method of the class game, we can now start a new game and also switch back to the title screen. The player can't control the plane for now, but we will implement this, later in this chapter.

If you build and run the game at this moment, and start a new game, the console should look like the following screenshot:

What just happened?
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.47.25