-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGameScene.js
More file actions
106 lines (82 loc) · 3.97 KB
/
GameScene.js
File metadata and controls
106 lines (82 loc) · 3.97 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
class GameScene extends Phaser.Scene {
constructor() {
super({ key: 'GameScene' });
}
create() {
// Creates a group we'll use to keep track of boxes
gameState.circles = this.add.group();
for (let i = 1; i < 10; i++) {
// Uses the provided helper functions to choose random coordinates for the numbers
let randomCoord = this.assignCoords();
// Creates an circle that is completely transparent
let currentCircle = this.add.circle(randomCoord.x + 10, randomCoord.y + 15, 20, 0x4D39E0, 0)
// Displays the number that the circle will cover
currentCircle.text = this.add.text(randomCoord.x, randomCoord.y, i, { fill: '#4D39E0', fontSize: '30px' })
// Assigns a number property to the circle
currentCircle.number = i
// Adds the circle to our gameState.circles group
gameState.circles.add(currentCircle)
// Allows for the circle to be clicked
currentCircle.setInteractive();
// Add the code for tweens below:
this.tweens.add({
targets: currentCircle,
paused: false,
completeDelay: 3000,
onComplete: function () {
currentCircle.fillAlpha = 1;
gameState.textAlert.setText('');
gameState.score.setText(` Correct: ${gameState.correct}\nIncorrect: ${gameState.incorrect}`);
currentCircle.on('pointerup', () => {
if (gameState.counter === currentCircle.number) {
gameState.counter++;
gameState.correct++;
currentCircle.destroy();
} else {
gameState.incorrect++;
//calling restart() allows the same tween to play multiple times in a scene
currentCircle.wrongTween.restart();
};
gameState.score.setText(` Correct: ${gameState.correct}\nIncorrect: ${gameState.incorrect}`);
})
}
});
//visual feedback for clicking the wrong circle
currentCircle.wrongTween = this.tweens.add({
targets: currentCircle,
paused: true,
scaleX: 1.5,
scaleY: 1.5,
yoyo: true,
duration: 150
});
}
// Adds in the starting text for GameScene
this.add.rectangle(225, 550, 450, 100, 0xFFFFFF, 0.2)
gameState.textAlert = this.add.text(65, 520, 'Remember the location \n of the numbers!', { fill: '#4D39E0', fontSize: '25px' })
gameState.score = this.add.text(100, 520, "", { fill: '#4D39E0', fontSize: '25px' })
}
update() {
if (gameState.circles.getChildren().length === 0) {
// Add logic to transition from GameScene to EndScene
this.scene.stop('GameScene');
this.scene.start('EndScene');
}
}
// Helper function to return an object containing evenly spaced x and y coordinates:
generateRandomCoords() {
const randomX = Math.floor(Math.random() * 5) * 90 + 25;
const randomY = Math.floor(Math.random() * 5) * 100 + 25;
return { x: randomX, y: randomY };
}
// Helper function that returns one set of coordinates not in gameState.numCoordinates
assignCoords() {
let assignedCoord = this.generateRandomCoords();
// If the coordinates is already in gameState.numCoordinates, then other set of coordinates are generated until there is one not in use
while (gameState.numCoordinates[`x${assignedCoord.x}y${assignedCoord.y}`]) {
assignedCoord = this.generateRandomCoords()
}
gameState.numCoordinates[`x${assignedCoord.x}y${assignedCoord.y}`] = true
return assignedCoord
}
}