From 1bfe58ab5571d88cc5463d04ae79d262a4972f93 Mon Sep 17 00:00:00 2001 From: Richard Davey Date: Wed, 27 Jun 2018 13:03:40 +0100 Subject: [PATCH] remove can take an array of cameras and also no longer needs total to be > 0 --- CHANGELOG.md | 2 ++ src/cameras/2d/CameraManager.js | 27 +++++++++++++++++++-------- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6bcfada8a..984874f4d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,8 @@ * `CameraManager.addExisting` no longer needs to be passed a Camera that already exists in the pool (as the pool has been removed), meaning you can now create your own Cameras and pass them to `addExisting` and have them treated as normal cameras and not be ignored by the manager. They are also assigned a proper ID when added. * `CameraManager.addExisting` has a new boolean argument `makeMain` which will make the new camera the main one. * `CameraManager.getTotal` is a new method that will return the total number of Cameras being managed, with an optional `isVisible` argument, that only counts visible cameras if set. +* `CameraManager.remove` can now take an array of cameras to be removed from the manager, as well as a single camera. +* `CameraManager.remove` would previously not allow you to remove a camera if it meant there would be no cameras left in the Camera Manager. This restriction has been removed. A Camera Manager can now run even with zero cameras. Your game obviously won't display anything, but it's still now possible. ### New Features diff --git a/src/cameras/2d/CameraManager.js b/src/cameras/2d/CameraManager.js index 3bd42b560..3656fbfe0 100644 --- a/src/cameras/2d/CameraManager.js +++ b/src/cameras/2d/CameraManager.js @@ -492,7 +492,7 @@ var CameraManager = new Class({ }, /** - * Removes the given Camera from this Camera Manager. + * Removes the given Camera, or an array of Cameras, from this Camera Manager. * * If found in the Camera Manager it will be immediately removed from the local cameras array. * If also currently the 'main' camera, 'main' will be reset to be camera 0. @@ -503,21 +503,32 @@ var CameraManager = new Class({ * @method Phaser.Cameras.Scene2D.CameraManager#remove * @since 3.0.0 * - * @param {Phaser.Cameras.Scene2D.Camera} camera - The Camera to be removed from this Camera Manager. + * @param {(Phaser.Cameras.Scene2D.Camera|Phaser.Cameras.Scene2D.Camera[])} camera - The Camera, or an array of Cameras, to be removed from this Camera Manager. */ remove: function (camera) { - var cameraIndex = this.cameras.indexOf(camera); - - if (cameraIndex >= 0 && this.cameras.length > 1) + if (!Array.isArray(camera)) { - this.cameras.splice(cameraIndex, 1); + camera = [ camera ]; + } - if (this.main === camera) + var cameras = this.cameras; + + for (var i = 0; i < camera.length; i++) + { + var index = cameras.indexOf(camera); + + if (index !== -1) { - this.main = this.cameras[0]; + cameras.splice(index, 1); + } } + + if (!this.main) + { + this.main = this.cameras[0]; + } }, /**