Skip to content

Commit 82c1ed9

Browse files
committed
tutorial update
I moved around part of the tutorial. that way if people are trying what is being said in that section they see an effect. since they added the update function beforehand. Otherwise people could wonder ( like myself ) why removing the immovable of the ground has no effect. of course people could realize this after reading the entire paragraph but when you code live with the tutorial this error can occur. and we should make it as easy as possible for people.
1 parent dd39f9a commit 82c1ed9

1 file changed

Lines changed: 27 additions & 26 deletions

File tree

resources/tutorials/02 Making your first game/tutorial.html

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ <h3>Requirements</h3>
1919
If you've gone through the Getting Started Guide you will have downloaded Phaser and got everything set-up and ready to code. Download the resources for this tutorial from here and unzip it into your web root.
2020

2121
Open the part1.html page in your editor of choice and let's have a closer look at the code. After a little boilerplate HTML that includes Phaser the code structure looks like this:
22-
22+
2323
<pre class="lang:js decode:true " >
2424
var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update });
2525

@@ -31,7 +31,7 @@ <h3>Requirements</h3>
3131

3232
function update() {
3333
}
34-
</pre>
34+
</pre>
3535

3636
Line 1 is where you bring Phaser to life by creating an instance of a Phaser.Game object and assigning it to a local variable called 'game'. Calling it 'game' is a common practice, but not a requirement, and this is what you will find in the Phaser examples.
3737

@@ -44,7 +44,7 @@ <h3>Load Assets</h3>
4444
Let's load the assets we need for our game. You do this by putting calls to game.load inside of a function called preload. Phaser will automatically look for this function when it starts and load anything defined within it.
4545

4646
Currently the preload function is empty. Change it to:
47-
47+
4848
<pre class="lang:js decode:true " >function preload() {
4949

5050
game.load.image('sky', 'assets/sky.png');
@@ -53,7 +53,7 @@ <h3>Load Assets</h3>
5353
game.load.spritesheet('dude', 'assets/dude.png', 32, 48);
5454

5555
}
56-
</pre>
56+
</pre>
5757

5858
This will load in 4 assets: 3 images and a sprite sheet. It may appear obvious to some of you, but I would like to point out the first parameter, also known as the asset key. This string is a link to the loaded asset and is what you'll use in your code when creating sprites. You're free to use any valid JavaScript string as the key.
5959

@@ -69,7 +69,7 @@ <h3>Create a Sprite</h3>
6969

7070
The order in which items are rendered in the display matches the order in which you create them. So if you wish to place a background behind the star sprite you would need to ensure that it was added as a sprite first, before the star.
7171

72-
<h3>World Building</h3>
72+
<h3>World Building</h3>
7373

7474
Under the hood game.add.sprite is creating a new <a href="http://docs.phaser.io/Phaser.Sprite.html">Phaser.Sprite</a> object and adding the sprite to the “game world”. This world is where all your objects live, it can be compared to the Stage in Actionscript3.
7575

@@ -78,7 +78,7 @@ <h3>World Building</h3>
7878
The world class can be accessed via game.world and comes with a lot of handy methods and properties to help you distribute your objects inside the world. It includes some simple properties like game.world.height, but also some more advanced ones that we will use in another tutorial.
7979

8080
For now let's build up the scene by adding a background and platforms. Here is the updated create function:
81-
81+
8282
<pre class="lang:js decode:true " >var platforms;
8383

8484
function create() {
@@ -112,17 +112,17 @@ <h3>World Building</h3>
112112
ledge = platforms.create(-150, 250, 'ground');
113113

114114
ledge.body.immovable = true;
115-
115+
116116
}
117-
</pre>
117+
</pre>
118118

119119
If you run this, which you'll find as part4.html in the tutorial zip file, you should see a much more game-like scene:
120120

121121
<img src="http://www.photonstorm.com/wp-content/uploads/2013/12/part4.png" alt="part4" width="800" height="600" class="alignnone size-full wp-image-13606" />
122122

123123
The first part is the same as the star sprite we had before, only instead we changed the key to 'sky' and it has displayed our sky background instead. This is an 800x600 PNG that fills the game screen.
124124

125-
<h3>Groups</h3>
125+
<h3>Groups</h3>
126126

127127
Groups are really powerful. As their name implies they allow you to group together similar objects and control them all as one single unit. You can also check for collision between Groups, and for this game we'll be using two different Groups, one of which is created in the code above for the platforms.
128128

@@ -135,7 +135,7 @@ <h3>Groups</h3>
135135
<h3>Ready Player One</h3>
136136

