Time for action — acting on layer update events

In the OnUpdateLayer method, we want to update emitters and enemies. Also we want to check whether all tiles were removed and start the next level. If all lives of the player's ball are gone, we want to display the Game Over text. Follow the given steps:

  1. Let's add a check to see if the layer parameter of the method is equal to layerGame.
    Method OnLayerUpdate:Int(layer:ftLayer)
    If layer = g.layerGame Then
    
  2. Now, do a FOR EACHIN loop of the emitter list of the game class:
    For Local emitter := Eachin g.emitterList
    
  3. Check whether a call to the emitter's Update method returns FALSE. We will use the engine's current delta time as a parameter for the emitter's Update call. If it is, then the emitter is done and we will remove it from the list:
    If emitter.Update(g.eng.GetDeltaTime()) = False Then
    g.emitterList.Remove(emitter)
    Endif
    Next
    
  4. Next is a For EachIn loop through the enemy list of the game class:
    For Local enemy := Eachin g.enemyList
    
  5. Call the enemy's Update method and check whether it returns FALSE:
    If enemy.Update() = False Then
    
  6. Retrieve the corresponding fantomEngine object of the enemy instance:
    Local eo:ftObject = enemy.GetObj()
    
  7. Remove the first entry from the game's enemyList, and then the object from the engine:
    g.enemyList.Remove(enemy)
    eo.Remove()
    
  8. Also spawn a new enemy at a random x position:
    g.SpawnEnemy(Rnd(20,g.cw-20))
    Endif
    Next
    

    We handled particle emitters and enemy objects. Next will be the conditions to reach a new level, that is, when all tiles are destroyed.

  9. Check whether all tiles are gone and the game mode is set to gmPlay:
    If g.tileCount <= 0 And g.gameMode = g.gmPlay Then
    
  10. Create a new tile set and add 1 to the level number:
    g.CreateTiles()
    g.levelNum += 1
    
  11. Spawn an additional enemy to make the player's life just a little bit harder:
    g.SpawnEnemy(Rnd(20,g.cw-20))
    
  12. Remove the ball and recreate it at the starting position:
    g.ball.Remove()
    g.CreateBall()
    Endif
    

    Now, we should act on the fact that all lives are gone and the game is over.

  13. Check if no lives are left and the game mode is set to gmPlay:
    If g.lifes <= 0 And g.gameMode = g.gmPlay Then
    
  14. Make the Game Over text object visible:
    g.txtGameOver.SetVisible(True)
    
  15. Set the game mode to gmGameOver:
    g.gameMode = g.gmGameOver
    
  16. If the Game Over text object has no transition running, create a new scaling transition. It should scale to the factor 2.0 over 2000 milliseconds and with a transition ID of tidGameOver:
    If g.txtGameOver.GetTransitionCount()<= 0 Then g.txtGameOver.CreateTransScale(2.0,2000,False,g.tidGameOver)
    Endif
    Endif
    Return 0
    End
    End
    

What just happened?

Now that we act on the layer update events, we control the particle emitters totally and also all enemies. Adding to that, we check whether a level is cleared and the game is over.

We reached our parking position—a transition is done

Our particle effects will use transitions to reach their final destination. Once they are there, we want to delete these particles. That is why their transitions are fired with an ID.

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

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