From f4a86fd368cb48f51c8c628155b04130f121b1fb Mon Sep 17 00:00:00 2001 From: Richard Davey Date: Wed, 24 Apr 2019 12:09:16 +0100 Subject: [PATCH] The `Container.setScrollFactor` method has a new optional argument `updateChildren`. If set, it will change the `scrollFactor` values of all the Container children as well as the Container. Fix #4466 #4475 --- CHANGELOG.md | 1 + src/gameobjects/container/Container.js | 99 +++++++++++++++++++++++++- 2 files changed, 98 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 69c3966123..f21f095553 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -82,6 +82,7 @@ Notes: * `CSSFile` is a new Loader FileType that allows you to load css into the current document via the normal Phaser Loader, using the `load.css` method. As such, you can chain it with other load calls, load via config, use as part of a pack load or any other option available to all loader filetypes. The CSS is applied immediately to the document. * `MultiScriptFile` is a new Loader FileType that allows you to load multiple script files into the document via the Phaser Loader, using the new `load.scripts` method. The difference between this and `load.script` is that you must pass an array of script file URLs to this method and they will be loaded in parallel but _processed_ (i.e. added to the document) in the exact order specified in the array. This allows you to load a bundle of scripts that have dependencies on each other. * `Key.getDuration` is a new method that will return the duration, in ms, that the Key has been held down for. If the Key isn't down it will return zero. +* The `Container.setScrollFactor` method has a new optional argument `updateChildren`. If set, it will change the `scrollFactor` values of all the Container children as well as the Container. Fix #4466 #4475 (thanks @pinkkis @enriqueto) ### Updates diff --git a/src/gameobjects/container/Container.js b/src/gameobjects/container/Container.js index 85bb56f0cc..f61d40a52e 100644 --- a/src/gameobjects/container/Container.js +++ b/src/gameobjects/container/Container.js @@ -63,7 +63,6 @@ var Vector2 = require('../../math/Vector2'); * @extends Phaser.GameObjects.Components.ComputedSize * @extends Phaser.GameObjects.Components.Depth * @extends Phaser.GameObjects.Components.Mask - * @extends Phaser.GameObjects.Components.ScrollFactor * @extends Phaser.GameObjects.Components.Transform * @extends Phaser.GameObjects.Components.Visible * @@ -82,7 +81,6 @@ var Container = new Class({ Components.ComputedSize, Components.Depth, Components.Mask, - Components.ScrollFactor, Components.Transform, Components.Visible, Render @@ -193,6 +191,60 @@ var Container = new Class({ */ this._sysEvents = scene.sys.events; + /** + * The horizontal scroll factor of this Container. + * + * The scroll factor controls the influence of the movement of a Camera upon this Container. + * + * When a camera scrolls it will change the location at which this Container is rendered on-screen. + * It does not change the Containers actual position values. + * + * For a Container, setting this value will only update the Container itself, not its children. + * If you wish to change the scrollFactor of the children as well, use the `setScrollFactor` method. + * + * A value of 1 means it will move exactly in sync with a camera. + * A value of 0 means it will not move at all, even if the camera moves. + * Other values control the degree to which the camera movement is mapped to this Container. + * + * Please be aware that scroll factor values other than 1 are not taken in to consideration when + * calculating physics collisions. Bodies always collide based on their world position, but changing + * the scroll factor is a visual adjustment to where the textures are rendered, which can offset + * them from physics bodies if not accounted for in your code. + * + * @name Phaser.GameObjects.Container#scrollFactorX + * @type {number} + * @default 1 + * @since 3.0.0 + */ + this.scrollFactorX = 1; + + /** + * The vertical scroll factor of this Container. + * + * The scroll factor controls the influence of the movement of a Camera upon this Container. + * + * When a camera scrolls it will change the location at which this Container is rendered on-screen. + * It does not change the Containers actual position values. + * + * For a Container, setting this value will only update the Container itself, not its children. + * If you wish to change the scrollFactor of the children as well, use the `setScrollFactor` method. + * + * A value of 1 means it will move exactly in sync with a camera. + * A value of 0 means it will not move at all, even if the camera moves. + * Other values control the degree to which the camera movement is mapped to this Container. + * + * Please be aware that scroll factor values other than 1 are not taken in to consideration when + * calculating physics collisions. Bodies always collide based on their world position, but changing + * the scroll factor is a visual adjustment to where the textures are rendered, which can offset + * them from physics bodies if not accounted for in your code. + * + * @name Phaser.GameObjects.Container#scrollFactorY + * @type {number} + * @default 1 + * @since 3.0.0 + */ + this.scrollFactorY = 1; + this.setPosition(x, y); this.clearAlpha(); @@ -1072,6 +1124,49 @@ var Container = new Class({ return this; }, + /** + * Sets the scroll factor of this Container and optionally all of its children. + * + * The scroll factor controls the influence of the movement of a Camera upon this Game Object. + * + * When a camera scrolls it will change the location at which this Game Object is rendered on-screen. + * It does not change the Game Objects actual position values. + * + * A value of 1 means it will move exactly in sync with a camera. + * A value of 0 means it will not move at all, even if the camera moves. + * Other values control the degree to which the camera movement is mapped to this Game Object. + * + * Please be aware that scroll factor values other than 1 are not taken in to consideration when + * calculating physics collisions. Bodies always collide based on their world position, but changing + * the scroll factor is a visual adjustment to where the textures are rendered, which can offset + * them from physics bodies if not accounted for in your code. + * + * @method Phaser.GameObjects.Container#setScrollFactor + * @since 3.0.0 + * + * @param {number} x - The horizontal scroll factor of this Game Object. + * @param {number} [y=x] - The vertical scroll factor of this Game Object. If not set it will use the `x` value. + * @param {boolean} [updateChildren=false] - Apply this scrollFactor to all Container children as well? + * + * @return {this} This Game Object instance. + */ + setScrollFactor: function (x, y, updateChildren) + { + if (y === undefined) { y = x; } + if (updateChildren === undefined) { updateChildren = false; } + + this.scrollFactorX = x; + this.scrollFactorY = y; + + if (updateChildren) + { + ArrayUtils.SetAll(this.list, 'scrollFactorX', x); + ArrayUtils.SetAll(this.list, 'scrollFactorY', y); + } + + return this; + }, + /** * The number of Game Objects inside this Container. *