137137
Create a new local variable called 'player' and add the following code to the create function. You can see this in part5.html:
138-
138+
139139
<pre class="lang:js decode:true " > // The player and its settings
140140
player = game.add.sprite(32, game.world.height - 150, 'dude');
141141

@@ -150,7 +150,7 @@ <h3>Ready Player One</h3>
150150
// Our two animations, walking left and right.
151151
player.animations.add('left', [0, 1, 2, 3], 10, true);
152152
player.animations.add('right', [5, 6, 7, 8], 10, true);
153-
</pre>
153+
</pre>
154154

155155
This creates a new sprite called 'player', positioned at 32 pixels by 150 pixels from the bottom of the game. We're telling it to use the 'dude' asset previously loaded. If you glance back to the preload function you'll see that 'dude' was loaded as a sprite sheet, not an image. That is because it contains animation frames. This is what the full sprite sheet looks like:
156156

@@ -163,24 +163,25 @@ <h3>The Body and Velocity: A world of physics</h3>
163163
Phaser has support for a variety of different physics systems. It ships with Arcade Physics, Ninja Physics and P2.JS Full-Body Physics. For the sake of this tutorial we will be using the Arcade Physics system, which is simple and light-weight, perfect for mobile browsers. You'll notice in the code that we have to start the physics system running, and then for every sprite or Group that we wish to use physics on we enable them.
164164

165165
Once done the sprites gain a new body property, which is an instance of <a href="http://docs.phaser.io/Phaser.Physics.Arcade.Body.html">ArcadePhysics.Body</a>. This represents the sprite as a physical body in Phasers Arcade Physics engine. The body object has itself a lot of properties that we can play with. To simulate the effects of gravity on a sprite, it's as simple as writing this:
166-
166+
167167
<pre class="lang:js decode:true">player.body.gravity.y = 300;</pre>
168168

169169
This is an arbitrary value, but logically, the higher the value, the heavier your object feels and the quicker it falls. If you add this to your code or run part5.html you will see that the player falls down without stopping, completely ignoring the ground we created earlier:
170170

171171
<img src="http://www.photonstorm.com/wp-content/uploads/2013/12/part5.png" alt="part5" width="800" height="600" class="alignnone size-full wp-image-13608" />
172172

173-
The reason for this is that we're not yet testing for collision between the ground and the player. We already told Phaser that our ground and ledges would be immovable. Had we not done that when the player collided with them it would stop for a moment and then everything would have collapsed. This is because unless told otherwise, the ground sprite is a moving physical object (also known as a dynamic body) and when the player hits it, the resulting force of the collision is applied to the ground, therefore, the two bodies exchange their velocities and ground starts falling as well.
174-
173+
The reason for this is that we're not yet testing for collision between the ground and the player. <br>
175174
So to allow the player to collide and take advantage of the physics properties we need to introduce a collision check in the update function:
176-
175+
177176
<pre class="lang:js decode:true " >function update() {
178177

179178
// Collide the player and the stars with the platforms
180179
game.physics.arcade.collide(player, platforms);
181180

182181
}
183-
</pre>
182+
</pre>
183+
We already told Phaser that our ground and ledges would be immovable. Had we not done that when the player collided with them it would stop for a moment and then everything would have collapsed. This is because unless told otherwise, the ground sprite is a moving physical object (also known as a dynamic body) and when the player hits it, the resulting force of the collision is applied to the ground, therefore, the two bodies exchange their velocities and ground starts falling as well.
184+
184185

185186
The update function is called by the core game loop every frame. The <a href="http://docs.phaser.io/Phaser.Physics.Arcade.html#toc22">Physics.collide</a> function is the one that performs the magic. It takes two objects and tests for collision and performs separation against them. In this case we're giving it the player sprite and the platforms Group. It's clever enough to run collision against all Group members, so this one call will collide against the ground and both ledges. The result is a firm platform:
186187

@@ -189,11 +190,11 @@ <h3>The Body and Velocity: A world of physics</h3>
189190
<h3>Controlling the player with the keyboard</h3>
190191

191192
Colliding is all good and well, but we really need the player to move. You would probably think of heading to the documentation and searching about how to add an event listener, but that is not necessary here. Phaser has a built-in Keyboard manager and one of the benefits of using that is this handy little function:
192-
193+
193194
<pre class="lang:js decode:true " >cursors = game.input.keyboard.createCursorKeys();</pre>
194195

195196
This populates the cursors object with four properties: up, down, left, right, that are all instances of <a href="http://docs.phaser.io/Phaser.Key.html">Phaser.Key</a> objects. Then all we need to do is poll these in our update loop:
196-
197+
197198
<pre class="lang:js decode:true " > // Reset the players velocity (movement)
198199
player.body.velocity.x = 0;
199200

