Time for action — detailing the OnObjectUpdate method

The OnObjectUpdate method has one parameter, the object that is updated.

  1. Check if the object is equal to g.player.
    Method OnObjectUpdate:Int(obj:ftObject)
    If obj = g.player Then
    
  2. If the up arrow key is pressed, add 1.5 times the engine delta field value to the object (player) with the AddSpeed method. Also, spawn a player engine particle with g.SpawnPlayerEngine.
    If KeyDown(KEY_UP) 'Speed up
    obj.AddSpeed(1.5*delta)
    g.SpawnPlayerEngine()
    Endif
    
  3. Check if the left arrow key is pressed. Then set the angle, 15*delta degrees relative to its current angle with SetAngle.
    If KeyDown(KEY_LEFT) 'turn left
    obj.SetAngle(-15.0 * delta,True)
    Endif
    
  4. If the right arrow key is pressed, set the angle, +15*delta degrees relative to its current angle with SetAngle.
    If KeyDown(KEY_RIGHT) 'turn right
    obj.SetAngle(15.0 * delta,True)
    Endif
    
  5. Check if the S key was pressed, then spawn a player shot with g.SpawnPlayerShot.
    If KeyHit(KEY_S) 'Shoot your gun
    g.SpawnPlayerShot()
    Endif
    
  6. To fire up the shield, check if the Space key was hit. If the shield is not active, then activate it with g.ActivateShield.
    'Fire up shield
    If KeyHit(KEY_SPACE) And g.shield.isActive= False Then
    g.ActivateShield()
    Endif
    Endif
    Return 0
    End
    

What just happened?

Inside the OnUpdateObject method, you have added some logic to read the input from the player and control the movement of the player ship. Also, you can now activate the shield and fire a shot from your deadly cannon.

Did I win—checking the losing conditions with OnLayerUpdate

Every game needs winning conditions. Or at least losing conditions. A perfect place is the OnLayerUpdate method which is called when the engine updates a layer. In our game, the relevant layer is layerGame.

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

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