Time for action — spawning some shots

  1. As always, create a new method. This time it is called SpawnPlayerShot.
    Method SpawnPlayerShot:Int()
    
  2. We need to get the position that is 20 pixels in front of the ship. For this, we can use the GetVector method. It needs the distances and the angle from it, which will be the same as the ship's angle.
    Local ds:Float[] = player.GetVector(20, player.angle)
    
  3. Create a local shot image from the sprite atlas.
    Local sht:ftObject = eng.CreateImage(atlas, 0,96,16,16, ds[0],ds[1])
    
  4. Set the angle of the shot to the same as the ship.
    sht.SetAngle(player.angle)
    
  5. Set the maximum speed to 25 and the actual speed to the player speed + 10.
    sht.SetMaxSpeed(25.0)
    sht.SetSpeed(player.speed+10.0)
    
  6. Scale the object down to a half and let it wrap around the screen edges.
    sht.SetScale(0.5)
    sht.SetWrapScreen(True)
    
  7. Reset the layer of the shot to layerGame.
    sht.SetLayer(layerGame)
    
  8. Add an object timer with the ID tmObjRemove to remove the shot after 2500 milliseconds.
    eng.CreateObjTimer(sht, tmObjRemove, 2500)
    
  9. Set the collision parameters.
    sht.SetColGroup(grpShot)
    sht.SetColWith(grpComet, True)
    sht.SetRadius(2)
    
  10. Play the sndShot sound and close the method.
    sndShot.Play()
    Return 0
    End
    

What just happened?

Besides spawning the shot from the player ship and letting it travel in the direction your ship was heading to, this method will also play the corresponding sound effect for the shot.

Save some high scores, will ya!

To save the high score list to the app's state, we will create a new method.

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

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