Time for action — detailing the OnUpdate method

The OnUpdate method of the game class is responsible for calculating the delta time since the last frame and updating the game, depending on which mode it is in.

  1. Start a Select statement on the game mode.
    Method OnUpdate:Int()
    Local d:Float = Float(eng.CalcDeltaTime())/60.0
    If isSuspended = False Then
    eng.Update(Float(d))
    Select gameMode
    
  2. Check for the constant gmPlay.
    Case gmPlay
    
  3. Start a touch check on the game layer to determine which gems are selected.
    If eng.TouchHit(0) Then
    eng.TouchCheck(layerGame)
    Endif
    
  4. Check if there are no gems moving.
    If layerGame.GetObjTransCount()= 0 Then
    
  5. Fill up empty slots with new gems.
    FillTiles()
    
  6. Check if gems line up and clear them. Then, close the IF check.
    ClearGems()
    Endif
    
  7. If the N key is pressed, reset the grid calling StartNewGame.
    If KeyHit(KEY_N) Then StartNewGame()
    
  8. If the Esc key is pressed, show the menu layer.
    If KeyHit(KEY_ESCAPE) Then ShowMenu()
    
  9. Update all the info text objects.
    UpdateInfoText()
    
  10. Check for the gmGameOver constant.
    Case gmGameOver
    
  11. If there was a 'touch hit', or if the Esc key was pressed, activate the menu layer.
    If eng.TouchHit(0) Or KeyHit(KEY_ESCAPE) Then
    gameMode=gmMenu
    ActivateLayer(gameMode)
    Endif
    
  12. Next, check for the gmScore constant. If there is a 'touch hit', then conduct a 'touch check' on the score layer.
    Case gmScore
    If eng.TouchHit(0) Then eng.TouchCheck(layerScore)
    
  13. Now, check for the gmMenu constant. Again, if there is a 'touch hit', then conduct a touch check on the menu layer.
    Case gmMenu
    If eng.TouchHit(0) Then eng.TouchCheck(layerMenu)
    
  14. And, finally, check for the gmTitle constant. If the Esc key is pressed, activate the menu layer.
    Case gmTitle
    If KeyHit(KEY_ESCAPE) Then
    gameMode=gmMenu
    ActivateLayer(gameMode)
    Endif
    End
    Endif
    Return 0
    End
    

What just happened?

Now, the update phase is set in stone. When you build and run the app, you can now press Esc in the title screen and it will switch to the menu screen. From there, you will be able to show the score layer and also start a new game.

To remind you what the menu screen looks like in the game, have a look at 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