Skip to content

Commit 581d637

Browse files
committed
Added Group.cursor. This points to the first item added to a Group. You can move the cursor with Group.next() and Group.previous().
1 parent 3c164b4 commit 581d637

3 files changed

Lines changed: 163 additions & 1 deletion

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ Version 1.1.2
4444
* New: Added init method to plugins, to be called as they are added to the PluginManager (thanks beeglebug)
4545
* New: Physics.Body now has a center property (issue 142, thanks MikeMnD)
4646
* New: Lots of fixes across Full Screen Mode support. Input now works, scaling restores properly, world scale is correct and anti-alias support added.
47+
* New: Added Group.cursor. This points to the first item added to a Group. You can move the cursor with Group.next() and Group.previous().
4748
* Updated: Fixed a few final bugs in the Sprite body and bounds calculations, in turn this resolved the Tilemap collision issues in the 1.1 release.
4849
* Updated: Finished documentation for the Phaser.Button class.
4950
* Updated: Fixed the Invaders game sample and enhanced it.

examples/wip/cursor.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
2+
3+
function preload() {
4+
5+
game.load.image('atari1', 'assets/sprites/atari130xe.png');
6+
game.load.image('atari2', 'assets/sprites/atari800xl.png');
7+
game.load.image('atari4', 'assets/sprites/atari800.png');
8+
game.load.image('sonic', 'assets/sprites/sonic_havok_sanity.png');
9+
game.load.image('duck', 'assets/sprites/darkwing_crazy.png');
10+
game.load.image('firstaid', 'assets/sprites/firstaid.png');
11+
game.load.image('diamond', 'assets/sprites/diamond.png');
12+
game.load.image('mushroom', 'assets/sprites/mushroom2.png');
13+
14+
}
15+
16+
var group;
17+
18+
function create() {
19+
20+
group = game.add.group();
21+
22+
var sprite1 = group.create(0, 200, 'atari1');
23+
sprite1.name = 'Atari 1';
24+
25+
var sprite2 = group.create(300, 200, 'atari2');
26+
sprite2.name = 'Atari 2';
27+
28+
var sprite3 = group.create(600, 200, 'atari4');
29+
sprite3.name = 'Atari 3';
30+
31+
game.input.onDown.add(moveCursor, this);
32+
33+
group.dump(false);
34+
35+
}
36+
37+
function moveCursor () {
38+
39+
group.next();
40+
group.dump(false);
41+
42+
}
43+
44+
function update() {
45+
46+
47+
}
48+
49+
function render() {
50+
51+
game.debug.renderText(group.cursor.name, 32, 32);
52+
53+
// game.debug.renderInputInfo(32, 32);
54+
55+
}

src/core/Group.js

