Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: [#2379] Deferred goto preserves wrong scene #2380

Merged
merged 4 commits into from
Jul 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
```

### Fixed
- Fixed bug where a deferred `goToScene` would preserve the incorrect scene so `engine.add(someActor)` would place actors in the wrong scene after transitioning to another.
- Fixed usability issue and log warning if the `ex.ImageSource` is not loaded and a draw was attempted.
- Fixed bug in `ex.Physics.useRealisticPhysics()` solver where `ex.Body.bounciness` was not being respected in the simulation
- Fixed bug in `ex.Physics.useRealisticPhysics()` solver where `ex.Body.limitDegreeOfFreedom` was not working all the time.
Expand Down
14 changes: 14 additions & 0 deletions sandbox/tests/gotoscene/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Go To Scene</title>
</head>
<body>
<canvas id="game"></canvas>
<script src="../../lib/excalibur.js"></script>
<script src="./index.js"></script>
</body>
</html>
57 changes: 57 additions & 0 deletions sandbox/tests/gotoscene/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
class Scene1 extends ex.Scene {
onInitialize(_engine: ex.Engine): void {
const actor = new ex.Actor({
x: _engine.halfDrawWidth,
y: _engine.halfDrawHeight,
width: 20,
height: 20,
color: ex.Color.Magenta,
});
this.add(actor);

_engine.input.pointers.primary.on(
"down",
(event: ex.Input.PointerEvent): void => {
_engine.goToScene("scene2");
}
);
}

onActivate(): void {
console.log('Scene 1 Activate')
}
}


class Scene2 extends ex.Scene {
onInitialize(_engine: ex.Engine): void {
// _engine.start();
const actor = new ex.Actor({
pos: ex.Vector.Zero,
width: 1000,
height: 1000,
color: ex.Color.Cyan,
});
_engine.add(actor);
}
onActivate(): void {
console.log('Scene 2 Activate')
}
}


var engine = new ex.Engine({
width: 1920 / 2,
height: 1080 / 2,
canvasElementId: "game",
});

engine.add("scene1", new Scene1());
engine.add("scene2", new Scene2());
engine.goToScene("scene1");

var loader = new ex.Loader();

loader.suppressPlayButton = true;

engine.start(loader);
4 changes: 3 additions & 1 deletion src/engine/Engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1164,7 +1164,9 @@ O|===|* >________________>\n\
super.emit('initialize', new InitializeEvent(engine, this));
this._isInitialized = true;
if (this._deferredGoTo) {
this.goToScene(this._deferredGoTo);
const deferredScene = this._deferredGoTo;
this._deferredGoTo = null;
this.goToScene(deferredScene);
} else {
this.goToScene('root');
}
Expand Down
27 changes: 27 additions & 0 deletions src/spec/EngineSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,33 @@ describe('The engine', () => {
expect(ex.Logger.getInstance().error).toHaveBeenCalledWith('Scene', 'madeUp', 'does not exist!');
});

it('will add actors to the correct scene when initialized after a deferred goTo', () => {
const engine = TestUtils.engine();
const scene1 = new ex.Scene();
const scene2 = new ex.Scene();
engine.add('scene1', scene1);
engine.add('scene2', scene2);

scene1.onInitialize = () => {
engine.goToScene('scene2');
};
scene2.onInitialize = () => {
engine.add(new ex.Actor());
};

spyOn(scene1, 'onInitialize').and.callThrough();
spyOn(scene2, 'onInitialize').and.callThrough();


engine.goToScene('scene1');

TestUtils.runToReady(engine);

expect(engine.currentScene).toBe(scene2);
expect(scene1.actors.length).toBe(0);
expect(scene2.actors.length).toBe(1);
});

it('can screen shot the game (in WebGL)', (done) => {

const engine = TestUtils.engine({}, []);
Expand Down