Time for action — acting on object update events

In this method, we will check whether certain keys are hit on the keyboard. This is to turn the plane or change its speed. Also, we make sure that the player can shoot bullets himself and spawn some smoke if the planes are hit. And finally, we make the enemy plane follow the player.

  1. Inside the OnObjectUpdate method, check whether the object is equal to g.player.
    Method OnObjectUpdate:Int(obj:ftObject)
    If obj = g.player Then
    
  2. If yes, set its basic speed to 8.0.
    obj.SetSpeed(8.0)
    
  3. Check whether the LEFT arrow key is held down on the keyboard. If yes, turn the player plane to the left.
    If KeyDown(KEY_LEFT) Then obj.SetAngle(-g.eng.GetDeltaTime()/16.0,True)
    
  4. Check whether the RIGHT arrow key is held down. If yes, then turn the player plane to the right.
    If KeyDown(KEY_RIGHT) Then obj.SetAngle(g.eng.GetDeltaTime()/16.0,True)
    
  5. If the UP arrow key is held down, set the object's speed to 10.
    If KeyDown(KEY_UP) Then obj.SetSpeed(10.0)
    
  6. Check whether the DOWN arrow key is held down. If yes, set the speed to 6.0.
    If KeyDown(KEY_DOWN) Then obj.SetSpeed(6.0)
    
  7. If the S key is held down, then spawn a shot.
    If KeyHit(KEY_S) Then g.SpawnShot(obj)
    
  8. Now, check whether the number of hits on the player plane is greater than zero. If yes, spawn some smoke. Then close the IF check.
    If g.hits > 0 Then g.SpawnSmoke(obj, g.hits)
    Endif
    
  9. Next, check whether the object is equal to g.enemy.
    If obj = g.enemy Then
    
  10. Let the object (the enemy) follow the player plane.
    FollowPlayer(obj)
    
  11. Check whether the number of hits on the enemy plane is greater than zero. If yes, then spawn some smoke. Close the IF check.
    If g.hitsC > 0 Then g.SpawnSmoke(obj, g.hitsC)
    Endif
    Return 0
    End
    

What just happened?

Yes, we can finally control the player plane and the enemy plane will now follow the player plane and try to shoot at it. Build and run the game, and see for yourself.

Hit it!—handling collision response

The initial collision check begins during the OnUpdate method of the game class. But, if a collision check is positive, the OnObjectCollision callback method is called. Here, you can handle the collision response.

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

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