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) {
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();
}

View file

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