Time for action — detailing the OnUpdate method

The update process will determine the current delta time, reset the collision markers, and do the collision checks. It also will check the buttons of the game pad.

  1. The first change is to add a Select statement inside the If statement that depends on the field gameMode.
    Method OnUpdate:Int()
    Local d:Float = Float(eng.CalcDeltaTime())/60.0
    If isSuspended = False then
    Select gameMode
    
  2. Check whether the game mode is equal to gmPlay.
    Case gmPlay
    
  3. Reset the number of crates that are on a target.
    OnTarget = 0
    
  4. Also, reset whether the player's collision zones hit a wall or a crate.
    hitWall = False
    hitWall2 = False
    hitCrate = Null
    hitCrate2 = Null
    
  5. Perform a collision check on the game layer.
    eng.CollisionCheck(layerGame)
    eng.Update(Float(d))
    

    While playing a level, we want the player to be able to switch back to the titlescreen.

  6. Check whether the X key was hit on either the keyboard or the Xbox controller.
    If KeyHit(KEY_X) Or JoyHit(JOY_X) Then
    
  7. Activate the title layer and set gameMode to gmMenu.
    g.layerTitle.SetActive(True)
    g.gameMode = g.gmMenu
    Endif
    

    In a former chapter we check for the winning conditions inside the engines callback methods. This time, we implement it inside the update process of the game class. It is all a matter of preference. There is no strict rule for anything.

  8. Check whether the total number of crates is equal to the number of crates on a target and that the game is in PLAY mode.
    If crateNum = onTarget And gameMode = gmPlay Then
    
  9. Set the game mode to gmNextLevel.
    gameMode = gmNextLevel
    
  10. Create a layer transition that will fade out the game layer within 2000 milliseconds. It will also have a transition ID attached to it so we can react to it when the transition is done.
    layerGame.CreateTransAlpha(0.01,2000,False,tidNextLevel)
    Endif
    
  11. Check whether gameMode is equal to gmMenu.
    Case gmMenu
    
  12. If the player hits the A key on the keyboard or the controller, start a new game.
    If KeyHit(KEY_A) Or JoyHit(JOY_A) Or TouchHit(0) Then
    StartNewGame()
    Endif
    
  13. If the player hits the Y key on the keyboard or the controller, end the game.
    If KeyHit(KEY_Y) Or JoyHit(JOY_Y) Then
    Error("")
    Endif
    
  14. Check that gameMode is equal to gmNextLevel.
    Case gmNextLevel
    
  15. Update the engine with a speed one-sixtieth the delta time; this is determined at the beginning of the OnUpdate method.
    eng.Update(Float(d)/60)
    
  16. Close the Select statement.
    End
    
  17. As we only have one dynamic info text object, we will update it here with the number of crates on the targets. This is the last change we make to the OnUpdate method.
    txtCrates.SetText("Crates on target: "+onTarget+"/"+crateNum)
    Endif
    Return 0
    End
    

What just happened?

Congratulate yourself. We just finalized the OnUpdate method of the game class. It will control the game flow by checking against the gameMode variable and also check whether certain keys are hit. We still can't control the player's machine, and collision detection is not implemented so far. But we will get there soon.

Save the script and run it. If you hit the A key, you should see the level as shown in the image at the beginning of the section The level maps—loading the tiles, in this chapter.

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

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