|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": null, |
| 6 | + "metadata": { |
| 7 | + "vscode": { |
| 8 | + "languageId": "yaml" |
| 9 | + } |
| 10 | + }, |
| 11 | + "outputs": [], |
| 12 | + "source": [ |
| 13 | + "---\n", |
| 14 | + "author: Lucas Masterson\n", |
| 15 | + "layout: post\n", |
| 16 | + "title: So you want to add music?\n", |
| 17 | + "description: Adding music to platformer\n", |
| 18 | + "permalink: /music\n", |
| 19 | + "categories: [Miscellaneous]\n", |
| 20 | + "toc: true\n", |
| 21 | + "---" |
| 22 | + ] |
| 23 | + }, |
| 24 | + { |
| 25 | + "cell_type": "markdown", |
| 26 | + "metadata": {}, |
| 27 | + "source": [ |
| 28 | + "## What am I doing here?\n", |
| 29 | + "\n", |
| 30 | + "You may have noticed a few levels have music (my personal favorite being the \"Regicide\" track from the Destiny: The Taken King OST, but I digress), and maybe you want to add that. I had the pleasure of figuring that out and now I may share my knowledge. See this [Pull Request](https://github.com/nighthawkcoders/platformer4x/pull/18)" |
| 31 | + ] |
| 32 | + }, |
| 33 | + { |
| 34 | + "cell_type": "markdown", |
| 35 | + "metadata": {}, |
| 36 | + "source": [ |
| 37 | + "## Introduction\n", |
| 38 | + "\n", |
| 39 | + "Most of this capability comes from `GameEnv.js` with these static methods:\n", |
| 40 | + "\n", |
| 41 | + "```js\n", |
| 42 | + "// ~/assets/js/platformer/GameEnv.js\n", |
| 43 | + "// Play a sound by its ID\n", |
| 44 | + "static playSound(id) {\n", |
| 45 | + " const sound = document.getElementById(id);\n", |
| 46 | + " sound.play();\n", |
| 47 | + "}\n", |
| 48 | + "\n", |
| 49 | + "// Play a sound by its ID in a loop\n", |
| 50 | + "static loopSound(id) {\n", |
| 51 | + " const sound = document.getElementById(id);\n", |
| 52 | + " sound.loop = true;\n", |
| 53 | + " sound.play();\n", |
| 54 | + "}\n", |
| 55 | + "\n", |
| 56 | + "// Stop all sounds\n", |
| 57 | + "static stopAllSounds() {\n", |
| 58 | + " const sounds = document.getElementsByTagName('audio');\n", |
| 59 | + " for (let sound of sounds) {\n", |
| 60 | + " sound.pause();\n", |
| 61 | + " sound.currentTime = 0;\n", |
| 62 | + " }\n", |
| 63 | + "}\n", |
| 64 | + "```\n", |
| 65 | + "\n", |
| 66 | + "I wrote the bottom two methods if you're curious. Anyway, I noticed that sound is played by its \"ID\". Well, what the heck is the id and why is it loaded from HTML (notice the `document.GetElementByID`). Took me a but longer than I should, but these IDs are loaded into HTML itself in `index.md`:\n", |
| 67 | + "\n", |
| 68 | + "```html\n", |
| 69 | + "<!--Index.md-->\n", |
| 70 | + "<!--Audio for music -->\n", |
| 71 | + "\n", |
| 72 | + " <!--Audio for Everlong by Foo Fighters (Winter) -->\n", |
| 73 | + " <audio id=\"everlong\" src=\"{{site.baseurl}}/assets/audio/everlong.mp3\" preload=\"auto\"></audio>\n", |
| 74 | + "```\n", |
| 75 | + "\n", |
| 76 | + "Ok, what does that do? Well, it loads a file from the filepath `{{site.baseurl}}/assets/audio/evelong.mp3` and assigns it the id `everlong`. Boom.\n", |
| 77 | + "\n", |
| 78 | + "Now, to play it on levels, I had to do a bit of a workaround. I didn't know where to load it to be unique to levels, so in a VERY jank workaround, I played it in upon loading the background.... Yeah not the best practice, but who'll know?\n", |
| 79 | + "\n", |
| 80 | + "```js\n", |
| 81 | + "// ~/assets/js/platformer/BackgroundSnow.js\n", |
| 82 | + "import Background from './Background.js';\n", |
| 83 | + "import GameEnv from './GameEnv.js';\n", |
| 84 | + "\n", |
| 85 | + "export class BackgroundSnow extends Background {\n", |
| 86 | + " constructor(canvas, image, data) {\n", |
| 87 | + " super(canvas, image, data);\n", |
| 88 | + "\n", |
| 89 | + " // Start the background music in loop\n", |
| 90 | + " GameEnv.loopSound('everlong');\n", |
| 91 | + "\n", |
| 92 | + " this.parallaxSpeed = 0.3; // Speed for vertical parallax scrolling\n", |
| 93 | + " }\n", |
| 94 | + "```\n", |
| 95 | + "\n", |
| 96 | + "Just import `GameEnv` and play the sound using `loopSound` to loop it or `playSound` if you just want to play it once." |
| 97 | + ] |
| 98 | + }, |
| 99 | + { |
| 100 | + "cell_type": "markdown", |
| 101 | + "metadata": {}, |
| 102 | + "source": [ |
| 103 | + "## I want it FAST\n", |
| 104 | + "\n", |
| 105 | + "OK, OK, sorry for boring you with my frankly uninteresting story. Where's the code?\n", |
| 106 | + "\n", |
| 107 | + "1. Add an audio file to `~/assets/audio/`. For example:\n", |
| 108 | + "\n", |
| 109 | + "```js\n", |
| 110 | + "// ~/assets/audio/everlong.mp3\n", |
| 111 | + "```\n", |
| 112 | + "\n", |
| 113 | + "2. Load this audio file into HTML:\n", |
| 114 | + "\n", |
| 115 | + "```html\n", |
| 116 | + "<!-- ~/index.md -->\n", |
| 117 | + "<!--Audio for Everlong by Foo Fighters (Winter) -->\n", |
| 118 | + "<audio id=\"everlong\" src=\"{{site.baseurl}}/assets/audio/everlong.mp3\" preload=\"auto\"></audio>\n", |
| 119 | + "```\n", |
| 120 | + "\n", |
| 121 | + "3. Play the sound (here we use a music example):\n", |
| 122 | + "\n", |
| 123 | + "```js\n", |
| 124 | + "// ~/assets/js/platformer/BackgroundSnow.js\n", |
| 125 | + "import Background from './Background.js';\n", |
| 126 | + "import GameEnv from './GameEnv.js';\n", |
| 127 | + "\n", |
| 128 | + "export class BackgroundSnow extends Background {\n", |
| 129 | + " constructor(canvas, image, data) {\n", |
| 130 | + " super(canvas, image, data);\n", |
| 131 | + "\n", |
| 132 | + " // Start the background music in loop\n", |
| 133 | + " GameEnv.loopSound('everlong');\n", |
| 134 | + "\n", |
| 135 | + " this.parallaxSpeed = 0.3; // Speed for vertical parallax scrolling\n", |
| 136 | + " }\n", |
| 137 | + "```\n", |
| 138 | + "\n", |
| 139 | + "4. Done! Should work.\n", |
| 140 | + "\n", |
| 141 | + "\n", |
| 142 | + "Some caveats though.\n", |
| 143 | + "- All game sounds are automatically stopped upon level transition (I placed a `GameEnv.stopAllSounds();` inside the `transitionToLevel()` function)\n", |
| 144 | + "- You can loop sounds using `loopSounds()` which is recommended! I used `playSound()` just for sake of example (which is more applicable to other sounds)\n", |
| 145 | + "- Choose good sounds/music please!\n", |
| 146 | + "\n", |
| 147 | + "> Potentia ex nihilo.\n" |
| 148 | + ] |
| 149 | + } |
| 150 | + ], |
| 151 | + "metadata": { |
| 152 | + "kernelspec": { |
| 153 | + "display_name": "Python 3", |
| 154 | + "language": "python", |
| 155 | + "name": "python3" |
| 156 | + }, |
| 157 | + "language_info": { |
| 158 | + "codemirror_mode": { |
| 159 | + "name": "ipython", |
| 160 | + "version": 3 |
| 161 | + }, |
| 162 | + "file_extension": ".py", |
| 163 | + "mimetype": "text/x-python", |
| 164 | + "name": "python", |
| 165 | + "nbconvert_exporter": "python", |
| 166 | + "pygments_lexer": "ipython3", |
| 167 | + "version": "3.10.12" |
| 168 | + } |
| 169 | + }, |
| 170 | + "nbformat": 4, |
| 171 | + "nbformat_minor": 2 |
| 172 | +} |
0 commit comments