Ambient settings

There are a ton of ambient settings available and most of them are pretty advanced in that they don't directly affect the gameplay. One thing that would be nice to have though is a soundtrack for our game. The settings for adding music are pretty straightforward, but they can really go a long way in setting the mood of the game.

We'll start by importing music in the Data Explorer window. The music that I am using came from OpenGameArt.org, because they have a ton of resources that you can use in your game. The song is called My New Favorite, Even with Reason (you'll find it in the downloadable content) and was composed by Delishus and uploaded by qubodup. Like everything else we import, we do it by clicking Import | Music. The import window is pretty simple—just browse for the music file and then depending on the type of file you are importing you may or may not be able to change the quality, channel, and sample rate. Once the file is imported, we can then open the Ambience Editor window in our viewport of choice and go to the Music tab, which is as shown in the following screenshot:

Ambient settings

As you can see, it is one of the simpler properties to configure—just click the musical note icon with the plus and select the music that we imported. If you want you can preview music by clicking the Play button. Otherwise, just note that the song name has an index number next to it; we'll now move on to the code to fire up the song.

Next, open MainAI in the Code desktop and open the onInit handler, then enter the following code:

-- Set the current scene to Level1 and 
-- compare to nil to make sure the scene loaded
if( application.setCurrentUserScene ( "Level1" ) ~= nil ) then
    -- Get a handle to the ship by using the ships 'name'
    this.hShip ( application.getCurrentUserSceneTaggedObject ( "Ship" ) )
    -- Play the rocket sound
    sound.play ( this.hShip ( ), 0, 0.5, true, 0.5 )
    -- Get a handle to the scene for playing music
    local hScene = application.getCurrentUserScene ( )
    music.play ( hScene, 0, 2 )
else
    -- Send an error message to the log 
    -- if the scene can not be set
    log.error ( "Unable to load initial scene" )
end

The highlighted code is all it takes to get the music playing in our game! We make a call out to the scene, tell it the index of the song to play, and the fade out in seconds that will happen between loops.

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

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