Camp Imagination Videogame Design Arena Shooter: Sound
This lesson picks up where the Arena Shooter: Text lesson left off. It's time to add some sound to the game.
- Right-click on Sounds in the resource tree.
- Choose "Create" to open the Sound Editor.
- Name your sound "sndDestroy."
- Import the "explosion" sound from the "TutorialResources" folder.
- Open the Step event of the enemy object.
- Under the Set Score action, add a Play Audio action.
- Choose the sound file you created.
- Before the Play Audio action, add a Set Audio Pitch action.
- Select the sound, and set the pitch to "random_range(0.8, 1.2)
In general, you want to use .wav files for sound effects and .mp3 files for music. The random_range will set the pitch to a random number between 0.8 and 1.2 so the sound doesn't always sound the same.
Let's create a title screen that will appear before we start the game.
- Create a new sprite called "sprTitleBackground."
- Import the "bg_dark_tile" image from the "TutorialResources" folder.
- Create another new sprite called "sprTitleScreen."
- Import the "titlescreen" image from the "TutorialResources" folder.
- Change the origin of this sprite to "Middle Centre."
- Right-click on Rooms in the resource tree.
- Choose "Create" to open the Room Editor.
- Name your room "roomTitle."
- In the resource tree, drag the new room above "mainRoom" so it loads first.
- Select the "Background" layer of the new room and open the "Background Properties" section.
- Choose the "sprTitleBackground" sprite for the background.
- Check the horizontal and vertical tile checkboxes.
- Set the "Horizontal Speed" to "-2" to make the background scroll to the left.
If you run your game you should see the scrolling background. Now we need to create an object for the title screen text.
- Create a new object called "objTitle" and select the "sprTitleScreen" sprite.
- Add the new object to the center of the room.
- Add a Create event to the title object.
- Add a Set Instance Alpha action to the new event.
- Set the alpha to "0" to make the title start invisible.
- To make sure the title is in the exact center, add a Jump To Point action and set "X" to "room_width/2" and "Y" to "room_height/2."
- Add a Step event to the title object.
- Add a Set Instance Alpha action and set the alpha to relative .02.
- Add an If Variable action and check if "image_alpha" is greater than "1."
- Attach a Set Instance Alpha action to the if and set the alpha to "1."
The title should now fade in. Finally, we want to move to the next room when the "Enter" key is pressed.
- Add a Key Up event for the "Enter" key.
- Add the Go To Room action.
- Choose "roomMain" for the action.
Right now the game only has the few enemies you placed in the room and when you destroy them nothing happens. Let's make it saw enemies randomly spawn in the room.
- Remove the enemies from your main room.
- Create a new instance layer called "Enemies" in the main room and drag it below the "Instances" layer.
- Create a new object called "objEnemySpawn" and assign it the enemy sprite.
- In the Create event of this new object, add a Set Instance Scale action.
- Set the horizontal and vertical scales to "0.1."
- Create a Step event for this new object.
- Add a Set Instance Scale action to the Step event.
- Change the horizontal and vertical scales to relative "0.02"
- Add an If Variable action and check if "image_x" is greater than "1."
- To the right of the If Variable action, add a Set Instance Scale action and set both scales to "1."
- Also to the right of the If Variable action, add a Change Instance action and select the original enemy object.
- Go to the main room, and drag as many enemy spawners into the room as you want. This will represent the maximum number of enemies that can be in the room at once.
If you run your game, the enemies should spawn, but once you destroy them they are gone. Follow the steps below to make the enemies respawn.
- Create an object called "objSpawner."
- In the new object's Create event, add an Assign Variable action.
- Name the variable "spawnRate" and give it a value of "60."
- Also add a Set Alarm Countdown action and set alarm 0 to a countdown of "spawnRate."
An alarm runs when it counts down to 0. Now we need to add an event for alarm 0 to cause the enemies to spawn at a random location.
- Add an event for Alarm 0.
- Add two Get Random Number actions from the "Random" section.
- Call one random number "rndX" and set the maximum to "room_width."
- Call the other random number "rndY" and set the maximum to "room_height."
- Check "Temp" for both random numbers.
- Add a Create Instance action and choose "objEnemySpawn."
- Set "X" to "rndX" and "Y" to "rndY" on the "Enemies" layer.
- Add a Set Alarm Countdown action for alarm 0 with a countdown of "spawnRate."
- Remove the "objEnemySpawn" objects from the room and add a single "objSpawner" object.