<?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 Hannah Robot on Medium]]></title>
        <description><![CDATA[Stories by Hannah Robot on Medium]]></description>
        <link>https://medium.com/@hannahrobot?source=rss-73f184629116------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*gvaorMRI0YOPCT90TVkVNA.png</url>
            <title>Stories by Hannah Robot on Medium</title>
            <link>https://medium.com/@hannahrobot?source=rss-73f184629116------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 10:16:28 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@hannahrobot/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[Build An Among Us Inspired Live-Multiplayer Game with Phaser 3 and Socket.io: Part 3]]></title>
            <link>https://medium.com/hannahrobot/build-an-among-us-inspired-live-multiplayer-game-with-phaser-3-and-socket-io-part-3-222a618694f3?source=rss-73f184629116------2</link>
            <guid isPermaLink="false">https://medium.com/p/222a618694f3</guid>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[socket-programming]]></category>
            <category><![CDATA[multiplayer-game]]></category>
            <category><![CDATA[tutorial]]></category>
            <category><![CDATA[phaser-3]]></category>
            <dc:creator><![CDATA[Hannah Robot]]></dc:creator>
            <pubDate>Wed, 16 Dec 2020 21:21:58 GMT</pubDate>
            <atom:updated>2020-12-17T15:29:52.820Z</atom:updated>
            <content:encoded><![CDATA[<p><em>This is part 3 of a 3 part series. To view part 2, </em><a href="http://build-among-us-live-multiplayer-phaser-3-socket-part-2/"><em>click here</em></a><em>. If you get lost at any time during this tutorial, you can always refer to the solution code </em><a href="https://github.com/hannahrobot/amongus-tutorial"><em>here</em></a>.</p><p>In part 2 we completed our waiting room and enabled the creation of private rooms with socket.io. In part 3 we will create our players and game functionality.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*uf-EGYXbUNm7hur-22LbJA.png" /></figure><p><strong>Add Players:</strong></p><p>Now that we’ve created the logic for joining our game room, we can implement what needs to happen upon joining. Inside of our ‘joinRoom’ socket listener callback function, let’s add our player to our game room object in the server state:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/427/1*ABq4LlzmFbZ9EUkUzY5UmA.png" /></figure><p>Now we can update our numPlayers variable on the gameRoom object. Additionally, we will emit the state to the client by sending the gameRoom object to the player that just joined via their socket:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/392/1*DgcEXWUjWdrAM4KG50SBnw.png" /></figure><p>Add a state property to the MainScene class:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/471/1*qSOuorBJA1qASnTQc2aMSQ.png" /></figure><p>In our create method inside MainScene, we can listen for ‘setState’. Inside of the callback function, we deconstruct off of the state object we receive, and assign it to our client site state object on the MainScene class.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/542/1*JMkEPzU9ng1ztpU-ke7ZQQ.png" /></figure><p>Lets head back to <em>socket/index.js</em> to add an emit for our current player, and tell everyone in the game room about our new player that joined. Inside our ‘joinRoom’ callback, we can add these emits:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/476/1*UfzvNfEa4qFzxvNFwGBt1g.png" /></figure><p>Back in MainScene we add a listener for ‘currentPlayers’:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/488/1*Y7Htp5un3EdpwUc4R1_gdg.png" /></figure><p>Here we use a method that we will create on the MainScene class for adding our player:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/508/1*-7cKqnp9y7ynX3Shp65WOw.png" /></figure><p>If the socket id that we receive is not ourself as the player, we call a different method on the class for adding other players:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/515/1*B515I-zKiOZFfAIFLHBUiw.png" /></figure><p>You’ll notice that we depend on a scene.otherPlayers group to add the other players at the bottom of our method, add this towards the top of the create function:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/572/1*GGO2pIQX8bth8WyYMESEOQ.png" /></figure><p>Back to line 54 of our code, below our ‘currentPlayers’ socket listener, add another socket listener for ‘newPlayer’:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/493/1*3A37ZnhSsAkp-Iw2tSAK4w.png" /></figure><p>If you <em>npm run start-dev</em> and open your localhost:8080, you should see a player on the screen. Now, open an incognito window, go to localhost:8080 and enter the same room code you just used. There should be another astronaut on the screen now! But they cant move! Let’s fix that.</p><p><strong>Add player Movement</strong></p><p>Inside of our update function, let’s handle player movement. We place this inside of the update function because update can recognize these actions at any time, as opposed to create which runs once when the scene is launched.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/496/1*3wyr_Crg3tQPFK69r9ABnw.png" /></figure><p>First, we check if the astronaut exists to avoid reference errors. Inside of this if statement, we set our speed to a variable, and we use setVelocity to set the initial movement to 0. We check if the left cursor is being pressed, and if it is, we set the x axis velocity to our speed as a negative number, and if our right cursor is being pressed, we set the x axis velocity as a positive integer. If you take a look at vertical movement, we follow the same logic. Open your browser again and give it a try, your astronaut should be moving all over! But if you open an incognito, the movement isn’t emitted. Add the following code below the movement that we just created:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/478/1*7RDuPUcXhfcaoCO2Be5M_w.png" /></figure><p>In our <em>socket/index</em> we add our listener, update the players movement on the server state, and broadcast it to the other players in our game room:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/554/1*VTzqgNUHQBBGvKoUpIutnQ.png" /></figure><p>Back to MainScene to add the listener for ‘playerMoved’, this can go inside our create method, since its a listener, it can be pinged at any time if its initialized in create:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/587/1*V0LYU9LCxveFs6Ywk25_2Q.png" /></figure><p>Give it a whirl in the browser and you should see that player movement is reflected on both browsers.</p><p><strong>Handle Disconnect</strong></p><p>In case someone leaves the came, we need to update our players object on the gameRoom, and update the client state. We tackle this in the following code:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/578/1*g9862quLM43tbnKwQhcK4Q.png" /></figure><p>On our client side in MainScene, at the bottom of the create method, we add the listener and update our client side state to reflect the servers players and numPlayers:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/593/1*5FRaXxa7ADut6byoWluI1A.png" /></figure><p>Try adding two players to your game and then closing one of your browsers, you should see the other player disappear and the console log in your terminal.</p><p><strong>Add a Task Control Panel</strong></p><p>Let’s add a control panel for tasks to pop up when it is clicked, just like in Among Us !</p><p>Create a control panel entity in our entity folder, and import it into MainScene. Also preload the image for our control panel, here we call it vending machine:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/849/1*7kCnRf_hwlfW6k6ch0NmbQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/645/1*Na9s6Fp7XkcOBpglNvO8Ow.png" /></figure><p>Create a control panel group and an instance of vending machine, and place is above players inside create:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/546/1*Wd73hG1SGuWtHRBs0BBqCQ.png" /></figure><p>The vending machine should now appear in your game. Now we will add functionality to it. We want our vending machine to become highlighted and clickable when we are overlapped with it.</p><p>First let’s create the overlap. At the bottom of the update method, we add an overlap to our player and the vending machine, and we add a callback function to our third argument of the overlap:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/552/1*HDNN9KHt5ZTQFGGlk4y32g.png" /></figure><p>Now we will create the callback function as a method on our MainScene class.</p><p>Add the method to our class:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/589/1*yraHcZT4goMMzU_dDoAFSQ.png" /><figcaption>Callback functions used in the third argument of overlaps always take two arguments that are the first two game entities passed to the overlap when it’s created. If a group is one of the entities, the sprite always comes first in the arguments.</figcaption></figure><p>Now if you run your player over the vending machine in your browser it will highlight green. Notice that we aren’t emitting the highlight so it only shows on the client side. But oh no! It doesn’t un-tint when we walk away. Let’s fix that!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/582/1*EtKz9UTp2837LgR_0Qm19g.png" /></figure><p>Inside of the if(this.astronaut) statement for our overlap, add another function that checks if it’s overlapped. Notice that this is not a method built into phaser, we are going to build it ourselves!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/590/1*ARIAYR2-cRGpC1HPyLtU0g.png" /></figure><p>Here we are checking to see if the bounds of the player and vending machine overlap, and if they aren’t overlapping, we call an additional method called deactivateControlPanel, that clears tint and disables interactivity.</p><p><strong>Add A Pop Up Scene for Tasks</strong></p><p>Now that we have a clickable vending machine, we can setup logic to launch another scene that will handle the task when the vending machine is clicked.</p><p>In the scenes folder, add a scene called TaskScene.js.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/809/1*Avk6eBRJQYa-19N8xM_PJw.png" /></figure><p>We load a simple image to show the scene when we eventually launch it. Also notice the init() method on the class, this will be important in our next step as we pass our state to our new TaskScene when it’s launched from MainScene.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/566/1*xz7q23QnMkB5m2rQshvBTw.png" /><figcaption>We pause physics here in MainScene so our player can’t move while we are in TaskScene</figcaption></figure><p>Notice that when we start TaskScene, we pass a second argument with an object holding the information that we want to pass to TaskScene. We then add these to the TaskScene class inside its init method.</p><p>Before we launch, we also need to add our TaskScene to our game instance:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/593/1*sWNg63dn8ERLd4qxEqz45g.png" /></figure><p>You should now be able to walk up to a vending machine, click on it, and launch the TaskScene.</p><p>This concludes part 3 of this tutorial. You now have the tools to create an Among Us inspired, live-multiplayer game with private game rooms and task functionality using Phaser 3 and Socket.io.</p><p>Try:</p><ul><li>Creating a return button to exit the TaskScene</li><li>Creating logic and graphics for a task in the TaskScene</li><li>Adding a start button the uses sockets to trigger the game starting for all players and events that need to happen in the beginning of the game</li><li>Adding a progress tracker with a scoring system</li><li>Adding a database for tasks and global scores</li></ul><p>And I’ll leave the rest up to you! I hope you enjoyed this tutorial and I would love to see how you choose to expand on this game.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=222a618694f3" width="1" height="1" alt=""><hr><p><a href="https://medium.com/hannahrobot/build-an-among-us-inspired-live-multiplayer-game-with-phaser-3-and-socket-io-part-3-222a618694f3">Build An Among Us Inspired Live-Multiplayer Game with Phaser 3 and Socket.io: Part 3</a> was originally published in <a href="https://medium.com/hannahrobot">hannahrobot</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Build An Among Us Inspired Live-Multiplayer Game with Phaser 3 and Socket.io: Part 2]]></title>
            <link>https://medium.com/hannahrobot/build-an-among-us-inspired-live-multiplayer-game-with-phaser-3-and-socket-io-part-2-f1ae7b0efb7b?source=rss-73f184629116------2</link>
            <guid isPermaLink="false">https://medium.com/p/f1ae7b0efb7b</guid>
            <category><![CDATA[socketio]]></category>
            <category><![CDATA[phaser-3]]></category>
            <category><![CDATA[game-development]]></category>
            <category><![CDATA[tutorial]]></category>
            <category><![CDATA[multiplayer-game]]></category>
            <dc:creator><![CDATA[Hannah Robot]]></dc:creator>
            <pubDate>Wed, 16 Dec 2020 20:57:23 GMT</pubDate>
            <atom:updated>2020-12-16T20:57:23.158Z</atom:updated>
            <content:encoded><![CDATA[<p><em>This is part 2 of a 3 part series. To view part 1, </em><a href="https://h4nn4hrobot.wordpress.com/2020/12/06/build-among-us-live-multiplayer-phaser-3-socket/"><em>click here</em></a><em>. If you get lost at any time during this tutorial, you can always refer to the solution code </em><a href="https://github.com/hannahrobot/amongus-tutorial"><em>here</em></a><em>.</em></p><p>In part 1, we completed our boilerplate including an express server, a Phaser 3 front end, and hooked up our socket.io. In part 2, we will create scenes, add entities, and introduce logic for sockets and multiple private game rooms.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*uf-EGYXbUNm7hur-22LbJA.png" /></figure><p><strong>Create the MainScene:</strong></p><p>Inside of your <em>src</em> folder, create another folder called <em>scenes</em>. Inside of <em>scenes</em>, create a file called <em>Mainscene.js</em>. Inside of Mainscene setup your class:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/378/1*Y_DigjTRNRizImfdfZ52TQ.png" /></figure><p>Hop over to <em>src/index.js</em> and lets import our Mainscene in, add it, and start it. We add all of our scenes into this<em> index.js</em>, but will only be starting Mainscene from it as it’s the first scene we want to load when our game begins.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/398/1*ynsLyXe3Hyi18q8ncStWNg.png" /></figure><p>Back in Mainscene, let’s start filling it out with some code. We will be dancing between Mainscene and our <em>socket/index.js</em> so I’d recommend having them both open.</p><p>Inside of the preload method, load the assets that you wish to use for your players and the background image. Inside of create, we safe the context of ‘this’ to the variable scene, and we add the image for the background. Lastly, we create our socket on the Mainscene class. Be sure make a folder called <em>assets </em>where you will add the images for your background and players.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/522/1*bsBAIbf8oOT1b892J7JKaQ.png" /></figure><p>If you <em>npm run start-dev</em> you should now see the background we added, and in your terminal you should see “A socket connection to the server has been made: (socket number)”.</p><p><strong>Create Private Game Rooms:</strong></p><p>Moving to our <em>socket/index.js</em> lets start writing what we want to happen when we connect. Because this is a live-multiplayer game, we need to have state that comes from the server and unifies game data between clients. At the top of this <em>index.js</em> add the following gameRooms object:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/289/1*WXaJSNq0dp972nxIKoAriQ.png" /><figcaption>The commented out code is for your own reference to what will eventually be added to this object from another part of our code.</figcaption></figure><p>Since this is an Among us inspired game, we want players to be able to join private game rooms with their friends using a game code. Let’s create a waiting room where they can retrieve and enter a game code. Inside of <em>src/scenes</em> create a new scene file called WaitingRoom.js. Inside of Mainscene, we will launch this WaitingRoom scene from the create method, so that it appears right when we begin our game. The reason we do this instead of starting it on the game instance is because we can pass it our socket so that we don’t create a new one on WaitingRoom (creating a new socket io would tell the server we have another player instead of connect the same one), and also so that we can show the Mainscene image running in the background.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/593/1*AoDCPa0IFFyhG2PGOC_X3A.png" /></figure><p>Since we passed our socket to waiting room, inside <em>src/scenes/WaitingRoom.js </em>we must initialize it with phasers init() method.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/540/1*Hmaa8D4lZx7yjESDyWilsA.png" /></figure><p>Add the following graphics to WaitingRoom:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/581/1*_XZPa9-XIVxwHelnqoCPPw.png" /></figure><p>The codeform that we preload is available in the solution code’s assets/text <a href="https://github.com/hannahrobot/amongus-tutorial">here</a>. It is a simple html document that includes an input field which we can extract input from, from inside our input element. Add the input field:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/569/1*2P9zILET98TConnTT5ZkJg.png" /></figure><p>Here we are putting a click event listener on our entire codeform, and inside of the click function, we are checking to see if what was clicked was our submit button, named “enterRoom” in our codeform html. Then, we save the input that the user has entered to a variable called input. We “emit” the input.value(the text the user entered) from our socket, with the identifier ‘isKeyValid’. Currently, we don’t have a listener for this emit method, so let’s add one to our <em>server/socket/index.js</em>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/550/1*NtWvftaMqTlK_6cdElx3WQ.png" /></figure><p>When we say Socket.on, and pass it the “isKeyValid” string, we are listening for an event when that specific key is emitted, when our socket.on hears it, it has a call back function that takes the input that we passed this emit inside out waiting room. then, we check our state object keys to see if this game exists. If it exists, we create a new socket.emit called keyIsValid, if it doesn’t exist, we create a new socket.emit called keyNotValid. Back in our WaitingRoom, we need to listen for this emits now. Noticing a pattern?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/498/1*T3Qj2KEaAX2WD93suv-qtw.png" /><figcaption>This code is added to the bottom of the create method on WaitingRoom scene.</figcaption></figure><p>Notice on the fourth line we have a new socket.emit called ‘joinRoom’. You can ignore this for now, we will return to it in a few steps. Let’s add functionality to our request button, and create the notValidText that we just referenced, you can put these above the socket listeners we just created.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/511/1*Ncnf-mikV4IydSz3MnOMhA.png" /></figure><p>Notice the socket.emit ‘getRoomCode’, lets create a listener on our <em>server/socket/index.js</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/565/1*QWkptJfY8OqyaWe8oqgKcA.png" /></figure><p>Inside our listener, we create a variable that stores a random code generated by the function we add below our sockets called codeGenerator. After creating this variable, we check to see if this code already exists as a room on our state object, if it does we generate a new code and save it to the variable, if not we keep the one we have. We then tell our gameRooms state object to create a new key that is our game room code and make the value an object that contains all the data we will need for out game. Lastly, we emit a new socket saying the room is created, with the roomKey attached to it. Now we must add a listener for this back in our WaitingRoom.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/517/1*0voRHi3CY-2mWBhnE6rayA.png" /></figure><p>When we hear ‘roomCreated’, we save the roomKey to our local state (on the WaitingRoom scene) and set the roomKeyText on our interface to the room key we get from the callback function.</p><p>The user flow here is that a user can generate a game room key, enter it into the input field, and submit it to enter the game. Now we must add the logic for joining the game.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/513/1*ybm71ba7yYmUhr-kPq_hig.png" /></figure><p>Following our ‘joinRoom’ socket emit, that includes the room key in the emit, let’s create a listener back in our <em>server/socket/index.js</em>. At the top of our io.on function, but below our socket connection console.log, include our ‘joinRoom’ listener. Inside of the listener, we use the socket.join method and pass it the room key, which will add us to the room!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/543/1*-23cTuTJMkLinKDgzs0nLg.png" /></figure><p>Now when you <em>npm run start-dev</em> you should be able to generate a room key, submit it, and enter into Mainscene.</p><p>This concludes part 2 of this tutorial. Continue to part three <a href="https://h4nn4hrobot.wordpress.com/2020/12/06/build-among-us-live-multiplayer-phaser-3-socket-part-3/">here</a>, where we will create our players and add our game functionality.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f1ae7b0efb7b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/hannahrobot/build-an-among-us-inspired-live-multiplayer-game-with-phaser-3-and-socket-io-part-2-f1ae7b0efb7b">Build An Among Us Inspired Live-Multiplayer Game with Phaser 3 and Socket.io: Part 2</a> was originally published in <a href="https://medium.com/hannahrobot">hannahrobot</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Build An Among Us Inspired Live-Multiplayer Game with Phaser 3 and Socket.io: Part 1]]></title>
            <link>https://medium.com/hannahrobot/build-an-among-us-inspired-live-multiplayer-game-with-phaser-3-and-socket-io-part-1-71749d70b7b4?source=rss-73f184629116------2</link>
            <guid isPermaLink="false">https://medium.com/p/71749d70b7b4</guid>
            <category><![CDATA[phaser-3]]></category>
            <category><![CDATA[tutorial]]></category>
            <category><![CDATA[game-development]]></category>
            <category><![CDATA[multiplayer-game]]></category>
            <category><![CDATA[socketio]]></category>
            <dc:creator><![CDATA[Hannah Robot]]></dc:creator>
            <pubDate>Wed, 16 Dec 2020 19:47:02 GMT</pubDate>
            <atom:updated>2020-12-16T19:55:50.551Z</atom:updated>
            <content:encoded><![CDATA[<p><em>This is part 1 of a 3 part series original published on my </em><a href="https://hannahrobot.com/2020/12/04/build-among-us-live-multiplayer-phaser-3-socket/"><em>website</em></a><em>. If you get lost at any time during this tutorial, you can always refer to the solution code </em><a href="https://github.com/hannahrobot/amongus-tutorial"><em>here</em></a>.</p><p>I recently set out on the (long, confusing, and questionably documented) journey of creating a live-multiplayer game using Phaser 3 and Socket.io. Initially, I built a single-player game (check it out <a href="https://cyberpunk-game.herokuapp.com/">here</a>) and naively thought to myself “I’ll simply get the single player set up and apply multiplayer afterwords, no problem.” Wrong, Hannah. This was a problem.</p><p>The reason that building a single player framework and then applying a multiplayer aspect to it later on is a problem — is because it requires lots of refactoring to your code, file structure, and unnecessary duplicate work. When initially set up as a live-multiplayer, you will be faced with engineering problems that are most easily solved before you have massive amounts of entities, scenes and logic to go with it.</p><p>Why is multiplayer complicated? Checkout this <a href="https://www.youtube.com/watch?v=Jns9LOesIgM">video</a> for a conceptual overview of how it works.</p><p>The problems that I’ve encountered in my research have made it a difficult stride to setup my game as a live-multiplayer. One reason being, that there are virtually no tutorials specific to Phaser 3 (and I mean PHASER 3, not Phaser 2) and Socket.io, that run when you smash that <em>npm run start-dev</em> command, or that don’t require a lot of other add on technologies that also don’t work or are confusing to learn — when you’re just trying to get the basics down.</p><p>If you are here, you’ve likely encountered these same problems in your research. Perhaps you’ve stumbled upon this <a href="https://gamedevacademy.org/create-a-basic-multiplayer-game-in-phaser-3-with-socket-io-part-1/">tutorial</a>, which provides a simple overview on how to setup a basic live-multiplayer game with Phaser 3 and Socket.io without additional technologies. Although, if you’ve gone through this tutorial, you’re likely banging your head on your keyboard and yelling while comparing your code to the solution code, as its riddled with bugs. I too have a keyboard imprinted on my forehead, as it took my team of four programmers to unriddle this. If you still feel compelled to complete this tutorial, worry not, I have provided the debugged solution code <a href="https://github.com/hannahrobot/live-multi-player-tutorial-debug">here</a>.</p><p>But how about a much cooler tutorial that doesn’t have a bug infestation? If you are seeking a comprehensive, step by step, live multiplayer tutorial using Phaser 3 and Socket.io from scratch, this is the tutorial for you my friend.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0S0jGDA-gFdM-n1atW0Kqg.png" /></figure><p><strong>Initialize the Repo:</strong></p><p>Go to your terminal and follow your directories to wherever you want your project to belong. In your command line run: “mkdir amongus-tutorial” to create your folder. “cd amongus-tutorial” to enter the folder. Inside your folder, run the commands “git init” to initialize git and “npm init” to initialize your package.json. Go through the prompts after running npm init, you don’t have to put anything in the prompts, just press enter to go all the way through, or you can name your project and add descriptions, your choice.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/583/1*EgBQcdB5phVyjdVidMyCFA.png" /></figure><p><strong>Setup the package.json:</strong></p><p>Inside of your terminal, create the following folders inside your project:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/585/1*VA3DicQmWTsQyMRKpV0Kkg.png" /></figure><p>Open your project directory in the code editor of your choice, I prefer VSCode. Navigate to your package.json in your code editor. For the sake of saving time, copy the following into your package.json, it can be found in the solution code <a href="https://github.com/hannahrobot/amongus-tutorial/blob/main/package.json">here</a>. In your terminal (inside your project directory) run <em>npm install</em>. What the heck are we installing here and which ones actually matter? Heres a run through:</p><ul><li>Scripts: For this tutorial, we will be using the <em>npm run start-dev</em> script to build and start our server.</li><li>Dependencies: We are installing express as our server, Morgan logs middleware (so you can see bugs in your terminal), and Socket.io is how we keep track of who is connected to our server and what they do. The dependencies that we wont worry about in this tutorial are axios, sequelize (they speak to your database), passport (its used for login functionality) and redux (manages state on the frontend). All of these softwares can help you down the road if you want to expand on this project. For the sake of simplicity, we will be hardcoding our database.</li><li>Dev dependencies: we are installing babel to help us build webpack, and nodemon for node.js dev.</li></ul><p><strong>Setup the Server:</strong></p><p>In your <em>server</em> directory, create the file <em>index.js</em> We need to require in the following at the top of this <em>index.js</em>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/374/1*QAK2P94uXxATS2IsxZVa1g.png" /></figure><p>Now we are going to make a function that creates our app. When it runs, it tells our app to use Morgan to log our middleware, we include a body parsing middleware that sits conveniently on express, and compression middle wear. We tell the server where to get our static files, and we provide an error handler at the end incase a client requested page associated with our app does not exist.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/581/1*Vv1YMn8SsJkEAHNAWxM4WQ.png" /></figure><p>Below this function, we must tell our server where our home base is. But where is our home base? We haven’t created one yet, let’s make it quick. Navigate to your public folder and create a file called <em>index.html</em>. Include the following code:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/585/1*xO6t8Q2UeOK4qlLS6voWCw.png" /></figure><p>Back in our <em>server/index.js</em> lets add some code to tell our server to get our <em>index.html</em>. We also include our second error handler, which catches errors on the server side.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/611/1*-eoctOYfUb-wZ3mD07Hfxw.png" /></figure><p>Our last step for setting up our server is to listen to our Port, and boot the app. Add the following code to the bottom of your <em>index.js</em> file</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/737/1*sXN-KILvadp0cTTuEYq0PQ.png" /></figure><p>Lastly, we need to add an <em>index.js</em> file to our src folder, we don’t need to add anything to it yet. And we need to create our <em>webpack.config.js</em> file to tell our app how to bundle.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/174/1*oiwqTnUQIZEREGzsnKkuHg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/634/1*otjH74aFRVq48Ya6UmVQfw.png" /></figure><p>Now smash that <em>npm run start-dev</em> command and take a look at <a href="http://localhost:8080/">http://localhost:8080/</a> !!!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FRRtGXj18IYzBils5j4uGA.png" /></figure><p>Empty black screen? No console errors? Excellent! Our server is running.</p><p>Let’s add some Phaser magic.</p><p><strong>Setup Phaser</strong></p><p>Inside of our <em>src</em> folder (if you haven’t figured it out yet, our <em>src</em> folder is our client side !!) create a folder called <em>config</em> and add a file to it called <em>config.js</em>. Inside of <em>config.js</em> we will be initializing our Phaser game settings with the following code:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/690/1*l6a7cICxm_9qHgaNWiUzTA.png" /></figure><p>Navigate to <em>src/index.js</em> this is where our game instance will live. At the top of your file, import in typings:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/787/1*NROradGdnZiILvrreevigA.png" /></figure><p>Follow this <a href="https://github.com/hannahrobot/amongus-tutorial/blob/main/typings/phaser.d.ts">link</a> to the solution code’s typings file, and include it in your typings folder so that the import we just wrote can access it.</p><p>In <em>src/index.js</em> let’s create our game class and access the config through our super.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/352/1*MmydpV7Deu_eR-D5RmMVhA.png" /></figure><p><strong>Initialize Socket.io</strong></p><p>Our server is up, we have our game initialized, now we need to incorporate socket. Inside of your server folder, create a new folder called <em>socket</em>, and create a file inside of it called <em>index.js</em>. Now that we’ve created where our sockets will live, we can require it into our server. Go to your <em>server/index.js</em> and find the startListening function that we wrote. Include our socket io variable and require our socket folder in at the bottom of startListening func.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/581/1*J1H9nJ27raojryXXZHO8bQ.png" /></figure><p>We’ve taken care of sockets on our server side, but how do they communicate with the client side? Navigate to our <em>index.html </em>and include the socket script above our bundle.js script:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/549/1*HNzqdLTVVmsjrB-hAjMzEg.png" /></figure><p>If you run<em> npm run start-dev</em>, why don’t we see the console.log from our <em>socket/index.js</em> file that says we are connected? It’s because, although we’ve plugged socket into the right places, we haven’t initialized a socket on our game yet. Lets create some scenes and find a place for it!</p><p>This concludes part 1 of our tutorial. <a href="https://h4nn4hrobot.wordpress.com/2020/11/05/build-among-us-live-multiplayer-phaser-3-socket-part-2/">Click here</a> to move onto part 2, where we will create graphics, fill out our scenes, and add logic to our sockets.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=71749d70b7b4" width="1" height="1" alt=""><hr><p><a href="https://medium.com/hannahrobot/build-an-among-us-inspired-live-multiplayer-game-with-phaser-3-and-socket-io-part-1-71749d70b7b4">Build An Among Us Inspired Live-Multiplayer Game with Phaser 3 and Socket.io: Part 1</a> was originally published in <a href="https://medium.com/hannahrobot">hannahrobot</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>