Time for action — setting up the player ship

  1. Add a new method called CreatePlayer to the game class.
    Method CreatePlayer:Int()
    
  2. Set the player object by calling CreateImage with our texture atlas. The positioning doesn't matter because we will do this later on.
    player = eng.CreateImage(atlas,0,0,32,32, 0,0)
    
  3. Now, set the friction and the maximum speed of the ship.
    player.SetFriction(0.2)
    player.SetMaxSpeed(20.0)
    
  4. To have the ship automatically wrap around the screen when it reaches its edges, set the wrapping to True via SetWrapScreen.
    player.SetWrapScreen(True)
    
  5. Reset the layer of the player object to layerGame.
    player.SetLayer(layerGame)
    
  6. The ship needs to be able to collide with comets, specifically. Set its collision group (grpPlayer) so that it can actually collide with comets set grpComets. The radius of the circle collision detection will be set to 12 pixels.
    player.SetColGroup(grpPlayer)
    player.SetColWith(grpComet, True)
    player.SetRadius(12)
    
  7. Now, set the player ship to be inactive so it doesn't show up at the start of the game automatically.
    player.SetActive(False)
    

    We now need to create the shield that protects the ship. The process of creation is similar to that of the player ship.

  8. Create the shield object.
    shield = eng.CreateImage(atlas,32,96,32,32,0,0)
    
  9. Set its scale, spinning speed, and layer.
    shield.SetScale(2.0)
    shield.SetSpin(15)
    shield.SetLayer(layerGame)
    
  10. By setting the parent as the player object, the shield will automatically move and rotate with the player ship.
    shield.SetParent(player)
    
  11. Now, set its collision group so that it can collide with comets and set its collision radius to 13 pixels.
    shield.SetColGroup(grpShield)
    shield.SetColWith(grpComet, True)
    shield.SetRadius(13)
    
  12. Also, set the shield to be inactive at the beginning of the game. Then, close the method.
    shield.SetActive(False)
    Return 0
    End
    

What just happened?

With the method CreatePlayer, you set up your ship in one go. It is always good to separate code in methods or functions, as it makes the code more readable.

Did you hear that—loading sounds

Our little game will feature two sound effects. One for a shot and one for an explosion. The nice thing is that you can create the code for loading the sounds to be format independent. fantomEngine will automatically choose the correct format depending on the platform. For HTML5, the best format so far is OGG, and for a FLASH game, it is MP3. To load the sounds, we will create a new method called LoadSounds.

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

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