Time for action — spawning a shot

The following method will spawn a bullet that will be harmful for any plane that crosses its path.

  1. Add the method SpawnShot to the game class. The parameter will be a plane object.
    Method SpawnShot:Int (plane:ftObject)
    
  2. Check whether the plane is the player's or the enemy's. If it is the enemy's, then also check whether it can shoot. This is to make sure that it cannot shoot each frame.
    If (plane = enemy And canShoot = True) Or plane=player Then
    
  3. Determine a vector that is located 40 pixels in front of the plane. That is the starting point for the bullet.
    Local vec:Float[] = plane.GetVector(40,0,True)
    
  4. Create a local object that will be a bullet.
    Local s:ftObject = eng.CreateImage(atlas,128,64,12,16, vec[0],vec[1])
    
  5. Set its angle to the plane's angle.
    s.SetAngle(plane.GetAngle())
    
  6. Set its speed to 15 and ensure that it will wrap around the screen edges.
    s.SetSpeed(15)
    s.SetWrapScreen(True)
    
  7. Next, set the collision group to grpShot.
    s.SetColGroup(grpShot)
    
  8. Enable it to collide with both the player and the enemy plane.
    s.SetColWith(grpPlayer, True)
    s.SetColWith(grpEnemy, True)
    
  9. The radius will be set to 6 pixels. By default, circle-to-circle collision detection is used.
    s.SetRadius(6)
    
  10. The bullet is allowed to fly for 2000 milliseconds. Create an object timer with a timer ID of tmDelete, which will fire in 2000 milliseconds.
    eng.CreateObjTimer(s,tmDelete,2000)
    
  11. Check whether plane is equal to enemy.
    If plane = enemy Then
    
  12. If yes, set canShoot to False and create an object timer with the ID tmCanShoot and a time of 300 milliseconds.
    canShoot = False
    eng.CreateObjTimer(plane,tmCanShoot,300)
    Endif
    

    Inside the engine class, we will detail the corresponding callback method and set the canShoot field back to True.

  13. Now, play the shooting sound effect.
    sndShoot.Play()
    
  14. Close the first IF check and the method.
    Endif
    Return 0
    End
    

What just happened?

Woohoo! We can shoot bullets now, with the method we just created. Not totally, because we have no trigger to actually shoot, but the method is there.

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

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