Skip to content

Commit b4d6a68

Browse files
authored
Merge pull request #1335 from melonjs/fix/loading-screen-race
Fix loading screen logo persisting after preload completes
2 parents f340ba9 + 3f0be6f commit b4d6a68

File tree

6 files changed

+42
-8
lines changed

6 files changed

+42
-8
lines changed

packages/examples/src/examples/spine/ExampleSpine.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ const CharacterSelector = () => {
9292
<div
9393
style={{
9494
position: "absolute",
95-
top: 150,
95+
top: 200,
9696
left: 16,
9797
zIndex: 1000,
9898
}}

packages/melonjs/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## [18.2.1] (melonJS 2)
4+
5+
### Fixed
6+
- Loader: fix race condition where the default loading screen logo sprite could persist after preloading completes, caused by the async logo image loading finishing after the state transition or `game.world.reset()`
7+
38
## [18.2.0] (melonJS 2)
49

510
### Added

packages/melonjs/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "melonjs",
3-
"version": "18.2.0",
3+
"version": "18.2.1",
44
"description": "melonJS Game Engine",
55
"homepage": "http://www.melonjs.org/",
66
"type": "module",

packages/melonjs/src/loader/loadingscreen.js

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import Sprite from "./../renderable/sprite.js";
44
import Stage from "./../state/stage.js";
55
import {
66
eventEmitter,
7+
LOADER_COMPLETE,
78
LOADER_PROGRESS,
89
VIEWPORT_ONRESIZE,
910
} from "../system/event.ts";
@@ -94,6 +95,11 @@ class DefaultLoadingScreen extends Stage {
9495
*/
9596
logoSprite = null;
9697

98+
/**
99+
* @ignore
100+
*/
101+
#boundCleanup = null;
102+
97103
/**
98104
* call when the loader is resetted
99105
* @ignore
@@ -113,8 +119,17 @@ class DefaultLoadingScreen extends Stage {
113119
);
114120
game.world.addChild(this.progressBar, 1);
115121

122+
// clean up loading screen children when the preloader completes,
123+
// whether or not a state.change() follows
124+
this.#boundCleanup = this.#cleanup.bind(this);
125+
eventEmitter.addListenerOnce(LOADER_COMPLETE, this.#boundCleanup);
126+
116127
// load the melonJS logo
117128
load({ name: "melonjs_logo", type: "image", src: logo_url }, () => {
129+
// guard against the logo loading after preload completed
130+
if (this.#boundCleanup === null) {
131+
return;
132+
}
118133
// melonJS logo
119134
this.logoSprite = new Sprite(renderer.width / 2, renderer.height / 2, {
120135
image: "melonjs_logo",
@@ -126,11 +141,12 @@ class DefaultLoadingScreen extends Stage {
126141
}
127142

128143
/**
129-
* Called by engine before deleting the object
144+
* Remove loading screen children and unload the logo
130145
* @ignore
131146
*/
132-
onDestroyEvent() {
133-
// remove children added during loading
147+
#cleanup() {
148+
this.#boundCleanup = null;
149+
134150
if (this.progressBar) {
135151
game.world.removeChild(this.progressBar);
136152
this.progressBar = null;
@@ -143,6 +159,19 @@ class DefaultLoadingScreen extends Stage {
143159
// unload the logo image
144160
unload({ name: "melonjs_logo", type: "image" });
145161
}
162+
163+
/**
164+
* Called by engine before deleting the object
165+
* @ignore
166+
*/
167+
onDestroyEvent() {
168+
// remove the listener in case state.change() is called
169+
// before the preloader fires LOADER_COMPLETE
170+
if (this.#boundCleanup) {
171+
eventEmitter.removeListener(LOADER_COMPLETE, this.#boundCleanup);
172+
}
173+
this.#cleanup();
174+
}
146175
}
147176

148177
export default DefaultLoadingScreen;

packages/spine-plugin/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ A [Spine](http://en.esotericsoftware.com/spine-in-depth) 4.2 runtime integration
3030
## Installation
3131
-------------------------------------------------------------------------------
3232
This plugin is already bundled with the required Spine [4.x runtime](package.json#dependencies), so there is no need to install it separately.
33-
>Note: this plugin requires melonJS version 18.2.0 or higher.
33+
>Note: this plugin requires melonJS version 18.2.1 or higher.
3434
3535
To install the plugin using npm:
3636

@@ -119,7 +119,7 @@ me.loader.preload(DataManifest, async function() {
119119
120120
| @melonjs/spine-plugin | melonJS | spine-runtime |
121121
|---|---|---|
122-
| v2.0.x | v18.2.x (or higher) | v4.2.x |
122+
| v2.0.x | v18.2.1 (or higher) | v4.2.x |
123123
| v1.5.x | v15.12.x — v18.0.x | v4.1, v4.2-beta |
124124
125125
## Questions, need help ?

packages/spine-plugin/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
"CHANGELOG.md"
4848
],
4949
"peerDependencies": {
50-
"melonjs": ">=18.2.0"
50+
"melonjs": ">=18.2.1"
5151
},
5252
"dependencies": {
5353
"@esotericsoftware/spine-canvas": "^4.2.108",

0 commit comments

Comments
 (0)