We will create metaphor for how we seek out or are drawn to negative emotions. This will be in the form of a spaceship in a kind of asteroid field! Obvi! The user will fly around as an emoji, seeking out the single “thumbs down” in a sea of “thumbs ups”. When they find it, another one will appear somewhere else and the sad saga continues.
activities folder and name it desperately-seeking-sadness (plain-javascript-project.zip)index.html to something appropriate like Desperately Seeking Sadnessindex.htmlWe’ll use this activity chiefly to go through the steps of setting up a fairly standard Phaser 3 project with sprites and physics and basic interactivity. By the end we’ll have something quite extensible, but for the activity we’ll focus on the basics. Thus, we’ll need to set up the base level project itself, add an avatar and make it move with user input, then add our other sprites and interactions.
We can roughly divide this project up into six steps:
We’ve gone over this in the associated Phaser 3 lessons, so really the objective here is to follow the same process discussed in the “setup” discussion to get the fundamentals ready. That is:
script.js to main.js and change the script tag in index.htmlBoot.js file and define the Boot class (extending Phaser.Scene), it should haveconstructor() that calls the super constructor to set the scene key to bootpreload() method that includes a listener for the loading complete event and switches to a scene with the key playcreate() methodupdate() methodPlay.js file and define the Play class (extending Phaser.Scene), it should haveconstructor() that calls the super constructor to set the scene key to bootcreate() methodupdate() methodBoot.js and Play.js to index.htmlmain.js add the basic configuration and game creation code, make sure youtype to Phaser.AUTOBoot and Play classes (in that order) to the scene property’s arrayWith this done we will see the classic blank black rectangle representing our game’s void-like canvas.
It’s time to add our first sprite, the avatar. Let’s represent them as a Neutral Face Emoji.
avatar.png, and move it to the assets/images folder of your projectBoot.js load the image with a key of avatar in the preload() methodPlay.js add a physics sprite to the scene using the avatar key, assign it to this.avatarWe should now see our black rectangle with the neutral face emoji wherever we chose to position it on the canvas.
We want the user to be able to steer the avatar around on the canvas using the arrow keys. They’ll use up for thrust and left and right for rotation. Fortunately there’s a nice Phaser 3 example for this which we can take inspiration and code from! Physics > Arcade > Space.
To adapt the example we will need to:
create(), reuse the line of code for creating the cursor keys object (if we can’t remember how), assigning the result to this.cursorsupdate() reuse the code being used to move the spaceship, replacing ship with this.avatar so it applies to our avatar!All going to plan, you should now be able to steer the emoji around on the canvas! Notice how it initially flies to the right (which is an angle of 0º!). That’s why it was a good idea to rotate the image 90º clockwise! Everyone knows emoji’s fly in the direction of the top of their head.
It would be nice if the emoji didn’t leave our canvas so let’s also
.collideWorldBounds to true on our avatar’s body property in create()There, now it’s stuck in the canvas. Trapped for all eternity with the bad vibes we’re about to introduce!
We’re ready to add the thumbs down sprite the avatar will collect. We’ve done this before, so let’s follow the same basic principles. We’ll use the Thumbs Down Emoji here.
thumbs-down.png, and move it to the assets/images folder of your projectBoot.js load the image with a key of thumbs-down in the preload() methodPlay.js create a physics sprite using thumbs down at a random position0 and this.sys.canvas.width to a new variable x0 and this.sys.canvas.height to a new variable yx and y position and using the thumbs-down key, assign it to this.sadnessNow you should see your avatar where you positioned it and a thumbs down in a random position.
We want the user to be able to collect the thumbs down by overlapping with it. When collected, the thumbs down should appear somewhere else on the canvas.
overlap event listener in create() that checks for an overlap between the avatar and the thumbs down sprites, call the event handler method getSad() (remember to include the extra arguments to include the context of this for the handler)getSad() assign a new random position to the thumbs down sprite (you can reuse the earlier code idea for this)Now if your avatar overlaps the thumbs down sprite, the thumbs down sprite should vanish and a “new one” will appear somewhere else on the canvas. We have our basic search for sadness working!
The cutting-edge critical perspective here is that we want the player to be flying amongst lots of positive emotions as they search out the sadness. The good feelings should be getting in their way. To do this, we’ll have a lot of thumbs up emojis that the player bumps into in their quest. This means we can play around with some more of the physics options in Arcade Physics!
We’ll use the Thumbs Up Emoji here. We’ll also reference the Group versus Group example from Phaser 3.
thumbs-up.png, and move it to the assets/images folder of your projectBoot.js load the image with a key of thumbs-up in the preload() methodPlay.js assign a new a physics group to this.happiness, with the following configuration (check out Group versus Group)key to thumbs-up so the sprites use that imagequantity to something like 50 to have multiple spritescollideWorldBounds to true so they stay on the canvasbounceX and bounceY to a value between 0 and 1 so the thumbs ups bounce!dragX and dragY to something like 50 so the thumbs ups slow down over time!0,0 which is no good but notice how they position the elements in Group versus Group using the Phaser.Actions.RandomInRectangle() helper and copy that idea!)Now you should see many thumbs ups on the canvas! If you steer the avatar into them they bounce off it and each other for some pleasing chaos! The metaphor is complete!
As always, there are probably improvements that can be made. Consider
Phaser.Actions.RandomInRectangle() trick from the happiness group to reposition the sadness sprite if you provide the sadness sprite to it in an array, e.g. Phaser.Actions.RandomInRectangle([this.sadness],this.physics.world.bounds)We can imagine all kinds of improvements and changes!?