<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Baptiste M. on Medium]]></title>
        <description><![CDATA[Stories by Baptiste M. on Medium]]></description>
        <link>https://medium.com/@bapmenard?source=rss-dd77a7802b84------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*q5fUeS6It93fsIosTXM0Ew.jpeg</url>
            <title>Stories by Baptiste M. on Medium</title>
            <link>https://medium.com/@bapmenard?source=rss-dd77a7802b84------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 18:28:49 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@bapmenard/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Making a game with Javascript: Part 2]]></title>
            <link>https://medium.com/hackernoon/making-a-game-with-javascript-part-2-8154bd6e2de1?source=rss-dd77a7802b84------2</link>
            <guid isPermaLink="false">https://medium.com/p/8154bd6e2de1</guid>
            <category><![CDATA[code]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[game-development]]></category>
            <category><![CDATA[pixijs]]></category>
            <category><![CDATA[games]]></category>
            <dc:creator><![CDATA[Baptiste M.]]></dc:creator>
            <pubDate>Wed, 21 Mar 2018 22:01:28 GMT</pubDate>
            <atom:updated>2018-03-24T19:02:00.765Z</atom:updated>
            <content:encoded><![CDATA[<h4>Building the spaceship</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*d6LciqpRc7XxOV_kCOU_NQ.png" /></figure><h4>Catching up</h4><p>Hi everybody! This article is the second part of my tutorial to make a little game with Javascript. <a href="https://hackernoon.com/making-a-game-with-javascript-and-pixijs-part-1-e3235139cd6f"><strong>You can read Part 1 here</strong></a><strong>.</strong></p><p><a href="https://github.com/Karzam/Spaceship_Tutorial_Part_2"><strong>The full source code of this tutorial can be found here</strong></a><strong>.</strong></p><h4>Creating the spaceship</h4><p>Now that the background is done, we can finally start setting up our spaceship!</p><p>Let’s <strong>create a </strong><em>Player.js</em><strong> file</strong> and <strong>add it to</strong> <em>index.html</em> as a script (same as <em>CloudManager</em>).</p><p>Here is a (beautiful) spaceship designed in a few minutes that you can <strong>add to your </strong><em>assets</em><strong> folder</strong> and in the loader<strong> </strong>function of <em>main.js</em>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/200/1*ExJFkzXCUvOL4D1OyWc0pw.png" /><figcaption>Spaceship</figcaption></figure><p>Basically, we want this ship to <strong>appear near the left border of the screen</strong>. We also <strong>need its anchor</strong> (position point of the object) to be <strong>in the center of the sprite</strong>, and because its maybe too huge, we can rescale it:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ab7c57139fb1b179a5612dd5cc113b40/href">https://medium.com/media/ab7c57139fb1b179a5612dd5cc113b40/href</a></iframe><p>Of course, we have to<strong> declare this object as a global variable of the </strong><em>main.js</em><strong> </strong>file, so it can be instantiated in the game:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b7524c6275370c64a602cb28aa99c87d/href">https://medium.com/media/b7524c6275370c64a602cb28aa99c87d/href</a></iframe><p>… and <strong>in the </strong><em>init</em><strong> function of the same file</strong> (just after <em>CloudManager</em>):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b248b4a8d37a9efadcaaace9f3cc11d9/href">https://medium.com/media/b248b4a8d37a9efadcaaace9f3cc11d9/href</a></iframe><p>Now you should see the glittering spaceship cruising.</p><p>However if you wait for a few seconds, you should get some troubles:</p><p><strong><em>The spaceship is flying behind the clouds!</em></strong></p><p>Yeah, because in fact, <strong>the last object created is moving behind the previous one. </strong>Clouds are spawning after the spaceship has been instantiated, so we need the cloud manager generating clouds in the bottom of the objects list. For this, we just have to <strong>update a bit the </strong><em>addChild</em><strong> </strong>of the<strong> </strong><em>CloudManager</em><strong> </strong>class:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b6b60b7695baba774ce2237c416f1eb3/href">https://medium.com/media/b6b60b7695baba774ce2237c416f1eb3/href</a></iframe><p><em>addChildAt</em> allows us to pass a second parameter, which is <strong>the position in the stage’s objects list</strong>. The furthest it is, the furthest the sprite will be on the screen.</p><p>“0” means the <strong>first index in the objects list</strong>, so each time we create a new cloud, we <strong>add it at the first position of the list</strong>, ensuring that <strong>it will appear behind the spaceship.</strong></p><p>Now that it’s done, we can start the spaceship controls.</p><h4>Moving the spaceship</h4><p>Let’s get back to <em>Player.js</em> class and <strong>add some lines in the constructor</strong>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b912da74660a5ae6b8c490bea1ff2c8e/href">https://medium.com/media/b912da74660a5ae6b8c490bea1ff2c8e/href</a></iframe><p>This allows the game to catch <strong>keyboard events</strong> (when a key is pressed and released). When one of this event occurs, the method at the end of each line is executed.</p><p><strong><em>Note</em></strong><em>: By adding “.bind(this)” at the end of the function, we pass the context of the object into it, which means we can still access its properties by calling “this”. If we don’t write it this way, the callback function is not able to determine which object we are working with. Just remember that if one of your callback function is getting an “undefined this” error, you might have to bind your object like this.</em></p><p>Let’s <strong>define 2 empty methods in the same class</strong> (we will fill these ones in a little while):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/618103a0abbf02f189e8a8be90736b75/href">https://medium.com/media/618103a0abbf02f189e8a8be90736b75/href</a></iframe><p>We also need 3 variables to move the spaceship: its<strong> horizontal and vertical directions</strong> (X &amp; Y) and the<strong> speed</strong> (all of them in the constructor):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0ffd31ce978f24202408235aa63ca416/href">https://medium.com/media/0ffd31ce978f24202408235aa63ca416/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7dae73601245a2aabf34226fbdf112f7/href">https://medium.com/media/7dae73601245a2aabf34226fbdf112f7/href</a></iframe><p>This object is basically an association of key codes and direction values:</p><ul><li>37 is the <strong>left arrow key</strong>, to move our spaceship to the left we need to decrease its X position.</li><li>38 is the <strong>up arrow key</strong>, to move our spaceship to the top we need to decrease its Y position.</li><li>39 is the <strong>right arrow key</strong>, to move our spaceship to the right we need to increase its X position.</li><li>40 is the <strong>down arrow key</strong>, to move our spaceship to the bottom we need to increase its Y position.</li></ul><p>Every time a key is pressed, we retrieve the corresponding direction:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d4c986f11b22ea0624f15c653fe7f78b/href">https://medium.com/media/d4c986f11b22ea0624f15c653fe7f78b/href</a></iframe><p>It’s looking pretty weird but let’s explain that: <strong>if the key code is 37 or 39 (which means left or right arrow key), then we just set the X direction</strong>. If it’s 38 or 40, you can guess what’s happening (yeah it’s actually the vertical direction). That’s all!</p><p>Let’s <strong>add the update method of the player</strong> (<a href="https://hackernoon.com/making-a-game-with-javascript-and-pixijs-part-1-e3235139cd6f">like the Cloud Manager, remember?</a>):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/193875ba77c776c2f5a1c053815c6067/href">https://medium.com/media/193875ba77c776c2f5a1c053815c6067/href</a></iframe><p>… and of course, <strong>don’t forget to call it in the loop function</strong> of <em>main.js</em>!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/762c4bc37b209d6a0df7589421ecc9e0/href">https://medium.com/media/762c4bc37b209d6a0df7589421ecc9e0/href</a></iframe><p>If you save and reload the game, it (should) work well, except that the ship <strong>keeps moving when we release the direction keys</strong>. It’s obvious because we didn’t complete the <em>onKeyUp</em> function, which is <strong>catching the key that has just been released</strong>. So this way we can stop it by setting either <em>directionX</em> or <em>directionY</em> to 0.</p><p>There is just one little problem there: <strong>what’s going on if we set the direction to 0 because we released the left key, but the right key is still pressed?</strong></p><p>Yeah, the spaceship is going to stop, and <strong>we would have to press the right key again to move</strong>, which is not very logic.</p><p>So why not check if the right key or left key is still pressed, so we can <strong>reset the direction to this previous pressed key?</strong></p><p>That’s what we are going to do. And for this we need to store the state of each key in a boolean, pressed or released (<em>true</em> or <em>false</em>). Let’s put all these states in an object variable in the <em>Player</em> constructor:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5f8f741f4500d8d08c53fcffd2f2a91b/href">https://medium.com/media/5f8f741f4500d8d08c53fcffd2f2a91b/href</a></iframe><p>If a key (identified by its own code) is pressed, then <strong>we just change its state to <em>true</em></strong>, and if it’s released we set it to <em>false</em>. Easy, right?</p><p>Let’s add this line in the beginning of <em>onKeyDown</em>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ef7847831d9ca6743e69d059750de3ac/href">https://medium.com/media/ef7847831d9ca6743e69d059750de3ac/href</a></iframe><p>… and this one in <em>onKeyUp</em>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/361ff57d81dee68e28c45e1d163652bf/href">https://medium.com/media/361ff57d81dee68e28c45e1d163652bf/href</a></iframe><p>Now we can continue with the <em>onKeyUp</em> logic: <strong>if one of the horizontal or vertical key is released but the opposite one is still pressed, we change the direction to the last one</strong>. <strong>If</strong> <strong>both of them are released, we stop the ship</strong>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a9eba9af0f426c4240286d23b23892bf/href">https://medium.com/media/a9eba9af0f426c4240286d23b23892bf/href</a></iframe><p><strong><em>Note:</em></strong><em> There are multiple ways of handling the spaceship move, this is certainly not the cleanest one, but I think it’s one of the simplest to understand.</em></p><p>We’re all set, save &amp; reload &amp; enjoy!</p><p><strong>Upgrade: </strong><em>The spaceship can exit the screen! Try to check its position before updating it.</em> <a href="https://github.com/Karzam/Spaceship_Tutorial_Part_2/blob/master/src/Player.js">You can check the result here</a>.</p><h4>Shooting rockets</h4><p>Now that we can control the ship, we want it to shoot rockets.</p><p>Let’s start by <strong>adding the sprite in the </strong><em>assets</em><strong> folder:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/50/1*3HQFL4Wbr9s3uahvNhHLNg.png" /></figure><p>… and <strong>add it to the assets loader</strong> as usual:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/663c5048febb54caf0befe9a6a2d3f40/href">https://medium.com/media/663c5048febb54caf0befe9a6a2d3f40/href</a></iframe><p><strong>Create a new file</strong> called <em>Rocket.js</em> in the <em>src</em> folder and <strong>add it</strong> in <em>index.html</em> with the others guys:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9eb88281423017ec62a849a458e36606/href">https://medium.com/media/9eb88281423017ec62a849a458e36606/href</a></iframe><p>Let’s go back to the <em>Player</em> class. We want it to be able to <strong>shoot a rocket if he presses the spacebar button</strong>. The <em>onKeyDown</em> function is already catching such events, so we just need to <strong>make a condition with the key code we want </strong>(spacebar in this example):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e392b6b47d4190976d4dc0011f65d183/href">https://medium.com/media/e392b6b47d4190976d4dc0011f65d183/href</a></iframe><p>We are giving the spaceship position in the Rocket constructor, so inside we just <strong>set its position to the spaceship one</strong> (with an offset to make it appear in front of the ship instead of the middle).</p><p>So let’s go, initialize <em>Rocket.js</em> with the constructor:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9088849f6f3e8a84a9cdc38b4df1f037/href">https://medium.com/media/9088849f6f3e8a84a9cdc38b4df1f037/href</a></iframe><p>Alright, if you save &amp; reload, <strong>you may be able to fire (static) rockets by pressing the spacebar!</strong></p><p>To make them move, we need to <strong>create an array variable</strong> containing all the rockets existing in the game (we don’t know how many of them are on stage):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1f52a478ad696b6062fb7556cc4381c7/href">https://medium.com/media/1f52a478ad696b6062fb7556cc4381c7/href</a></iframe><p>The <em>_list</em> variable is located outside of the class because it’s static, which means its <strong>value is unique and is not only one object’s property</strong> (unlike <em>this</em>). However we can get it and set it as we want (with the 2 first lines of the class).</p><p>We can <strong>push the current object inside this list</strong> (within the constructor) and <strong>declare the </strong><em>speed</em><strong> variable</strong> in the same time:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3fc20b93a76541aca45b676c27118058/href">https://medium.com/media/3fc20b93a76541aca45b676c27118058/href</a></iframe><p>… and also <strong>add the </strong><em>update</em><strong> method</strong>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5dacb90a31af6743c3e05ec218c1048b/href">https://medium.com/media/5dacb90a31af6743c3e05ec218c1048b/href</a></iframe><p>This is basically the same as before, we <strong>update the </strong><em>x</em><strong> position of the rocket </strong>(and not the y because it’s not moving vertically) and like the clouds, we <strong>remove it when it goes outside of the screen limits</strong>, except that this time it’s the right edge.</p><p>After that, in the loop of <em>main.js</em>, we just have to <strong>parse the rocket list</strong> and <strong>call the </strong><em>update</em><strong> function for each element:</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/04525cec027f1d1cc9bfe552c2edb1dc/href">https://medium.com/media/04525cec027f1d1cc9bfe552c2edb1dc/href</a></iframe><p>Save &amp; reload, and try it!</p><p>It’s shooting, but it isn’t automatic. You have to press the key for each rocket<strong> </strong>and when you don’t move, it’s kinda weird because it’s firing fast as hell. What we want is to be able to <strong>shoot automatically when the key is kept pressed, with an adjustable speed.</strong></p><p>Let’s <strong>define two new variables in the </strong><em>Player</em><strong> constructor</strong>: the fire speed (that can be modified) and the cooldown, which is going to be the timer value:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0bdeed73a0f3be2979faf9144a4c7262/href">https://medium.com/media/0bdeed73a0f3be2979faf9144a4c7262/href</a></iframe><p>We also need to <strong>update </strong><em>keyState</em> to <strong>add the space key</strong>, because we want to know if it’s pressed or not:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0cdc71a32b5ba72e8a17182d208b8c0d/href">https://medium.com/media/0cdc71a32b5ba72e8a17182d208b8c0d/href</a></iframe><p>Here is the function we are using to shoot (we need to <strong>call it in the </strong><em>update </em><strong>of the player</strong>):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/627ff4aef657659627bc0023dd4c85f9/href">https://medium.com/media/627ff4aef657659627bc0023dd4c85f9/href</a></iframe><p>It’s pretty simple here: <strong>we just increment the timer from 0 to the fire speed</strong> we set and if the key is pressed and the timer has reached the value, we <strong>spawn a rocket and reset the timer to 0.</strong></p><p>This function is permanently executed in the <em>update</em> loop of the player:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/59509e7b4c18077bc5dfc7000060bcc7/href">https://medium.com/media/59509e7b4c18077bc5dfc7000060bcc7/href</a></iframe><p>Work done! If you want it faster, just decrease the fire speed variable (and increase it for slower speed).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*43IY-GlgG93yqB87tBiDXg.gif" /><figcaption>Firing some rockets</figcaption></figure><h4>Next post soon: we are adding some enemies!</h4><p>Thank you for reading!</p><p><strong><em>If you have any questions or suggestions, please let me know so I can improve the next one!</em></strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8154bd6e2de1" width="1" height="1" alt=""><hr><p><a href="https://medium.com/hackernoon/making-a-game-with-javascript-part-2-8154bd6e2de1">Making a game with Javascript: Part 2</a> was originally published in <a href="https://medium.com/hackernoon">HackerNoon.com</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Making a game with Javascript: Part 1]]></title>
            <link>https://medium.com/hackernoon/making-a-game-with-javascript-and-pixijs-part-1-e3235139cd6f?source=rss-dd77a7802b84------2</link>
            <guid isPermaLink="false">https://medium.com/p/e3235139cd6f</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[pixijs]]></category>
            <category><![CDATA[games]]></category>
            <category><![CDATA[game-development]]></category>
            <dc:creator><![CDATA[Baptiste M.]]></dc:creator>
            <pubDate>Tue, 23 Jan 2018 08:11:11 GMT</pubDate>
            <atom:updated>2018-03-21T23:16:24.266Z</atom:updated>
            <content:encoded><![CDATA[<h4>Starting with an animated background</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HzqOJEbfVXOlhZ4Qh4qjMA.png" /></figure><h4>Introduction</h4><p>I really wanted to write a tutorial about a game technology I like to use, so here it is. In this story, we will start making a little shoot’em up game with PixiJS, a really simple and cool Javascript library.</p><p>What we are going to do exactly is to make a spaceship able to move and shoot, enemy waves coming through and a beautiful animated background with moving clouds. The first part (this story) will focus on the background.</p><p>Ready guys? Let’s nail it!</p><h4>Getting started</h4><p>Let’s start by setting up our project: I uploaded a code structure already set so we are all working with the same base. However if you want to make it yourself, I put a picture of my folder just below<strong>:</strong></p><p><a href="https://github.com/Karzam/Spaceship_Tutorial_Starter_Project">Click here to download the starter project</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/250/1*bKB5woPIR7lvDSl62Jer8w.tiff" /><figcaption>Project folder structure</figcaption></figure><p>We will need a local server to run the game: I invite you to download <strong>WAMP</strong> if you’re working with Windows, or <strong>MAMP</strong> for macOS, they are free and easy to use. Let’s <strong>put your game folder in the server one</strong> (<em>htdocs</em> for MAMP / <em>www</em> for WAMP) and type the localhost link in your favorite browser (for me: <em>http://localhost:8888/Spaceship/bin/</em>)</p><p>In <em>index.html</em>, we are importing the javascript files in the header:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8d92e34f54b52734273043793434d9d4/href">https://medium.com/media/8d92e34f54b52734273043793434d9d4/href</a></iframe><p>… and <strong>we need to do the same for every file we make</strong>. Then comes the initialization of Pixi (which is based on the WebGL render engine):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/21707d662e8eedda95a23e3ecbc690b6/href">https://medium.com/media/21707d662e8eedda95a23e3ecbc690b6/href</a></iframe><p>For this tutorial, we tell the game to cover the whole browser window, so if you try it now you will get an all-black background.</p><p>The <em>main.js</em> file is the place where all the game starts. It’s like a manager, with the first-executed function of the game, and the loop where we can tell the game what it needs to do on each frame. What we want is a blue background for the sky when the game starts, so let’s<strong> update the <em>init</em> function:</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fbda77cef776cb2d001685f0bc9c4fad/href">https://medium.com/media/fbda77cef776cb2d001685f0bc9c4fad/href</a></iframe><p>Pixi is using the <em>hex</em> color format, so <strong>you need to write your color code preceded by <em>0x</em></strong>. Let’s save and see the result in your browser!</p><h4>Clouds everywhere</h4><p>This background is very boring, let’s add some floating clouds.</p><p>First, let’s <strong>add a new </strong><em>CloudManager</em><strong> </strong>class file in the <em>src</em> folder (which is going to create and move the clouds):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5ddf56f22221ed87c6fd458083f36d67/href">https://medium.com/media/5ddf56f22221ed87c6fd458083f36d67/href</a></iframe><p>Don’t forget to <strong>add it in the </strong><em>index.html</em><strong> file</strong> as we did for <em>main.js</em>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/be72db722db90d8031f1f20f24607b5a/href">https://medium.com/media/be72db722db90d8031f1f20f24607b5a/href</a></iframe><p>The constructor is the entry point of this class where we can add the spawn function for our clouds. What we want is basically a method able to create a cloud every X seconds, and it’s fine because there is a javascript thing for this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d1b02fdecbfa92df89938027c074171e/href">https://medium.com/media/d1b02fdecbfa92df89938027c074171e/href</a></iframe><p>This piece of code, placed in the constructor, will <strong>call what’s inside the moustache brackets every</strong> <strong>1000 milliseconds</strong> (= 1 second).</p><p>Let’s <strong>add cloud sprites in the </strong><em>assets</em><strong> folder</strong>, and because it’s better we have 2 different images: (<strong>the clouds are white with transparent background so they were invisible on this page, but here are the links on GitHub </strong>;)</p><blockquote><a href="https://github.com/Karzam/Spaceship_Tutorial_Part_1/blob/master/bin/assets/cloud_1.png">https://github.com/Karzam/Spaceship_Tutorial_Part_1/blob/master/bin/assets/cloud_1.png</a></blockquote><blockquote><a href="https://github.com/Karzam/Spaceship_Tutorial_Part_1/blob/master/bin/assets/cloud_2.png">https://github.com/Karzam/Spaceship_Tutorial_Part_1/blob/master/bin/assets/cloud_2.png</a></blockquote><p>We need to load the sprites before the game starts, so <strong>add them in the </strong><em>Pixi.loader.add</em><strong> function:</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ba6a14c649126f88efc3e95e82457bf1/href">https://medium.com/media/ba6a14c649126f88efc3e95e82457bf1/href</a></iframe><p>Ok, now we can display the clouds in the <em>setInterval</em> method of the <em>CloudManager</em>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9ca494bfb64cc1ac9602e2cc82441996/href">https://medium.com/media/9ca494bfb64cc1ac9602e2cc82441996/href</a></iframe><p>To resume this code:</p><ul><li>First, we are <strong>computing a random number between 0 and 1</strong>, and either it’s less than 0.5 so we store the first sprite in a constant or otherwise it’s the second one.</li><li>Then, we <strong>create a new sprite object</strong> with the image we got in the previous line.</li><li>The origin point of this sprite is going to be its top left corner, so we <strong>set its anchor point in the middle.</strong></li><li>We have to <strong>display the cloud beyond the right border of the screen</strong>, so it can move to the left through the screen: <strong><em>renderer.width * 1.2</em></strong> is the left border position + the width of the screen + 20% of its width. We can be sure that we won’t see it spawning. For the y position, <strong><em>renderer.height * Math.random()</em></strong> is a number between 0 and the window height. So the cloud vertical position will be located <strong>between the top and the bottom of the screen.</strong></li><li>Finally, we <strong><em>addChild</em></strong> this cloud to the stage.</li></ul><p>If you run this code, nothing should appear, and it’s on purpose because they have to pop out of sight. So now we have to make them move.</p><p>The update function is the place to do it. But we need to store the clouds in an array so we can iterate through and set their positions. Let’s<strong> initialize a new array in </strong><em>CloudManager</em><strong> constructor:</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e2c1d63a67a9024649a585afc14d8284/href">https://medium.com/media/e2c1d63a67a9024649a585afc14d8284/href</a></iframe><p>… and push the clouds inside after the <em>stage.addChild</em> function:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/59ba3be72b2cc172a7ecd98f1b1abb40/href">https://medium.com/media/59ba3be72b2cc172a7ecd98f1b1abb40/href</a></iframe><p>Now we can <strong>iterate the array in <em>update</em> and set the position</strong> of each cloud:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/aeeffd6bd37d4ec7b06bcbe36145d2ee/href">https://medium.com/media/aeeffd6bd37d4ec7b06bcbe36145d2ee/href</a></iframe><p>Now it’s working!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/890/1*O-F1YaJaFMeijf6ewskl7A.gif" /><figcaption>Moving clouds!</figcaption></figure><p>Oh wait, something should actually annoy us: <strong>where are all those clouds going?</strong></p><p>Yeah, if we don’t remove them after they left the screen, they will continue to exist and it may cause some performance troubles<strong>. </strong>Let’s add a statement in <em>forEach</em> that delete them when their horizontal position is a little bit inferior to the left border of the screen (so we can’t see them popping out):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/939612acbf710ff771564dd2ec6ba2b6/href">https://medium.com/media/939612acbf710ff771564dd2ec6ba2b6/href</a></iframe><p>We’re done with the clouds!</p><p>What about making a random variation on the clouds size? <strong>Add this to the cloud creation block:</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c3370033c91d6a7d746531324992eeef/href">https://medium.com/media/c3370033c91d6a7d746531324992eeef/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*7fHsC9aCJwOe3f4WBjAbBA.gif" /><figcaption>Random clouds size</figcaption></figure><p>If you something’s missing / not working in your code, <a href="https://github.com/Karzam/Spaceship_Tutorial_Part_1"><strong>you can check the result here</strong></a><strong>.</strong></p><h4><a href="https://hackernoon.com/making-a-game-with-javascript-part-2-8154bd6e2de1">Next part here: the player spaceship</a></h4><p>Thank you for reading!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e3235139cd6f" width="1" height="1" alt=""><hr><p><a href="https://medium.com/hackernoon/making-a-game-with-javascript-and-pixijs-part-1-e3235139cd6f">Making a game with Javascript: Part 1</a> was originally published in <a href="https://medium.com/hackernoon">HackerNoon.com</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>