@@ -218,13 +219,13 @@ <h3>Controlling the player with the keyboard</h3>
218219

219220
player.frame = 4;
220221
}
221-
222+
222223
// Allow the player to jump if they are touching the ground.
223224
if (cursors.up.isDown &amp;&amp; player.body.touching.down)
224225
{
225226
player.body.velocity.y = -350;
226227
}
227-
</pre>
228+
</pre>
228229

229230
Although we've added a lot of code it should all be pretty readable. The first thing we do is reset the horizontal velocity on the sprite. Then we check to see if the left cursor key is held down. If it is we apply a negative horizontal velocity and start the 'left' running animation. If they are holding down 'right' instead we literally do the opposite. By clearing the velocity and setting it in this manner, every frame, it creates a 'stop-start' style of movement.
230231

@@ -239,7 +240,7 @@ <h3>Jump to it</h3>
239240
<h3>Starshine</h3>
240241

241242
It's time to give our little game a purpose. Let's drop a sprinkling of stars into the scene and allow the player to collect them. To achieve this we'll create a new Group called 'stars' and populate it. In our create function we add the following code (this can be seen in part8.html):
242-
243+
243244
<pre class="lang:js decode:true">
244245
// Finally some stars to collect
245246
stars = game.add.group();
@@ -259,7 +260,7 @@ <h3>Starshine</h3>
259260
// This just gives each star a slightly random bounce value
260261
star.body.bounce.y = 0.7 + Math.random() * 0.2;
261262
}
262-
</pre>
263+
</pre>
263264

264265
The process is similar to when we created the platforms Group. Using a JavaScript 'for' loop we tell it to create 12 stars in our game. They have an x coordinate of i * 70, which means they will be evenly spaced out in the scene 70 pixels apart. As with the player we give them a gravity value so they'll fall down, and a bounce value so they'll bounce a little when they hit the platforms. Bounce is a value between 0 (no bounce at all) and 1 (a full bounce). Ours will bounce somewhere between 0.7 and 0.9. If we were to run the code like this the stars would fall through the bottom of the game. To stop that we need to check for their collision against the platforms in our update loop:
265266

@@ -272,7 +273,7 @@ <h3>Starshine</h3>
272273
This tells Phaser to check for an overlap between the player and any star in the stars Group. If found then pass them to the 'collectStar' function:
273274

274275
<pre class="lang:js decode:true">function collectStar (player, star) {
275-
276+
276277
// Removes the star from the screen
277278
star.kill();
278279

@@ -297,7 +298,7 @@ <h3>Finishing touches</h3>
297298
16x16 is the coordinate to display the text at. 'Score: 0' is the default string to display and the object that follows contains a font size and fill colour. By not specifying which font we'll actually use the browser will default, so on Windows it will be Arial. Next we need to modify the collectStar function so that when the player picks-up a star their score increases and the text is updated to reflect this:
298299

299300
<pre class="lang:js decode:true">function collectStar (player, star) {
300-
301+
301302
// Removes the star from the screen
302303
star.kill();
303304

@@ -314,9 +315,9 @@ <h3>Finishing touches</h3>
314315

315316
<h2>Conclusion</h2>
316317

317-
You have now learnt how to create a sprite with physics properties, to control its motion and to make it interact with other objects in a small game world. There are lots more things you can do to enhance this, for example there is no sense of completion or jeopardy yet. Why not add some spikes you must avoid? You could create a new 'spikes' group and check for collision vs. the player, only instead of killing the spike sprite you kill the player instead. Or for a non-violent style game you could make it a speed-run and simply challenge them to collect the stars as quickly as possible. We've included a few
318+
You have now learnt how to create a sprite with physics properties, to control its motion and to make it interact with other objects in a small game world. There are lots more things you can do to enhance this, for example there is no sense of completion or jeopardy yet. Why not add some spikes you must avoid? You could create a new 'spikes' group and check for collision vs. the player, only instead of killing the spike sprite you kill the player instead. Or for a non-violent style game you could make it a speed-run and simply challenge them to collect the stars as quickly as possible. We've included a few
318319
extra graphics in the zip file to help inspire you.
319320

320321
With the help of what you have learnt in this tutorial and the <a href="http://examples.phaser.io">250+ examples</a> available to you, you should now have a solid foundation for a future project. But as always if you have questions, need advice or want to share what you've been working on then feel free to ask for help in the <a href="http://www.html5gamedevs.com/">Phaser forum</a>.
321-
322+
322323
More tutorials will follow, stay tuned :)

0 commit comments

Comments
 (0)