-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEndScene.js
66 lines (51 loc) · 2.28 KB
/
EndScene.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
class EndScene extends Phaser.Scene {
constructor() {
super({ key: 'EndScene' })
}
preload() {
this.load.spritesheet('happy', 'https://s3.amazonaws.com/codecademy-content/courses/learn-phaser/Counting/codey_happy.png', { frameWidth: 300, frameHeight: 300 });
this.load.spritesheet('sad', 'https://s3.amazonaws.com/codecademy-content/courses/learn-phaser/Counting/codey_sad.png', { frameWidth: 300, frameHeight: 300 });
}
create() {
// Calculates a score out of 100 points
const score = gameState.correct / (gameState.correct + gameState.incorrect) * 100;
// Converts score to a string and adds the percentage symbol
const percentage = Math.round(score).toString() + "%";
if (score >= 70) {
// Add the happy sprite and animation below:
gameState.win = this.add.sprite(220, 220, 'happy');
this.anims.create({
key: 'celebrate',
frames: this.anims.generateFrameNames('happy', { start: 0, end: 1 }),
delay: 0,
frameRate: 2,
repeat: -1
});
gameState.win.anims.play('celebrate');
} else {
// Add the sad sprite and animation below:
gameState.lose = this.add.sprite(220, 200, 'sad');
this.anims.create({
key: 'cry',
frames: this.anims.generateFrameNames('sad', { start: 0, end: 1 }),
delay: 0,
frameRate: 2,
repeat: -1
});
gameState.lose.anims.play('cry');
};
this.add.rectangle(225, 488, 450, 235, 0xFFFFFF, 0.2)
this.add.text(30, 450, `You counted ${percentage} correctly\n `, { fill: '#4D39E0', fontSize: '25px' })
this.add.text(100, 520, 'Click to play again?', { fill: '#4D39E0', fontSize: '20px' })
this.input.on('pointerup', () => {
// Resets some gameState values needed to replay the game:
gameState.numCoordinates = {}
gameState.counter = 1
gameState.correct = 0
gameState.incorrect = 0
// Add logic to transition from EndScene
this.scene.stop('EndScene');
this.scene.start('GameScene');
})
}
}