Skip to content

Commit 428e331

Browse files
committed
Groups can now be added to other Groups as children via group.add() and group.addAt().
Groups now have an 'alive' property, which can be useful when iterating through child groups with functions like forEachAlive.
1 parent aa3a86d commit 428e331

6 files changed

Lines changed: 205 additions & 45 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ New features:
7777
* Buttons now properly use their upFrame if set.
7878
* InputHandler now has snapOffsetX and snapOffsetY properties so your snap grid doesn't have to be 0,0 aligned (thanks srmeier)
7979
* Loader.progressFloat contains the actual non-rounded progress value, where-as Loader.progress contains a rounded value. Use progressFloat if you've > 100 files to load.
80+
* Groups can now be added to other Groups as children via group.add() and group.addAt()
81+
* Groups now have an 'alive' property, which can be useful when iterating through child groups with functions like forEachAlive.
8082

8183

8284
New Examples:
@@ -99,6 +101,7 @@ New Examples:
99101
* Animation - Group Creation, showing how to create animations across all Group children in one call.
100102
* Particles - Rain by Jens Anders Bakke.
101103
* Particles - Snow by Jens Anders Bakke.
104+
* Groups - Nested Groups - showing how to embed one Group into another Group.
102105

103106

104107
Updates:
Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,45 @@
1-
window.onload = function() {
1+
// Here is a custom group
2+
// It will automatically create 30 sprites of the given image when created.
23

3-
// Here is a custom group
4-
// It will automatically create 30 sprites of the given image when created.
4+
MonsterGroup = function (game, image, action) {
55

6-
MonsterGroup = function (game, image, action) {
6+
Phaser.Group.call(this, game);
77

8-
Phaser.Group.call(this, game);
8+
for (var i = 0; i < 30; i++)
9+
{
10+
var sprite = this.create(game.world.randomX, game.world.randomY, image);
911

10-
for (var i = 0; i < 30; i++)
12+
if (action == 'bounce')
1113
{
12-
var sprite = this.create(game.world.randomX, game.world.randomY, image);
13-
14-
if (action == 'bounce')
15-
{
16-
game.add.tween(sprite).to({ y: sprite.y - 100 }, 2000, Phaser.Easing.Elastic.Out, true, 0, 1000, true);
17-
}
18-
else if (action == 'slide')
19-
{
20-
game.add.tween(sprite).to({ x: sprite.x + 200 }, 4000, Phaser.Easing.Elastic.Out, true, 0, 1000, true);
21-
}
22-
14+
game.add.tween(sprite).to({ y: sprite.y - 100 }, 2000, Phaser.Easing.Elastic.Out, true, 0, 1000, true);
15+
}
16+
else if (action == 'slide')
17+
{
18+
game.add.tween(sprite).to({ x: sprite.x + 200 }, 4000, Phaser.Easing.Elastic.Out, true, 0, 1000, true);
2319
}
2420

25-
};
21+
}
2622

27-
MonsterGroup.prototype = Object.create(Phaser.Group.prototype);
28-
MonsterGroup.prototype.constructor = MonsterGroup;
23+
};
2924

30-
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create });
25+
MonsterGroup.prototype = Object.create(Phaser.Group.prototype);
26+
MonsterGroup.prototype.constructor = MonsterGroup;
3127

32-
var customGroup1;
33-
var customGroup2;
28+
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create });
3429

35-
function preload() {
30+
var customGroup1;
31+
var customGroup2;
3632

37-
game.load.image('ufo', 'assets/sprites/ufo.png');
38-
game.load.image('baddie', 'assets/sprites/space-baddie.png');
39-
40-
}
33+
function preload() {
4134

42-
function create() {
35+
game.load.image('ufo', 'assets/sprites/ufo.png');
36+
game.load.image('baddie', 'assets/sprites/space-baddie.png');
37+
38+
}
4339

44-
customGroup1 = new MonsterGroup(game, 'ufo', 'bounce');
45-
customGroup2 = new MonsterGroup(game, 'baddie', 'slide');
40+
function create() {
4641

47-
}
42+
customGroup1 = new MonsterGroup(game, 'ufo', 'bounce');
43+
customGroup2 = new MonsterGroup(game, 'baddie', 'slide');
4844

49-
}();
45+
}

examples/groups/nested groups.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create, update: update });
3+
4+
function preload() {
5+
6+
game.load.image('ball', 'assets/sprites/pangball.png');
7+
game.load.image('arrow', 'assets/sprites/asteroids_ship.png');
8+
9+
}
10+
11+
var ballsGroup;
12+
var shipsGroup;
13+
14+
function create() {
15+
16+
ballsGroup = game.add.group();
17+
shipsGroup = game.add.group();
18+
19+
for (var i = 0; i < 20; i++)
20+
{
21+
// Create some randomly placed sprites in both Groups
22+
ballsGroup.create(game.rnd.integerInRange(0, 128), game.world.randomY, 'ball');
23+
shipsGroup.create(game.rnd.integerInRange(0, 128), game.world.randomY, 'arrow');
24+
}
25+
26+
// Now make the ships group a child of the balls group - so anything that happens to the balls group
27+
// will happen to the ships group too
28+
ballsGroup.add(shipsGroup);
29+
30+
}
31+
32+
function update() {
33+
34+
ballsGroup.x += 0.1;
35+
36+
// Because shipsGroup is a child of ballsGroup it has already been moved 0.1px by the line above
37+
// So the following line of code will make it appear to move twice as fast as ballsGroup
38+
shipsGroup.x += 0.1;
39+
40+
}

