Skip to content

Commit

Permalink
fix: [#1553] Scene onInitialize order (#1554)
Browse files Browse the repository at this point in the history
Closes #1553

## Changes:

- Update the Scene `onInitialize` order
- Add test to confirm order works
eonarheim authored May 23, 2020
1 parent c5d710a commit 02e1a61
Showing 3 changed files with 19 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -32,6 +32,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
- Fixed Loader play button markup and styles are now cleaned up after clicked ([#1431](https://github.com/excaliburjs/Excalibur/issues/1431))
- Fixed Excalibur crashing when embedded within a cross-origin IFrame ([#1151](https://github.com/excaliburjs/Excalibur/issues/1151))
- Fixed issue when loading images from a base64 strings that would crash the loader ([#1543](https://github.com/excaliburjs/Excalibur/issues/1543))
- Fixed Scene initialization order when using the lifecycle overrides ([#1553](https://github.com/excaliburjs/Excalibur/issues/1553))

<!--------------------------------- DO NOT EDIT BELOW THIS LINE --------------------------------->
<!--------------------------------- DO NOT EDIT BELOW THIS LINE --------------------------------->
5 changes: 3 additions & 2 deletions src/engine/Scene.ts
Original file line number Diff line number Diff line change
@@ -228,12 +228,13 @@ export class Scene extends Class implements CanInitialize, CanActivate, CanDeact
this.camera.y = engine.halfDrawHeight;
}

// This order is important! we want to be sure any custom init that add actors
// fire before the actor init
this.onInitialize.call(this, engine);
this._initializeChildren();

this._logger.debug('Scene.onInitialize', this, engine);

this.eventDispatcher.emit('initialize', new InitializeEvent(engine, this));
this.onInitialize.call(this, engine);
this._isInitialized = true;
}
}
15 changes: 15 additions & 0 deletions src/spec/SceneSpec.ts
Original file line number Diff line number Diff line change
@@ -285,6 +285,21 @@ describe('A scene', () => {
expect(initializeCount).toBe(1, 'Scenes can only be initialized once');
});

it('should initialize before actors in the scene', () => {
const actor = new ex.Actor();
scene.add(actor);
let sceneInit = false;
scene.onInitialize = () => {
sceneInit = true;
};
actor.onInitialize = () => {
expect(sceneInit).toBe(true, 'Scene should be initialized first before any actors');
};

engine.goToScene('root');
scene.update(engine, 100);
});

it('should allow adding and removing an Actor in same frame', () => {
let removed = false;
scene.add(actor);

0 comments on commit 02e1a61

Please sign in to comment.