Lines changed: 107 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ Phaser.Group = function (game, parent, name, useStage) {
8383
this.scale = new Phaser.Point(1, 1);
8484

8585
/**
86-
* @property {any} cursor - The current display object that the Group cursor is pointing to. You can move the cursor with Group.next and Group.previous.
86+
* The cursor is a simple way to iterate through the objects in a Group using the Group.next and Group.previous functions.
87+
* The cursor is set to the first child added to the Group and doesn't change unless you call next, previous or set it directly with Group.cursor.
88+
* @property {any} cursor - The current display object that the Group cursor is pointing to.
8789
*/
8890
this.cursor = null;
8991

@@ -116,6 +118,11 @@ Phaser.Group.prototype = {
116118
this._container.addChild(child);
117119

118120
child.updateTransform();
121+
122+
if (this.cursor === null)
123+
{
124+
this.cursor = child;
125+
}
119126
}
120127

121128
return child;
@@ -145,6 +152,11 @@ Phaser.Group.prototype = {
145152
this._container.addChildAt(child, index);
146153

147154
child.updateTransform();
155+
156+
if (this.cursor === null)
157+
{
158+
this.cursor = child;
159+
}
148160
}
149161

150162
return child;
@@ -197,6 +209,11 @@ Phaser.Group.prototype = {
197209

198210
child.updateTransform();
199211

212+
if (this.cursor === null)
213+
{
214+
this.cursor = child;
215+
}
216+
200217
return child;
201218

202219
},
@@ -233,6 +250,55 @@ Phaser.Group.prototype = {
233250
this._container.addChild(child);
234251
child.updateTransform();
235252

253+
if (this.cursor === null)
254+
{
255+
this.cursor = child;
256+
}
257+
258+
}
259+
260+
},
261+
262+
/**
263+
* Advances the Group cursor to the next object in the Group. If it's at the end of the Group it wraps around to the first object.
264+
*
265+
* @method Phaser.Group#next
266+
*/
267+
next: function () {
268+
269+
if (this.cursor)
270+
{
271+
// Wrap the cursor?
272+
if (this.cursor == this._container.last)
273+
{
274+
this.cursor = this._container._iNext;
275+
}
276+
else
277+
{
278+
this.cursor = this.cursor._iNext;
279+
}
280+
}
281+
282+
},
283+
284+
/**
285+
* Moves the Group cursor to the previous object in the Group. If it's at the start of the Group it wraps around to the last object.
286+
*
287+
* @method Phaser.Group#previous
288+
*/
289+
previous: function () {
290+
291+
if (this.cursor)
292+
{
293+
// Wrap the cursor?
294+
if (this.cursor == this._container._iNext)
295+
{
296+
this.cursor = this._container.last;
297+
}
298+
else
299+
{
300+
this.cursor = this.cursor._iPrev;
301+
}
236302
}
237303

238304
},
@@ -423,8 +489,14 @@ Phaser.Group.prototype = {
423489

424490
this._container.removeChild(oldChild);
425491
this._container.addChildAt(newChild, index);
492+
426493
newChild.events.onAddedToGroup.dispatch(newChild, this);
427494
newChild.updateTransform();
495+
496+
if (this.cursor == oldChild)
497+
{
498+
this.cursor = this._container._iNext;
499+
}
428500
}
429501

430502
},
@@ -960,6 +1032,18 @@ Phaser.Group.prototype = {
9601032

9611033
this._container.removeChild(child);
9621034

1035+
if (this.cursor == child)
1036+
{
1037+
if (this._container._iNext)
1038+
{
1039+
this.cursor = this._container._iNext;
1040+
}
1041+
else
1042+
{
1043+
this.cursor = null;
1044+
}
1045+
}
1046+
9631047
child.group = null;
9641048

9651049
},
@@ -987,6 +1071,8 @@ Phaser.Group.prototype = {
9871071
}
9881072
while (this._container.children.length > 0);
9891073

1074+
this.cursor = null;
1075+
9901076
},
9911077

9921078
/**
@@ -1013,6 +1099,18 @@ Phaser.Group.prototype = {
10131099
var child = this._container.children[i];
10141100
child.events.onRemovedFromGroup.dispatch(child, this);
10151101
this._container.removeChild(child);
1102+
1103+
if (this.cursor == child)
1104+
{
1105+
if (this._container._iNext)
1106+
{
1107+
this.cursor = this._container._iNext;
1108+
}
1109+
else
1110+
{
1111+
this.cursor = null;
1112+
}
1113+
}
10161114
}
10171115

10181116
},
@@ -1034,6 +1132,8 @@ Phaser.Group.prototype = {
10341132

10351133
this.exists = false;
10361134

1135+
this.cursor = null;
1136+
10371137
},
10381138

10391139
/**
@@ -1071,6 +1171,12 @@ Phaser.Group.prototype = {
10711171
do
10721172
{
10731173
var name = displayObject.name || '*';
1174+
1175+
if (this.cursor == displayObject)
1176+
{
1177+
var name = '> ' + name;
1178+
}
1179+
10741180
var nameNext = '-';
10751181
var namePrev = '-';
10761182
var nameFirst = '-';

0 commit comments

Comments
 (0)