Fixing problem when switching scenes with container

This commit is contained in:
David Négrier 2020-07-29 11:40:05 +02:00
parent dcbe8d35db
commit bc929615d1
4 changed files with 24 additions and 14 deletions

View File

@ -62,6 +62,7 @@ export abstract class Character extends Container {
for (const texture of textures) { for (const texture of textures) {
const sprite = new Sprite(scene, 0, 0, texture, frame); const sprite = new Sprite(scene, 0, 0, texture, frame);
this.add(sprite);
this.getPlayerAnimations(texture).forEach(d => { this.getPlayerAnimations(texture).forEach(d => {
this.scene.anims.create({ this.scene.anims.create({
key: d.key, key: d.key,
@ -70,9 +71,8 @@ export abstract class Character extends Container {
repeat: d.repeat repeat: d.repeat
}); });
}) })
this.add(sprite); // Needed, otherwise, animations are not handled correctly.
this.scene.sys.updateList.add(sprite); this.scene.sys.updateList.add(sprite);
this.scene.sys.displayList.add(sprite);
this.sprites.set(texture, sprite); this.sprites.set(texture, sprite);
} }
@ -212,6 +212,9 @@ export abstract class Character extends Container {
if (this.scene) { if (this.scene) {
this.scene.events.removeListener('postupdate', this.postupdate.bind(this)); 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); super.destroy(fromScene);
this.playerName.destroy(); this.playerName.destroy();
} }

View File

@ -1,3 +1,5 @@
import LoaderPlugin = Phaser.Loader.LoaderPlugin;
export interface BodyResourceDescriptionInterface { export interface BodyResourceDescriptionInterface {
name: string, name: string,
img: string img: string
@ -296,3 +298,15 @@ export const LAYERS: Array<Array<BodyResourceDescriptionInterface>> = [
HATS_RESOURCES, HATS_RESOURCES,
ACCESSORIES_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}
)
}
}
}

View File

@ -28,6 +28,7 @@ import {SimplePeer} from "../../WebRtc/SimplePeer";
import {ReconnectingSceneName} from "../Reconnecting/ReconnectingScene"; import {ReconnectingSceneName} from "../Reconnecting/ReconnectingScene";
import FILE_LOAD_ERROR = Phaser.Loader.Events.FILE_LOAD_ERROR; import FILE_LOAD_ERROR = Phaser.Loader.Events.FILE_LOAD_ERROR;
import {FourOFourSceneName} from "../Reconnecting/FourOFourScene"; import {FourOFourSceneName} from "../Reconnecting/FourOFourScene";
import {LAYERS, loadAllLayers} from "../Entity/body_character";
export enum Textures { 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.load.bitmapFont('main_font', 'resources/fonts/arcade.png', 'resources/fonts/arcade.xml');
this.connectionPromise = Connection.createConnection(gameManager.getPlayerName(), gameManager.getCharacterSelected()).then((connection : Connection) => { this.connectionPromise = Connection.createConnection(gameManager.getPlayerName(), gameManager.getCharacterSelected()).then((connection : Connection) => {

View File

@ -2,7 +2,7 @@ import {EnableCameraSceneName} from "./EnableCameraScene";
import {TextField} from "../Components/TextField"; import {TextField} from "../Components/TextField";
import Image = Phaser.GameObjects.Image; import Image = Phaser.GameObjects.Image;
import Rectangle = Phaser.GameObjects.Rectangle; import Rectangle = Phaser.GameObjects.Rectangle;
import {LAYERS} from "../Entity/body_character"; import {LAYERS, loadAllLayers} from "../Entity/body_character";
import Sprite = Phaser.GameObjects.Sprite; import Sprite = Phaser.GameObjects.Sprite;
import Container = Phaser.GameObjects.Container; import Container = Phaser.GameObjects.Container;
import {gameManager} from "../Game/GameManager"; 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'); this.load.bitmapFont(CustomizeTextures.mainFont, 'resources/fonts/arcade.png', 'resources/fonts/arcade.xml');
//load all the png files //load all the png files
for (let j = 0; j < LAYERS.length; j++) { loadAllLayers(this.load);
for (let i = 0; i < LAYERS[j].length; i++) {
this.load.spritesheet(
LAYERS[j][i].name,
LAYERS[j][i].img,
{frameWidth: 32, frameHeight: 32}
)
}
}
} }
create() { create() {
@ -105,14 +97,12 @@ export class CustomizeScene extends Phaser.Scene {
const layers: string[] = []; const layers: string[] = [];
let i = 0; let i = 0;
for (const layerItem of this.selectedLayers) { for (const layerItem of this.selectedLayers) {
console.log(i, layerItem, LAYERS);
if (layerItem !== undefined) { if (layerItem !== undefined) {
layers.push(LAYERS[i][layerItem].name); layers.push(LAYERS[i][layerItem].name);
} }
i++; i++;
} }
console.log(layers);
gameManager.setCharacterLayers(layers); gameManager.setCharacterLayers(layers);
return this.scene.start(EnableCameraSceneName); return this.scene.start(EnableCameraSceneName);