Camera Data from One Scene to Another

I was wondering if there was a way to send camera data from one scene to another.

Essentially, I have this in my original scene to update an image within that ‘level’ scene:

this.parallax.tilePositionX = this.cameras.main.scrollX * 0.5;

Since then, I have moved this image to another scene that controls both static and parallax backgrounds (for scaling reasons and camera zoom quirks) – so I can call this scene to create backgrounds based on which level a player is on.

however, I would like to pull “this.cameras.main.scrollX * 0.5” data from a given ‘level’ scene.

You can use the SceneManager's getScene method to get a reference to a different scene by key: https://photonstorm.github.io/phaser3-docs/Phaser.Scenes.SceneManager.html#getScene__anchor

Then, you can access that other scene’s cameras from that reference.

Is that the same as:

let level0 = this.scene.get(‘Level0’);

so far I have that written but when I write:

this.parallax1.tilePositionX = level0.cameras.main.scrollX * 0.5;

It is saying level0 is not defined, rewriting it other ways leads to cameras is not defined or main is not defined. I’m still fairly new to JS so I’m not quite sure about the syntax of extending items with dotSomethingdotSomething. I can’t say what syntax mistakes I’m doing. Sorry.

Yep, what you wrote is the approach I was getting at.

I believe you can only do a this.scene.get for a scene that is running. so if the 'Level0' scene isn’t running when you make this call, you’d get back undefined which would match your error.

I see I see.

Technically both are active and running – but… ‘Background’ Scene loads before ‘Level’ Scene (so I can have the depth correct. (Because I have Background > Level > UI scenes being created/loaded in that order) so based on that logic, it’s trying to pull ‘Level’ scene info before ‘Level’ scene actually exists…

Now… that might be fixed if I had a conditional if before processing the scroll… Hahaha. Right now, I’m thinking of setting up a variable, but is it possible to simply make an implicit check like if (‘Scene’)

Also just another question about Scenes, if I for example, created and start Level1 would that automatically deactivate Level0 if I didn’t create it again after some condition?

Ah that makes sense then why you’re getting undefined if the order you run the scenes in is “backwards”. You can get around that by manually setting the depth of your scenes after they are all started, rather than relying on the order of your code to make it work. There are a bunch of methods in the scene manager that let you control the depths of your scenes (see https://photonstorm.github.io/phaser3-docs/Phaser.Scenes.SceneManager.html#moveAbove__anchor as an example).

Scenes get created/destroyed depending on the methods you call. For example, calling this.scene.start(newSceneKey) will stop the scene you called that from then start the new scene. But this.scene.launch(newSceneKey) will keep the scene you called that from running and also start the new scene. This is a good resource for all the methods that you can interact with scenes: https://github.com/samme/phaser3-faq/wiki#how-do-scenes-work

1 Like

Thank you so much @snowbillr !!!

I’ve just reorganized my scenes and used the moveAbove() function and its working. I’ll be testing the camera controls later tonight, but its been a small victory already.

1 Like