In-Game Radio Stations: Randomized music based on user input

Attached Files

The following files have been attached to this tutorial:

.capx

radio-stations.capx

Download now 15.84 MB

Stats

753 visits, 1,140 views

Translations

This tutorial hasn't been translated.

Tools

Do you want to add randomized music to your game? And maybe allow the player to change it based on music genre like an in-game radio?

Well, now here's a relatively simple way to add Radio Stations to your game with a handful of Events.

In this tutorial we'll start by importing our music using a naming convention that will simplify our events, then we'll add the objects and events needed to play the music and finally we'll add in a way for players to change radio stations.

There is also an example capx (r244) attached to this tutorial that you can use to follow along and see a working set up.

The capx contains music by Eric Matyas at www.soundimage.org that is free to use (even commercially) as long as long as there is proper attribution.

EDIT: The attachment option seems to have issues, here is an alternate link

Naming and Importing Music

Name your music files based on the stations you want and import them into Construct 2

In the example capx, the stations are Fantasy, ChipTune and Sci-Fi, with 4 tracks per station, so the music is named:

Fantasy1, Fantasy2, Fantasy3, Fantasy4

ChipTune1, ChipTune2, ChipTune3, ChipTune4

Sci-Fi1, Sci-Fi2, Sci-Fi3, Sci-Fi4

It's important to have the same number of music tracks per station to simplify things.

Setting up Objects and the Event Sheet

We'll need the Audio and Function objects, so add those to your layout

Also add a Global Variable called RadioStation, with Type set to Text and Initial Value set to "None" or it can be left blank, just don't set it to a radio station you use or the next part won't work.

Now add an Event/Function to handle changing the station. The function will take one Parameter for the new station and we'll only switch stations if the new station is different to the old one.

    Function: OnChangeStation
    Function.Param(0) != RadioStation
    	-> Audio: Stop RadioStation
    	-> Set RadioStation to Function.Param(0)
    	-> Play RadioStation & choose(1,2,3,4), Tag = RadioStation

If you want, you can leave out the condition that compares the parameter to the RadioStation global variable, and it will pick a new track for the current station if they match.

Next add an Event to start playing a random radio station when the layout starts:

    On start of layout
    	-> Function: Call "OnChangeStation" (choose("Fantasy","ChipTune","Sci-Fi"))

If you want to wait for user input before you start playing, you can leave this Event out.

Finally add an Event to start the next track when the current track finishes:

    Audio: On RadioStation Ended
    	-> Play RadioStation & choose(1,2,3,4), Tag = RadioStation

If you set up all the Events correctly we should now have a functioning "radio" that randomly picks a different station at the start and plays random tracks, but we lack a way for players to change station.

Allowing Players to change station

There are many ways to set up user input to change the station, whichever way you choose, you then only need to add an action calling the function OnChangeStation, passing the name of the new station as a parameter.

In the example capx, we added a SpriteFont object and named it StationsButtons. To this we added an instance variable called Station, we then made 3 instances of this object and set the Text and Station values appropriately for our 3 stations.

We also added the Touch object (which works with left-clicking the mouse as well).

This allowed us to add a single Event to change stations based on user input

    Touch: On tap object StationsButtons
    	-> Function: Call "OnChangeStation" (StationsButtons.Station)

Now players can simply switch between stations by clicking on text labels.

We hope you enjoyed this tutorial and found it useful for your game development.

Credits for the Music in the Example

Music by Eric Matyas

www.soundimage.org

Fantasy: Misty Bog, Lost Jungle, Magic Clock Shop, Magic Ocean

ChipTune: It's Raining Pixels, The Ice Cream Man, Arcade Heroes, Pixelville

Sci-Fi: Terraforming Begins, Cosmic Switchboard, Lost Forever, Star Light

.CAPX

radio-stations.capx

Download now 15.84 MB
  • 0 Comments

  • Order by
Want to leave a comment? Login or Register an account!