Skip to content

Commit 39add47

Browse files
committed
enableBody added to Group constructor. Also: game.add.physicsGroup(Phaser.Physics.ARCADE) is a thing :)
1 parent 6b55fea commit 39add47

3 files changed

Lines changed: 32 additions & 14 deletions

File tree

src/core/Group.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,14 @@
1313
* @param {Phaser.Group|Phaser.Sprite|null} parent - The parent Group, DisplayObject or DisplayObjectContainer that this Group will be added to. If undefined it will use game.world. If null it won't be added to anything.
1414
* @param {string} [name=group] - A name for this Group. Not used internally but useful for debugging.
1515
* @param {boolean} [addToStage=false] - If set to true this Group will be added directly to the Game.Stage instead of Game.World.
16+
* @param {boolean} [enableBody=false] - If true all Sprites created with `Group.create` or `Group.createMulitple` will have a physics body created on them. Change the body type with physicsBodyType.
17+
* @param {number} [physicsBodyType=0] - If enableBody is true this is the type of physics body that is created on new Sprites. Phaser.Physics.ARCADE, Phaser.Physics.P2, Phaser.Physics.NINJA, etc.
1618
*/
17-
Phaser.Group = function (game, parent, name, addToStage) {
19+
Phaser.Group = function (game, parent, name, addToStage, enableBody, physicsBodyType) {
1820

1921
if (typeof addToStage === 'undefined') { addToStage = false; }
22+
if (typeof enableBody === 'undefined') { enableBody = false; }
23+
if (typeof physicsBodyType === 'undefined') { physicsBodyType = Phaser.Physics.ARCADE; }
2024

2125
/**
2226
* @property {Phaser.Game} game - A reference to the currently running Game.
@@ -103,14 +107,13 @@ Phaser.Group = function (game, parent, name, addToStage) {
103107

104108
/**
105109
* @property {boolean} enableBodyDebug - If true when a physics body is created (via Group.enableBody) it will create a physics debug object as well. Only works for P2 bodies.
106-
* @default
107110
*/
108-
this.enableBodyDebug = false;
111+
this.enableBodyDebug = enableBody;
109112

110113
/**
111114
* @property {number} physicsBodyType - If Group.enableBody is true this is the type of physics body that is created on new Sprites. Phaser.Physics.ARCADE, Phaser.Physics.P2, Phaser.Physics.NINJA, etc.
112115
*/
113-
this.physicsBodyType = Phaser.Physics.ARCADE;
116+
this.physicsBodyType = physicsBodyType;
114117

115118
/**
116119
* @property {string} _sortProperty - The property on which children are sorted.

src/gameobjects/GameObjectCreator.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,17 +78,15 @@ Phaser.GameObjectCreator.prototype = {
7878
* A Group is a container for display objects that allows for fast pooling, recycling and collision checks.
7979
*
8080
* @method Phaser.GameObjectCreator#group
81-
* @param {any} [parent] - The parent Group or DisplayObjectContainer that will hold this group, if any. If set to null the Group won't be added to the display list. If undefined it will be added to World by default.
8281
* @param {string} [name='group'] - A name for this Group. Not used internally but useful for debugging.
8382
* @param {boolean} [addToStage=false] - If set to true this Group will be added directly to the Game.Stage instead of Game.World.
83+
* @param {boolean} [enableBody=false] - If true all Sprites created with `Group.create` or `Group.createMulitple` will have a physics body created on them. Change the body type with physicsBodyType.
84+
* @param {number} [physicsBodyType=0] - If enableBody is true this is the type of physics body that is created on new Sprites. Phaser.Physics.ARCADE, Phaser.Physics.P2, Phaser.Physics.NINJA, etc.
8485
* @return {Phaser.Group} The newly created group.
8586
*/
86-
group: function (parent, name, addToStage) {
87+
group: function (parent, name, addToStage, enableBody, physicsBodyType) {
8788

88-
if (typeof name === 'undefined') { name = 'group'; }
89-
if (typeof addToStage === 'undefined') { addToStage = false; }
90-
91-
return new Phaser.Group(this.game, parent, name, addToStage);
89+
return new Phaser.Group(this.game, null, name, addToStage, enableBody, physicsBodyType);
9290

9391
},
9492

src/gameobjects/GameObjectFactory.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,31 @@ Phaser.GameObjectFactory.prototype = {
9898
* @param {any} [parent] - The parent Group or DisplayObjectContainer that will hold this group, if any. If set to null the Group won't be added to the display list. If undefined it will be added to World by default.
9999
* @param {string} [name='group'] - A name for this Group. Not used internally but useful for debugging.
100100
* @param {boolean} [addToStage=false] - If set to true this Group will be added directly to the Game.Stage instead of Game.World.
101+
* @param {boolean} [enableBody=false] - If true all Sprites created with `Group.create` or `Group.createMulitple` will have a physics body created on them. Change the body type with physicsBodyType.
102+
* @param {number} [physicsBodyType=0] - If enableBody is true this is the type of physics body that is created on new Sprites. Phaser.Physics.ARCADE, Phaser.Physics.P2, Phaser.Physics.NINJA, etc.
101103
* @return {Phaser.Group} The newly created group.
102104
*/
103-
group: function (parent, name, addToStage) {
105+
group: function (parent, name, addToStage, enableBody, physicsBodyType) {
104106

105-
if (typeof name === 'undefined') { name = 'group'; }
106-
if (typeof addToStage === 'undefined') { addToStage = false; }
107+
return new Phaser.Group(this.game, parent, name, addToStage, enableBody, physicsBodyType);
108+
109+
},
110+
111+
/**
112+
* A Group is a container for display objects that allows for fast pooling, recycling and collision checks.
113+
* A Physics Group is the same as an ordinary Group except that is has enableBody turned on by default, so any Sprites it creates
114+
* are automatically given a physics body.
115+
*
116+
* @method Phaser.GameObjectFactory#group
117+
* @param {number} [physicsBodyType=Phaser.Physics.ARCADE] - If enableBody is true this is the type of physics body that is created on new Sprites. Phaser.Physics.ARCADE, Phaser.Physics.P2, Phaser.Physics.NINJA, etc.
118+
* @param {any} [parent] - The parent Group or DisplayObjectContainer that will hold this group, if any. If set to null the Group won't be added to the display list. If undefined it will be added to World by default.
119+
* @param {string} [name='group'] - A name for this Group. Not used internally but useful for debugging.
120+
* @param {boolean} [addToStage=false] - If set to true this Group will be added directly to the Game.Stage instead of Game.World.
121+
* @return {Phaser.Group} The newly created group.
122+
*/
123+
physicsGroup: function (physicsBodyType, parent, name, addToStage) {
107124

108-
return new Phaser.Group(this.game, parent, name, addToStage);
125+
return new Phaser.Group(this.game, parent, name, addToStage, true, physicsBodyType);
109126

110127
},
111128

0 commit comments

Comments
 (0)