Skip to content

Commit f4a86fd

Browse files
committed
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 phaserjs#4466 phaserjs#4475
1 parent 61011d3 commit f4a86fd

2 files changed

Lines changed: 98 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ Notes:
8282
* `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.
8383
* `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.
8484
* `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.
85+
* 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)
8586

8687
### Updates
8788

src/gameobjects/container/Container.js

Lines changed: 97 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ var Vector2 = require('../../math/Vector2');
6363
* @extends Phaser.GameObjects.Components.ComputedSize
6464
* @extends Phaser.GameObjects.Components.Depth
6565
* @extends Phaser.GameObjects.Components.Mask
66-
* @extends Phaser.GameObjects.Components.ScrollFactor
6766
* @extends Phaser.GameObjects.Components.Transform
6867
* @extends Phaser.GameObjects.Components.Visible
6968
*
@@ -82,7 +81,6 @@ var Container = new Class({
8281
Components.ComputedSize,
8382
Components.Depth,
8483
Components.Mask,
85-
Components.ScrollFactor,
8684
Components.Transform,
8785
Components.Visible,
8886
Render
@@ -193,6 +191,60 @@ var Container = new Class({
193191
*/
194192
this._sysEvents = scene.sys.events;
195193

194+
/**
195+
* The horizontal scroll factor of this Container.
196+
*
197+
* The scroll factor controls the influence of the movement of a Camera upon this Container.
198+
*
199+
* When a camera scrolls it will change the location at which this Container is rendered on-screen.
200+
* It does not change the Containers actual position values.
201+
*
202+
* For a Container, setting this value will only update the Container itself, not its children.
203+
* If you wish to change the scrollFactor of the children as well, use the `setScrollFactor` method.
204+
*
205+
* A value of 1 means it will move exactly in sync with a camera.
206+
* A value of 0 means it will not move at all, even if the camera moves.
207+
* Other values control the degree to which the camera movement is mapped to this Container.
208+
*
209+
* Please be aware that scroll factor values other than 1 are not taken in to consideration when
210+
* calculating physics collisions. Bodies always collide based on their world position, but changing
211+
* the scroll factor is a visual adjustment to where the textures are rendered, which can offset
212+
* them from physics bodies if not accounted for in your code.
213+
*
214+
* @name Phaser.GameObjects.Container#scrollFactorX
215+
* @type {number}
216+
* @default 1
217+
* @since 3.0.0
218+
*/
219+
this.scrollFactorX = 1;
220+
221+
/**
222+
* The vertical scroll factor of this Container.
223+
*
224+
* The scroll factor controls the influence of the movement of a Camera upon this Container.
225+
*
226+
* When a camera scrolls it will change the location at which this Container is rendered on-screen.
227+
* It does not change the Containers actual position values.
228+
*
229+
* For a Container, setting this value will only update the Container itself, not its children.
230+
* If you wish to change the scrollFactor of the children as well, use the `setScrollFactor` method.
231+
*
232+
* A value of 1 means it will move exactly in sync with a camera.
233+
* A value of 0 means it will not move at all, even if the camera moves.
234+
* Other values control the degree to which the camera movement is mapped to this Container.
235+
*
236+
* Please be aware that scroll factor values other than 1 are not taken in to consideration when
237+
* calculating physics collisions. Bodies always collide based on their world position, but changing
238+
* the scroll factor is a visual adjustment to where the textures are rendered, which can offset
239+
* them from physics bodies if not accounted for in your code.
240+
*
241+
* @name Phaser.GameObjects.Container#scrollFactorY
242+
* @type {number}
243+
* @default 1
244+
* @since 3.0.0
245+
*/
246+
this.scrollFactorY = 1;
247+
196248
this.setPosition(x, y);
197249

198250
this.clearAlpha();
@@ -1072,6 +1124,49 @@ var Container = new Class({
10721124
return this;
10731125
},
10741126

1127+
/**
1128+
* Sets the scroll factor of this Container and optionally all of its children.
1129+
*
1130+
* The scroll factor controls the influence of the movement of a Camera upon this Game Object.
1131+
*
1132+
* When a camera scrolls it will change the location at which this Game Object is rendered on-screen.
1133+
* It does not change the Game Objects actual position values.
1134+
*
1135+
* A value of 1 means it will move exactly in sync with a camera.
1136+
* A value of 0 means it will not move at all, even if the camera moves.
1137+
* Other values control the degree to which the camera movement is mapped to this Game Object.
1138+
*
1139+
* Please be aware that scroll factor values other than 1 are not taken in to consideration when
1140+
* calculating physics collisions. Bodies always collide based on their world position, but changing
1141+
* the scroll factor is a visual adjustment to where the textures are rendered, which can offset
1142+
* them from physics bodies if not accounted for in your code.
1143+
*
1144+
* @method Phaser.GameObjects.Container#setScrollFactor
1145+
* @since 3.0.0
1146+
*
1147+
* @param {number} x - The horizontal scroll factor of this Game Object.
1148+
* @param {number} [y=x] - The vertical scroll factor of this Game Object. If not set it will use the `x` value.
1149+
* @param {boolean} [updateChildren=false] - Apply this scrollFactor to all Container children as well?
1150+
*
1151+
* @return {this} This Game Object instance.
1152+
*/
1153+
setScrollFactor: function (x, y, updateChildren)
1154+
{
1155+
if (y === undefined) { y = x; }
1156+
if (updateChildren === undefined) { updateChildren = false; }
1157+
1158+
this.scrollFactorX = x;
1159+
this.scrollFactorY = y;
1160+
1161+
if (updateChildren)
1162+
{
1163+
ArrayUtils.SetAll(this.list, 'scrollFactorX', x);
1164+
ArrayUtils.SetAll(this.list, 'scrollFactorY', y);
1165+
}
1166+
1167+
return this;
1168+
},
1169+
10751170
/**
10761171
* The number of Game Objects inside this Container.
10771172
*

0 commit comments

Comments
 (0)