Time for action — finalizing the OnUpdate method

The OnUpdate event is the place to update all game objects and control the flow of the game in general. You do everything in the method line-by-line and with hundreds of statements. However, we took the approach with building a lot of helper methods, which will now be called inside this method:

  1. Inside the OnUpdate method, store the result of a call to the method GetDeltaTime in the local variable d.
    Method OnUpdate:Int()
    Local d:Int = GetDeltaTime()
    
  2. Check with an If statement, whether the field isSuspended is FALSE.
    If isSuspended = False Then
    
  3. Add a Select statement with the field gameMode. Then, insert the Case statement for the constant gmPlay.
    Select gameMode
    Case gmPlay
    
  4. Now, call the Update method of your engine instance. As a speed parameter, we use the local d variable divided by 60. The speed parameter is a FLOAT value so you need to cast d to FLOAT.
    eng.Update(Float(d)/60.0)
    
  5. Now, check whether a TouchHit for the first finger is reported. On HTML5, this also reports a click with the left mouse button.
    If TouchHit(0) Then
    
  6. Because when you touch an atom element you create a new chain reaction, you need to set the game score to 0.
    score = 0
    
  7. Now, do a TouchCheck for the game and background layers. This will test each touchable object in these layers, if the touch coordinates are within the borders of that object. If yes, it will call the engine's OnObjectTouch method.
    eng.TouchCheck(layerGame)
    eng.TouchCheck(layerBackGround)
    Endif
    
  8. Now add a Case statement for the gmMenu constant.
    Case gmMenu
    
  9. Again check for a touch hit, and if yes, do a TouchCheck on the layerTitle layer. It will basically check if the PLAY button was hit.
    If TouchHit(0) Then
    eng.TouchCheck(layerTitle)
    Endif
    
  10. End the Select statement and update the info text objects.
    End
    UpdateInfoText()
    
  11. Now close the first If check.
    Endif
    Return 0
    End
    

What just happened?

That is it. We have the OnUpdate method set in stone. If the app is not suspended, we will do touch hit checks and update all the objects in the engine. If you are missing any collision checks, we will do this inside the engine class.

..................Content has been hidden....................

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