examples/wip/group extends.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2+
// Here is a custom group with a property 'key'
3+
// This isn't required, it's just an example of a custom group level property. Only 'game' is required.
4+
MonsterGroup = function (game, key) {
5+
6+
Phaser.Group.call(this, game);
7+
8+
this.key = key;
9+
10+
};
11+
12+
MonsterGroup.prototype = Object.create(Phaser.Group.prototype);
13+
MonsterGroup.prototype.constructor = MonsterGroup;
14+
15+
/**
16+
* Generate some monsters on request, all spaced out evenly
17+
*/
18+
MonsterGroup.prototype.make = function(qty, x, y) {
19+
20+
for (var i = 0; i < qty; i++)
21+
{
22+
this.create(x, y, this.key);
23+
x += 64;
24+
}
25+
26+
}
27+
28+
// Boilerplate code below
29+
30+
var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create });
31+
32+
function preload() {
33+
34+
game.load.image('bunny', 'assets/sprites/bunny.png');
35+
game.load.image('ball', 'assets/sprites/shinyball.png');
36+
37+
}
38+
39+
function create() {
40+
41+
var monsters = new MonsterGroup(game, 'bunny');
42+
var balls = new MonsterGroup(game, 'ball');
43+
44+
monsters.make(6, 100, 100);
45+
balls.make(10, 64, 500);
46+
47+
}

examples/wip/nested groups.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create, update: update });
3+
4+
function preload() {
5+
6+
game.load.image('ball', 'assets/sprites/pangball.png');
7+
game.load.image('arrow', 'assets/sprites/asteroids_ship.png');
8+
9+
}
10+
11+
var ballsGroup;
12+
var shipsGroup;
13+
14+
function create() {
15+
16+
ballsGroup = game.add.group();
17+
shipsGroup = game.add.group();
18+
19+
for (var i = 0; i < 20; i++)
20+
{
21+
// Create some randomly placed sprites in both Groups
22+
ballsGroup.create(game.rnd.integerInRange(0, 128), game.world.randomY, 'ball');
23+
shipsGroup.create(game.rnd.integerInRange(0, 128), game.world.randomY, 'arrow');
24+
}
25+
26+
// Now make the ships group a child of the balls group - so anything that happens to the balls group
27+
// will happen to the ships group too
28+
ballsGroup.add(shipsGroup);
29+
30+
}
31+
32+
function update() {
33+
34+
ballsGroup.x += 0.1;
35+
36+
// Because shipsGroup is a child of ballsGroup it has already been moved 0.1px by the line above
37+
// So the following line of code will make it appear to move twice as fast as ballsGroup
38+
shipsGroup.x += 0.1;
39+
40+
}
41+

src/core/Group.js

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,23 @@ Phaser.Group = function (game, parent, name, useStage) {
7171
*/
7272
this.type = Phaser.GROUP;
7373

74+
/**
75+
* @property {boolean} alive - The alive property is useful for Groups that are children of other Groups and need to be included/excluded in checks like forEachAlive.
76+
* @default
77+
*/
78+
this.alive = true;
79+
7480
/**
7581
* @property {boolean} exists - If exists is true the the Group is updated, otherwise it is skipped.
7682
* @default
7783
*/
7884
this.exists = true;
7985

86+
/**
87+
* @property {Phaser.Group} group - The parent Group of this Group, if a child of another.
88+
*/
89+
this.group = null;
90+
8091
// Replaces the PIXI.Point with a slightly more flexible one.
8192
this._container.scale = new Phaser.Point(1, 1);
8293

@@ -141,16 +152,27 @@ Phaser.Group.prototype = {
141152

142153
if (child.group !== this)
143154
{
144-
child.group = this;
145-
146-
if (child.events)
155+
if (child.type && child.type === Phaser.GROUP)
147156
{
148-
child.events.onAddedToGroup.dispatch(child, this);
157+
child.group = this;
158+
159+
this._container.addChild(child._container);
160+
161+
child._container.updateTransform();
149162
}
163+
else
164+
{
165+
child.group = this;
150166

151-
this._container.addChild(child);
167+
if (child.events)
168+
{
169+
child.events.onAddedToGroup.dispatch(child, this);
170+
}
152171

153-
child.updateTransform();
172+
this._container.addChild(child);
173+
174+
child.updateTransform();
175+
}
154176

155177
if (this.cursor === null)
156178
{
@@ -175,16 +197,27 @@ Phaser.Group.prototype = {
175197

176198
if (child.group !== this)
177199
{
178-
child.group = this;
179-
180-
if (child.events)
200+
if (child.type && child.type === Phaser.GROUP)
181201
{
182-
child.events.onAddedToGroup.dispatch(child, this);
202+
child.group = this;
203+
204+
this._container.addChildAt(child._container, index);
205+
206+
child._container.updateTransform();
183207
}
208+
else
209+
{
210+
child.group = this;
211+
212+
if (child.events)
213+
{
214+
child.events.onAddedToGroup.dispatch(child, this);
215+
}
184216

185-
this._container.addChildAt(child, index);
217+
this._container.addChildAt(child, index);
186218

187-
child.updateTransform();
219+
child.updateTransform();
220+
}
188221

189222
if (this.cursor === null)
190223
{

0 commit comments

Comments
 (0)