Skip to content

Commit a5e4b71

Browse files
committed
Fix syntax errors in the example code and several parts I forgot to fix
1 parent 4ef696f commit a5e4b71

File tree

11 files changed

+186
-105
lines changed

11 files changed

+186
-105
lines changed

files/en-us/games/tutorials/2d_breakout_game_phaser/animations_and_tweens/index.md

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,26 @@ To add an animation to the object we use the `anims.create` method, which receiv
5959
In the `physics.collide` method call that handles the collision between the ball and the paddle (the first line inside `update`, see below), we can add an extra parameter that specifies a function to be executed every time the collision happens, in the same fashion as the `hitBrick` method. Update the first line inside `update()` as shown below:
6060

6161
```js
62-
update () {
63-
this.physics.collide(this.ball, this.paddle, this.hitPaddle.bind(this));
64-
this.physics.collide(this.ball, this.bricks, this.hitBrick.bind(this));
65-
this.paddle.x = this.input.x || this.scale.width * 0.5;
62+
class Example extends Phaser.Scene {
63+
// ...
64+
update() {
65+
this.physics.collide(this.ball, this.paddle, this.hitPaddle.bind(this));
66+
this.physics.collide(this.ball, this.bricks, this.hitBrick.bind(this));
67+
this.paddle.x = this.input.x || this.scale.width * 0.5;
68+
}
69+
// ...
6670
}
6771
```
6872

6973
Then, we can create the `hitPaddle` method (having `ball` and `paddle` as default parameters), playing the wobble animation when it is called. Add the following method just before your closing brace `}` of the `Example` class:
7074

7175
```js
72-
hitPaddle (ball, paddle) {
73-
this.ball.anims.play('wobble');
76+
class Example extends Phaser.Scene {
77+
// ...
78+
hitPaddle(ball, paddle) {
79+
this.ball.anims.play("wobble");
80+
}
81+
// ...
7482
}
7583
```
7684

files/en-us/games/tutorials/2d_breakout_game_phaser/build_the_brick_field/index.md

Lines changed: 63 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,13 @@ The `bricks` property will be used to create a group of bricks, which will make
3030
Next, let's load the image of the brick — add the following `load.image()` call just below the others:
3131

3232
```js
33-
preload () {
33+
class Example extends Phaser.Scene {
34+
// ...
35+
preload() {
36+
// ...
37+
this.load.image("brick", "img/brick.png");
38+
}
3439
// ...
35-
this.load.image('brick', 'img/brick.png');
3640
}
3741
```
3842

