Get post-update Camera scrollX, scrollY

Hi,

I have a scene where I have the main camera “startFollow” on a player sprite with Arcade physics. I have an image sprite that relies on the scrollX and scrollY of the main camera AFTER it has finished updating to follow the player sprite.

Getting this.cameras.main.scrollX/scrollY from the update function of the scene happens before the “follow” happens, and so the image sprite lags behind in the render.

I tried using the scene “postupdate” eventEmitter.

    let eventEmitter = this.scene.systems.events;
		eventEmitter.on("postupdate", function() {
			// Get main camera scrollX, scrollY
		}, this);

The above seems to happen before the camera updates so the image sprite still lags behind.

I’ve tried adding a listener to the camera via the following:

this.cameras.main.on("postupdate", function() {
// Get the camera scrollX, and scrollY
}, this);
this.cameras.main.on("prerender", function() {
// Get the camera scrollX, and scrollY
}, this);
this.cameras.main.addListener("postupdate", function() {
// Get the camera scrollX, and scrollY
}, this);
this.cameras.main.addListener("prerender", function() {
// Get the camera scrollX, and scrollY
}, this);

But none of these seem to fire.

How can I get the scrollX and scrollY of the camera after it has finished updating because of “startFollow”?

Thanks :smiley:

The Scene’s post-update event is too early because it’s fired before the Camera’s pre-render. Cameras do not have update events. Their render events are emitted only if the Camera is rendering to a texture, which is why your listeners aren’t firing. This is a tricky problem and I’m not sure if there’s a good way to solve it.

The first thing you should consider is whether you can use the image’s scroll factor instead. If it’s just a visual effect which follows the camera, this is by far the easiest and cleanest solution.

If you can’t use a scroll factor for this, another quick solution is to just make the Camera render to a texture. This will probably hurt the game’s performance, but it will allow you to listen for a prerender event, which is dispatched after the Camera’s position has been updated.

Finally, you could override the image’s rendering method - either renderCanvas or renderWebGL depending on the renderer you’re using. This will allow you to change the image’s position, then call its original rendering method. If you can’t use its scroll factor (and you’re certain you can’t), this solution is better than the previous one, albeit a bit more complicated.

Yeah, scroll factor isn’t the answer for this one. Looks like it’s gonna be complicated. :frowning:
Thanks for your help anyways :smiley: