Skip to content

Commit faec17f

Browse files
committed
Cameras.Scene2D.Events.FOLLOW_UPDATE is a new Event that is dispatched by a Camera when it is following a Game Object. It is dispatched every frame, right after the final Camera position and internal matrices have been updated. Use it if you need to react to a camera, using its most current position and the camera is following something. Fix phaserjs#5253
1 parent 29e13be commit faec17f

3 files changed

Lines changed: 33 additions & 0 deletions

File tree

src/cameras/2d/Camera.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ var Clamp = require('../../math/Clamp');
1111
var Class = require('../../utils/Class');
1212
var Components = require('../../gameobjects/components');
1313
var Effects = require('./effects');
14+
var Events = require('./events');
1415
var Linear = require('../../math/Linear');
1516
var Rectangle = require('../../geom/rectangle/Rectangle');
1617
var Vector2 = require('../../math/Vector2');
@@ -774,6 +775,8 @@ var Camera = new Class({
774775
CenterOn(deadzone, this.midPoint.x, this.midPoint.y);
775776
}
776777

778+
var emitFollowEvent = false;
779+
777780
if (follow && !this.panEffect.isRunning)
778781
{
779782
var fx = (follow.x - this.followOffset.x);
@@ -804,6 +807,8 @@ var Camera = new Class({
804807
sx = Linear(sx, fx - originX, this.lerp.x);
805808
sy = Linear(sy, fy - originY, this.lerp.y);
806809
}
810+
811+
emitFollowEvent = true;
807812
}
808813

809814
if (this.useBounds)
@@ -850,6 +855,11 @@ var Camera = new Class({
850855
matrix.translate(-originX, -originY);
851856

852857
this.shakeEffect.preRender();
858+
859+
if (emitFollowEvent)
860+
{
861+
this.emit(Events.FOLLOW_UPDATE, this, follow);
862+
}
853863
},
854864

855865
/**
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2020 Photon Storm Ltd.
4+
* @license {@link https://opensource.org/licenses/MIT|MIT License}
5+
*/
6+
7+
/**
8+
* The Camera Follower Update Event.
9+
*
10+
* This event is dispatched by a Camera instance when it is following a
11+
* Game Object and the Camera position has been updated as a result of
12+
* that following.
13+
*
14+
* Listen to it from a Camera instance using: `camera.on('followupdate', listener)`.
15+
*
16+
* @event Phaser.Cameras.Scene2D.Events#FOLLOW_UPDATE
17+
* @since 3.50.0
18+
*
19+
* @param {Phaser.Cameras.Scene2D.BaseCamera} camera - The camera that emitted the event.
20+
* @param {Phaser.GameObjects.GameObject} gameObject - The Game Object the camera is following.
21+
*/
22+
module.exports = 'followupdate';

src/cameras/2d/events/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ module.exports = {
1717
FADE_OUT_START: require('./FADE_OUT_START_EVENT'),
1818
FLASH_COMPLETE: require('./FLASH_COMPLETE_EVENT'),
1919
FLASH_START: require('./FLASH_START_EVENT'),
20+
FOLLOW_UPDATE: require('./FOLLOW_UPDATE_EVENT'),
2021
PAN_COMPLETE: require('./PAN_COMPLETE_EVENT'),
2122
PAN_START: require('./PAN_START_EVENT'),
2223
POST_RENDER: require('./POST_RENDER_EVENT'),

0 commit comments

Comments
 (0)