@@ -43,29 +47,37 @@ You also need to [grab the brick image from GitHub](https://github.com/igrep/2D_
4347
We will place all the code for drawing the bricks inside an `initBricks` method to keep it separated from the rest of the code. Add a call to `initBricks` at the end of the `create` method:
4448

4549
```js
46-
create () {
50+
class Example extends Phaser.Scene {
51+
// ...
52+
create() {
53+
// ...
54+
this.initBricks();
55+
}
4756
// ...
48-
this.initBricks();
4957
}
5058
```
5159

5260
Now onto the method itself. Add the `initBricks` method at the end of the `Example` class, just before the closing brace `}`, as shown below. To begin with, we add the `bricksLayout` object, as this will come in handy very soon:
5361

5462
```js
55-
initBricks () {
56-
const bricksLayout = {
57-
width: 50,
58-
height: 20,
59-
count: {
60-
row: 3,
61-
col: 7,
62-
},
63-
offset: {
64-
top: 50,
65-
left: 60,
66-
},
67-
padding: 10,
68-
};
63+
class Example extends Phaser.Scene {
64+
// ...
65+
initBricks() {
66+
const bricksLayout = {
67+
width: 50,
68+
height: 20,
69+
count: {
70+
row: 3,
71+
col: 7,
72+
},
73+
offset: {
74+
top: 50,
75+
left: 60,
76+
},
77+
padding: 10,
78+
};
79+
}
80+
// ...
6981
}
7082
```
7183

@@ -121,35 +133,41 @@ Each `brickX` position is worked out as `bricksLayout.width` plus `bricksLayout.
121133
Here is the complete code for the `initBricks()` function:
122134

123135
```js
124-
initBricks () {
125-
const bricksLayout = {
126-
width: 50,
127-
height: 20,
128-
count: {
129-
row: 3,
130-
col: 7,
131-
},
132-
offset: {
133-
top: 50,
134-
left: 60,
135-
},
136-
padding: 10,
137-
};
138-
139-
this.bricks = this.add.group();
140-
for (let c = 0; c < bricksLayout.count.col; c++) {
141-
for (let r = 0; r < bricksLayout.count.row; r++) {
142-
const brickX =
143-
c * (bricksLayout.width + bricksLayout.padding) + bricksLayout.offset.left;
144-
const brickY =
145-
r * (bricksLayout.height + bricksLayout.padding) + bricksLayout.offset.top;
146-
147-
const newBrick = this.add.sprite(brickX, brickY, "brick");
148-
this.physics.add.existing(newBrick);
149-
newBrick.body.setImmovable(true);
150-
this.bricks.add(newBrick);
136+
class Example extends Phaser.Scene {
137+
// ...
138+
initBricks() {
139+
const bricksLayout = {
140+
width: 50,
141+
height: 20,
142+
count: {
143+
row: 3,
144+
col: 7,
145+
},
146+
offset: {
147+
top: 50,
148+
left: 60,
149+
},
150+
padding: 10,
151+
};
152+
153+
this.bricks = this.add.group();
154+
for (let c = 0; c < bricksLayout.count.col; c++) {
155+
for (let r = 0; r < bricksLayout.count.row; r++) {
156+
const brickX =
157+
c * (bricksLayout.width + bricksLayout.padding) +
158+
bricksLayout.offset.left;
159+
const brickY =
160+
r * (bricksLayout.height + bricksLayout.padding) +
161+
bricksLayout.offset.top;
162+
163+
const newBrick = this.add.sprite(brickX, brickY, "brick");
164+
this.physics.add.existing(newBrick);
165+
newBrick.body.setImmovable(true);
166+
this.bricks.add(newBrick);
167+
}
151168
}
152169
}
170+
// ...
153171
}
154172
```
155173

files/en-us/games/tutorials/2d_breakout_game_phaser/buttons/index.md

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,14 @@ First, we call `setInteractive` on the button to make it respond to pointer even
101101
Now, we need to define the `startGame` method referenced in the code above:
102102

103103
```js
104-
startGame () {
105-
this.startButton.destroy();
106-
this.ball.body.setVelocity(150, -150);
107-
this.playing = true;
104+
class Example extends Phaser.Scene {
105+
// ...
106+
startGame() {
107+
this.startButton.destroy();
108+
this.ball.body.setVelocity(150, -150);
109+
this.playing = true;
110+
}
111+
// ...
108112
}
109113
```
110114

@@ -117,10 +121,14 @@ Finally for this section, go back into your `create` method, find the `this.ball
117121
It works as expected, but we can still move the paddle when the game hasn't started yet, which looks a bit silly. To stop this, we can take advantage of the `playing` variable and make the paddle movable only when the game has started. To do that, adjust the `update` function like so:
118122

119123
```js
120-
update () {
124+
class Example extends Phaser.Scene {
121125
// ...
122-
if (this.playing) {
123-
this.paddle.x = this.input.x || this.scale.width * 0.5;
126+
update() {
127+
// ...
128+
if (this.playing) {
129+
this.paddle.x = this.input.x || this.scale.width * 0.5;
130+
}
131+
// ...
124132
}
125133
// ...
126134
}

files/en-us/games/tutorials/2d_breakout_game_phaser/collision_detection/index.md

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,26 @@ Now onto the next challenge — the collision detection between the ball and the
1616
The physics engine makes everything a lot easier — we just need to add two simple pieces of code. First, add a new line inside your `update` method that detects a collision between the ball and bricks, as shown below:
1717

1818
```js
19-
function update() {
20-
this.physics.collide(this.ball, this.paddle);
21-
this.physics.collide(this.ball, this.bricks, this.hitBrick);
22-
paddle.x = game.input.x || game.world.width * 0.5;
19+
class Example extends Phaser.Scene {
20+
// ...
21+
update() {
22+
this.physics.collide(this.ball, this.paddle);
23+
this.physics.collide(this.ball, this.bricks, this.hitBrick);
24+
paddle.x = game.input.x || game.world.width * 0.5;
25+
}
26+
// ...
2327
}
2428
```
2529

2630
The ball's position is calculated against the positions of all the bricks in the group. The third, optional parameter is the function executed when a collision occurs — `hitBrick`. Create this new function as the last method of the `Example` class, just before the closing brace `}`, as follows:
2731

2832
```js
29-
hitBrick (ball, brick) {
30-
brick.destroy();
33+
class Example extends Phaser.Scene {
34+
// ...
35+
hitBrick(ball, brick) {
36+
brick.destroy();
37+
}
38+
// ...
3139
}
3240
```
3341

files/en-us/games/tutorials/2d_breakout_game_phaser/extra_lives/index.md

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -115,20 +115,28 @@ if (ballIsOutOfBounds) {
115115
We want to decrease the number of lives every time the ball leaves the canvas. Add the `ballLeaveScreen()` method definition at the end of the `Example` class:
116116

117117
```js
118-
ballLeaveScreen () {
119-
this.lives--;
120-
if (this.lives > 0) {
121-
this.livesText.setText(`Lives: ${this.lives}`);
122-
this.lifeLostText.visible = true;
123-
this.ball.body.reset(this.scale.width * 0.5, this.scale.height - 25);
124-
this.input.once("pointerdown", () => {
125-
this.lifeLostText.visible = false;
126-
this.ball.body.setVelocity(150, -150);
127-
}, this);
128-
} else {
129-
alert("Game over!");
130-
location.reload();
118+
class Example extends Phaser.Scene {
119+
// ...
120+
ballLeaveScreen() {
121+
this.lives--;
122+
if (this.lives > 0) {
123+
this.livesText.setText(`Lives: ${this.lives}`);
124+
this.lifeLostText.visible = true;
125+
this.ball.body.reset(this.scale.width * 0.5, this.scale.height - 25);
126+
this.input.once(
127+
"pointerdown",
128+
() => {
129+
this.lifeLostText.visible = false;
130+
this.ball.body.setVelocity(150, -150);
131+
},
132+
this,
133+
);
134+
} else {
135+
alert("Game over!");
136+
location.reload();
137+
}
131138
}
139+
// ...
132140
}
133141
```
134142

files/en-us/games/tutorials/2d_breakout_game_phaser/load_the_assets_and_print_them_on_screen/index.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,12 @@ ball;
2424
Loading images and printing them on our canvas is a lot easier using Phaser than using pure JavaScript. To load the asset, we will use the `Phaser.Scene`'s `load.image` method, available as `this.load.image`. Add the following new line just the `preload()` method:
2525

2626
```js
27-
preload () {
28-
this.load.image('ball', 'img/ball.png');
27+
class Example extends Phaser.Scene {
28+
// ...
29+
preload() {
30+
this.load.image("ball", "img/ball.png");
31+
}
32+
// ...
2933
}
3034
```
3135

@@ -36,8 +40,12 @@ Of course, to load the image, it must be available in our code directory. [Grab
3640
Now, to show it on the screen we will use another `Phaser.Scene`'s method called `add.sprite`; add the following new line inside the `create()` method:
3741

3842
```js
39-
create () {
40-
this.ball = this.add.sprite(50, 50, 'ball');
43+
class Example extends Phaser.Scene {
44+
// ...
45+
create() {
46+
this.ball = this.add.sprite(50, 50, "ball");
47+
}
48+
// ...
4149
}
4250
```
4351

files/en-us/games/tutorials/2d_breakout_game_phaser/physics/index.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,13 @@ this.ball.body.setVelocity(150, 150);
4343
Remember to remove our old method of adding values to `x` and `y` from the `update` method:
4444

4545
```js
46-
update () {
47-
ball.x += 1;
48-
ball.y += 1;
46+
class Example extends Phaser.Scene {
47+
// ...
48+
update() {
49+
ball.x += 1;
50+
ball.y += 1;
51+
}
52+
// ...
4953
}
5054
```
5155

files/en-us/games/tutorials/2d_breakout_game_phaser/player_paddle_and_controls/index.md

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,23 @@ From the framework point of view the paddle is very similar to the ball — we n
2020
First, add the `paddle` property we will be using in our game, right after the `ball` property:
2121

2222
```js
23-
paddle;
23+
class Example extends Phaser.Scene {
24+
ball;
25+
paddle;
26+
// ...
27+
}
2428
```
2529

26-
Then, in the `preload` function, load the `paddle` image by adding the following new `load.image()` call:
30+
Then, in the `preload` method, load the `paddle` image by adding the following new `load.image()` call:
2731

2832
```js
29-
function preload() {
30-
//
31-
this.load.image("ball", "img/ball.png");
32-
this.load.image("paddle", "img/paddle.png");
33+
class Example extends Phaser.Scene {
34+
// ...
35+
preload() {
36+
this.load.image("ball", "img/ball.png");
37+
this.load.image("paddle", "img/paddle.png");
38+
}
39+
// ...
3340
}
3441
```
3542

files/en-us/games/tutorials/2d_breakout_game_phaser/randomizing_gameplay/index.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,13 @@ Our game appears to be completed, but if you look close enough you'll notice tha
1616
We can change the ball's velocity depending on the exact spot it hits the paddle, by modifying the `x` velocity each time the `hitPaddle` method is run using a line along the lines of the below. Add this new line to your code now, and try it out.
1717

1818
```js
19-
hitPaddle (ball, paddle) {
20-
this.ball.anims.play('wobble');
21-
ball.body.velocity.x = -5 * (paddle.x - ball.x);
19+
class Example extends Phaser.Scene {
20+
// ...
21+
hitPaddle (ball, paddle) {
22+
this.ball.anims.play('wobble');
23+
ball.body.velocity.x = -5 * (paddle.x - ball.x);
24+
}
25+
// ...
2226
}
2327
```
2428

files/en-us/games/tutorials/2d_breakout_game_phaser/the_score/index.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,14 @@ The last parameter looks very similar to CSS styling. In our case the score text
5050
We will increase the number of points every time the ball hits a brick and update the `scoreText` to display the current score. This can be done using the `setText` method — add the two new lines seen below to the `hitBrick` method:
5151

5252
```js
53-
hitBrick (ball, brick) {
54-
brick.destroy();
55-
this.score += 10;
56-
this.scoreText.setText(`Points: ${this.score}`);
53+
class Example extends Phaser.Scene {
54+
// ...
55+
hitBrick (ball, brick) {
56+
brick.destroy();
57+
this.score += 10;
58+
this.scoreText.setText(`Points: ${this.score}`);
59+
}
60+
// ...
5761
}
5862
```
5963

0 commit comments

Comments
 (0)