1
1
import Phaser from 'phaser' ;
2
2
import { type Ref , render } from 'phaser-jsx' ;
3
3
4
- import { Title , Win } from '../components' ;
4
+ import { Lose , Title , Win } from '../components' ;
5
5
import { Audio , Image , Scene } from '../constants' ;
6
6
import { createCard } from '../utils/createCard' ;
7
7
@@ -177,8 +177,8 @@ export class Main extends Phaser.Scene {
177
177
}
178
178
179
179
startGame ( ) {
180
- // WinnerText and GameOverText
181
180
let winnerTextRef : Ref < Phaser . GameObjects . Text > ;
181
+
182
182
render (
183
183
< Win
184
184
onClick = { this . restartGame . bind ( this ) }
@@ -187,23 +187,15 @@ export class Main extends Phaser.Scene {
187
187
this ,
188
188
) ;
189
189
190
- const gameOverText = this . add
191
- . text (
192
- this . sys . game . scale . width / 2 ,
193
- - 1000 ,
194
- 'GAME OVER\nClick to restart' ,
195
- {
196
- align : 'center' ,
197
- strokeThickness : 4 ,
198
- fontSize : 40 ,
199
- fontStyle : 'bold' ,
200
- color : '#ff0000' ,
201
- } ,
202
- )
203
- . setName ( 'gameOverText' )
204
- . setDepth ( 3 )
205
- . setOrigin ( 0.5 )
206
- . setInteractive ( ) ;
190
+ let gameOverTextRef : Ref < Phaser . GameObjects . Text > ;
191
+
192
+ render (
193
+ < Lose
194
+ onClick = { this . restartGame . bind ( this ) }
195
+ ref = { ( ref ) => ( gameOverTextRef = ref ) }
196
+ /> ,
197
+ this ,
198
+ ) ;
207
199
208
200
// Start lifes images
209
201
const hearts = this . createHearts ( ) ;
@@ -224,9 +216,10 @@ export class Main extends Phaser.Scene {
224
216
Phaser . Input . Events . POINTER_MOVE ,
225
217
( pointer : Phaser . Input . Pointer ) => {
226
218
if ( this . canMove ) {
227
- const card = this . cards . find ( ( card ) =>
228
- card . gameObject . hasFaceAt ( pointer . x , pointer . y ) ,
229
- ) ;
219
+ const card = this . cards . find ( ( card ) => {
220
+ card . gameObject . hasFaceAt ( pointer . x , pointer . y ) ;
221
+ } ) ;
222
+
230
223
if ( card ) {
231
224
this . input . setDefaultCursor ( 'pointer' ) ;
232
225
} else {
@@ -303,7 +296,7 @@ export class Main extends Phaser.Scene {
303
296
// Show Game Over text
304
297
this . sound . play ( Audio . Whoosh , { volume : 1.3 } ) ;
305
298
this . add . tween ( {
306
- targets : gameOverText ,
299
+ targets : gameOverTextRef . current ,
307
300
ease : Phaser . Math . Easing . Bounce . Out ,
308
301
y : this . sys . game . scale . height / 2 ,
309
302
} ) ;
@@ -339,26 +332,5 @@ export class Main extends Phaser.Scene {
339
332
}
340
333
} ,
341
334
) ;
342
-
343
- gameOverText . on ( Phaser . Input . Events . POINTER_OVER , ( ) => {
344
- gameOverText . setColor ( '#FF7F50' ) ;
345
- this . input . setDefaultCursor ( 'pointer' ) ;
346
- } ) ;
347
-
348
- gameOverText . on ( Phaser . Input . Events . POINTER_OUT , ( ) => {
349
- gameOverText . setColor ( '#8c7ae6' ) ;
350
- this . input . setDefaultCursor ( 'default' ) ;
351
- } ) ;
352
-
353
- gameOverText . on ( Phaser . Input . Events . POINTER_DOWN , ( ) => {
354
- this . add . tween ( {
355
- targets : gameOverText ,
356
- ease : Phaser . Math . Easing . Bounce . InOut ,
357
- y : - 1000 ,
358
- onComplete : ( ) => {
359
- this . restartGame ( ) ;
360
- } ,
361
- } ) ;
362
- } ) ;
363
335
}
364
336
}
0 commit comments