This guide continues from Part 1.
In this part we will add audio to our game.
This will involve adding music to the main level and sound effects when the player jumps.
You can download free audio assets from kenney.nl here or use a tool to create your own.
This example uses the Music Jingles and Impact Sounds audio asset packs.
Extract the asset packs into your Assets \ Audio folder.
Open the Level1 scene.
Add a child node to the root node.
Add an AudioStreamPlayer. This plays the audio globally. the 2D and 3D players play audio in relation to the worldspace of the node they are attached to.
Now we need to attach the audio file to the audio player.
Select the AudioStreamPlayer (rename it MusicPlayer)
Go to the inspector.
Set the audio to autoplay
We now need to add the audio, drag an audio file into the Stream section.
You can also adjust the pitch of the audio.
If you double click on the audio file you can adjust other properties.
Save your work and test it by pressing the play button.
Playing audio when an action occurs (Play on Jump)
Now we want to add a sound that will play each time the user jumps.
Open the Player scene.
Add an AudioStreamPlayer2D to the root player object.
When you select an audio file in the FileSystem you can then preview it using the Inspector.
By default ogg files are imported with loop enabled.
Select the audio file and then the Import tab (next to Scene)
Uncheck the Loop checkbox.
Then click Reimport.
Switch back to Scene.
Add your chosen audio to the AudioSteamPlayer2D node.
We do not want it to autoplay as we will handle this with code later.
Select the audio stream and make sure that it is not set to loop
Click on the scroll to open the script attached to the Player node.
We are going to add code to the jump action so that the audio plays when the player jumps.
Add the points shown in 21 and 22 to add in a variable for the audio player and to play the audio inside the jump press and on the ground condition.
# 1 Extend the kinematic body node to allow for 2d physics extends KinematicBody2D # 2 Create a gravity variable of type int and set it to 800 # In capitals as it is a constant and won't change var GRAVITY : int = 80 # 3 Create a variable to store the player velocity # Set this to the type of a 2D vector e.g. x and y var velocity: Vector2 = Vector2() # 7 Create a variable to set the speed of the player # 20 Update the speed that the player moves var speed : int = 50 # 14 Jump force var jump : int = 50 # 4 Create a sprite variable and link it to the sprite node. When the node is # initialised. onready var sprite = $Sprite # 21 Add a variable for the audio stream player onready var audioPlayer = $AudioStreamPlayer2D # Called when the node enters the scene tree for the first time. func _ready(): pass # Replace with function body. # 5 Runs the physics updates to the game func _physics_process(delta): # 6 Update the y axis velocity by the time elapsed velocity.y += delta * GRAVITY # 8 Check is the left input is pressed # note player_left is the action in the input map if Input.is_action_pressed("player_left"): # 9 Update the velocity by the speed of the player # 18 set x velocity to -speed to have fixed speed velocity.x = -speed # 10 do the same for the moving right elif Input.is_action_pressed("player_right"): # 19 set x velocity to speed to have fixed speed velocity.x = speed # 11 If left or right is not pressed set the x velocity to 0 else: velocity.x = 0 # 12 Flip the sprite depending on the direction of movement if velocity.x < 0: sprite.flip_h = false elif velocity.x > 0: sprite.flip_h = true # 15 Detect the jump button being pressed # 17 Add the is_on_floor() check to the jump condition if Input.is_action_pressed("player_jump") && is_on_floor(): # 16 Multiply by -1 to turn the velocity into a negative value velocity.y = jump * -1 # 22 Play the audio for the jump audioPlayer.play() # 13 Move the object, the second parameter sets the vertical axis move_and_slide(velocity, Vector2.UP)
Test your program. When the player jumps the audio should play once.