From bc929615d16b2a48eeb880d1f6ecc126917705ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20N=C3=A9grier?= Date: Wed, 29 Jul 2020 11:40:05 +0200 Subject: [PATCH] Fixing problem when switching scenes with container --- front/src/Phaser/Entity/Character.ts | 7 +++++-- front/src/Phaser/Entity/body_character.ts | 14 ++++++++++++++ front/src/Phaser/Game/GameScene.ts | 3 +++ front/src/Phaser/Login/CustomizeScene.ts | 14 ++------------ 4 files changed, 24 insertions(+), 14 deletions(-) diff --git a/front/src/Phaser/Entity/Character.ts b/front/src/Phaser/Entity/Character.ts index c3c4def4..6edb0e36 100644 --- a/front/src/Phaser/Entity/Character.ts +++ b/front/src/Phaser/Entity/Character.ts @@ -62,6 +62,7 @@ export abstract class Character extends Container { for (const texture of textures) { const sprite = new Sprite(scene, 0, 0, texture, frame); + this.add(sprite); this.getPlayerAnimations(texture).forEach(d => { this.scene.anims.create({ key: d.key, @@ -70,9 +71,8 @@ export abstract class Character extends Container { repeat: d.repeat }); }) - this.add(sprite); + // Needed, otherwise, animations are not handled correctly. this.scene.sys.updateList.add(sprite); - this.scene.sys.displayList.add(sprite); this.sprites.set(texture, sprite); } @@ -212,6 +212,9 @@ export abstract class Character extends Container { if (this.scene) { this.scene.events.removeListener('postupdate', this.postupdate.bind(this)); } + for (const sprite of this.sprites.values()) { + this.scene.sys.updateList.remove(sprite); + } super.destroy(fromScene); this.playerName.destroy(); } diff --git a/front/src/Phaser/Entity/body_character.ts b/front/src/Phaser/Entity/body_character.ts index efa82c15..3d9d5a5f 100644 --- a/front/src/Phaser/Entity/body_character.ts +++ b/front/src/Phaser/Entity/body_character.ts @@ -1,3 +1,5 @@ +import LoaderPlugin = Phaser.Loader.LoaderPlugin; + export interface BodyResourceDescriptionInterface { name: string, img: string @@ -296,3 +298,15 @@ export const LAYERS: Array> = [ HATS_RESOURCES, ACCESSORIES_RESOURCES ]; + +export const loadAllLayers = (load: LoaderPlugin) => { + for (let j = 0; j < LAYERS.length; j++) { + for (let i = 0; i < LAYERS[j].length; i++) { + load.spritesheet( + LAYERS[j][i].name, + LAYERS[j][i].img, + {frameWidth: 32, frameHeight: 32} + ) + } + } +} diff --git a/front/src/Phaser/Game/GameScene.ts b/front/src/Phaser/Game/GameScene.ts index 8034bfbe..1a3cd9b7 100644 --- a/front/src/Phaser/Game/GameScene.ts +++ b/front/src/Phaser/Game/GameScene.ts @@ -28,6 +28,7 @@ import {SimplePeer} from "../../WebRtc/SimplePeer"; import {ReconnectingSceneName} from "../Reconnecting/ReconnectingScene"; import FILE_LOAD_ERROR = Phaser.Loader.Events.FILE_LOAD_ERROR; import {FourOFourSceneName} from "../Reconnecting/FourOFourScene"; +import {LAYERS, loadAllLayers} from "../Entity/body_character"; export enum Textures { @@ -158,6 +159,8 @@ export class GameScene extends Phaser.Scene { ); }); + loadAllLayers(this.load); + this.load.bitmapFont('main_font', 'resources/fonts/arcade.png', 'resources/fonts/arcade.xml'); this.connectionPromise = Connection.createConnection(gameManager.getPlayerName(), gameManager.getCharacterSelected()).then((connection : Connection) => { diff --git a/front/src/Phaser/Login/CustomizeScene.ts b/front/src/Phaser/Login/CustomizeScene.ts index 7a1dd12e..a4c4282f 100644 --- a/front/src/Phaser/Login/CustomizeScene.ts +++ b/front/src/Phaser/Login/CustomizeScene.ts @@ -2,7 +2,7 @@ import {EnableCameraSceneName} from "./EnableCameraScene"; import {TextField} from "../Components/TextField"; import Image = Phaser.GameObjects.Image; import Rectangle = Phaser.GameObjects.Rectangle; -import {LAYERS} from "../Entity/body_character"; +import {LAYERS, loadAllLayers} from "../Entity/body_character"; import Sprite = Phaser.GameObjects.Sprite; import Container = Phaser.GameObjects.Container; import {gameManager} from "../Game/GameManager"; @@ -50,15 +50,7 @@ export class CustomizeScene extends Phaser.Scene { this.load.bitmapFont(CustomizeTextures.mainFont, 'resources/fonts/arcade.png', 'resources/fonts/arcade.xml'); //load all the png files - for (let j = 0; j < LAYERS.length; j++) { - for (let i = 0; i < LAYERS[j].length; i++) { - this.load.spritesheet( - LAYERS[j][i].name, - LAYERS[j][i].img, - {frameWidth: 32, frameHeight: 32} - ) - } - } + loadAllLayers(this.load); } create() { @@ -105,14 +97,12 @@ export class CustomizeScene extends Phaser.Scene { const layers: string[] = []; let i = 0; for (const layerItem of this.selectedLayers) { - console.log(i, layerItem, LAYERS); if (layerItem !== undefined) { layers.push(LAYERS[i][layerItem].name); } i++; } - console.log(layers); gameManager.setCharacterLayers(layers); return this.scene.start(EnableCameraSceneName);