Skip to content

Commit 903b11b

Browse files
author
Webeled
committed
Groups examples and some boolean checks corrected
1 parent 18c695e commit 903b11b

12 files changed

Lines changed: 563 additions & 5 deletions

examples/camera/follow styles.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,6 @@ function create() {
7171

7272
game.camera.follow(ufo);
7373

74-
75-
76-
7774
// follow style switch buttons
7875
btn0 = game.add.button(6, 40, 'button', lockonFollow,this, 0, 0, 0);
7976
btn1 = game.add.button(6, 120, 'button', platformerFollow,this, 1, 1, 1);

examples/groups/add to group 1.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
$title = "Adding to group using 'add'";
3+
require('../head.php');
4+
?>
5+
6+
<script type="text/javascript">
7+
8+
(function () {
9+
10+
var game = new Phaser.Game(800, 600, Phaser.CANVAS, '', { preload: preload, create: create,render:render });
11+
12+
var friendAndFoe,
13+
enemies;
14+
15+
function preload() {
16+
game.load.image('ufo', 'assets/sprites/ufo.png');
17+
game.load.image('baddie', 'assets/sprites/space-baddie.png');
18+
19+
}
20+
function create() {
21+
22+
// Create some local groups for later use.
23+
friendAndFoe = game.add.group();
24+
enemies = game.add.group();
25+
26+
// Use game.add (GameObjectFactory) to create sprites, those
27+
// newly created ones will be added to game.world.group
28+
// automatically. While you can still use new to allocate and
29+
// only add them to your own groups.
30+
var ufo = game.add.sprite(200, 240, 'ufo');
31+
friendAndFoe.add(ufo);
32+
33+
34+
// Create some enemies using new keyword.
35+
// (Don't forget to pass game as the first parameter.)
36+
var enemy;
37+
for (var i = 0; i < 16; i++) {
38+
39+
enemy = new Phaser.Sprite(game,
40+
360 + Math.random() * 200, 120 + Math.random() * 200,
41+
'baddie');
42+
43+
enemies.add(enemy);
44+
}
45+
46+
}
47+
function render() {
48+
49+
game.debug.renderText('ufo added to game.world.group and "friendAndFoe" group', 20, 24);
50+
game.debug.renderText('others ONLY added to "enemies" group', 20, 40);
51+
52+
}
53+
54+
55+
56+
})();
57+
58+
</script>
59+
60+
<?php
61+
require('../foot.php');
62+
?>
63+

examples/groups/add to group 2.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
$title = "Adding to group using create";
3+
require('../head.php');
4+
?>
5+
6+
<script type="text/javascript">
7+
8+
(function () {
9+
10+
var game = new Phaser.Game(800, 600, Phaser.CANVAS, '', { preload: preload, create: create,render:render });
11+
12+
function preload() {
13+
game.load.image('ufo', 'assets/sprites/ufo.png');
14+
game.load.image('baddie', 'assets/sprites/space-baddie.png');
15+
16+
}
17+
18+
var friendAndFoe,
19+
enemies;
20+
21+
function create() {
22+
// Create some local groups for later use.
23+
friendAndFoe = game.add.group();
24+
enemies = game.add.group();
25+
26+
// You can directly create sprite and add it to a group
27+
// using just one line.
28+
friendAndFoe.create(200, 240, 'ufo');
29+
30+
// Create some enemies.
31+
for (var i = 0; i < 8; i++) {
32+
createBaddie();
33+
}
34+
35+
// Tap to create new baddie sprites.
36+
game.input.onTap.add(createBaddie, this);
37+
}
38+
function createBaddie() {
39+
40+
enemies.create(360 + Math.random() * 200, 120 + Math.random() * 200,'baddie');
41+
}
42+
43+
function render() {
44+
45+
game.debug.renderText('Tap screen or click to create new baddies.', 16, 24);
46+
}
47+
48+
49+
})();
50+
51+
</script>
52+
53+
<?php
54+
require('../foot.php');
55+
?>
56+

examples/groups/bring to top 2.php

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php
2+
$title = "Bringing to top and game indexes";
3+
require('../head.php');
4+
?>
5+
6+
<script type="text/javascript">
7+
8+
(function () {
9+
10+
var game = new Phaser.Game(800, 600, Phaser.CANVAS, '', { preload: preload, create: create,update:update,render:render });
11+
12+
function preload() {
13+
14+
game.load.image('beast', 'assets/pics/shadow_of_the_beast2_karamoon.png');
15+
game.load.image('snot', 'assets/pics/nslide_snot.png');
16+
game.load.image('atari1', 'assets/sprites/atari130xe.png');
17+
game.load.image('sonic', 'assets/sprites/sonic_havok_sanity.png');
18+
game.load.image('coke', 'assets/sprites/cokecan.png');
19+
game.load.image('disk', 'assets/sprites/oz_pov_melting_disk.png');
20+
21+
22+
}
23+
24+
var group1,
25+
group2,
26+
coke,
27+
disk;
28+
29+
function create() {
30+
31+
// Create a background image
32+
game.add.sprite(0, 0, 'beast');
33+
34+
// Create a Group that will sit above the background image
35+
group1 = game.add.group();
36+
37+
// Create a Group that will sit above Group 1
38+
group2 = game.add.group();
39+
40+
// Now let's create some random sprites and enable them all for drag and 'bring to top'
41+
for (var i = 0; i < 10; i++)
42+
{
43+
44+
var tempSprite = game.add.sprite(game.stage.randomX, game.stage.randomY, 'atari1');
45+
46+
tempSprite.name = 'atari' + i;
47+
tempSprite.input.start(i, true);
48+
tempSprite.input.enableDrag(false, true);
49+
50+
group1.add(tempSprite);
51+
52+
// Sonics
53+
54+
var tempSprite=game.add.sprite(game.stage.randomX, game.stage.randomY, 'sonic');
55+
56+
tempSprite.name = 'sonic' + i;
57+
tempSprite.input.start(10 + i, true);
58+
tempSprite.input.enableDrag(false, true);
59+
60+
group2.add(tempSprite);
61+
}
62+
63+
// Add 2 control sprites into each group - these cannot be dragged but should be bought to the top each time
64+
coke = group1.create(100, 100, 'coke');
65+
disk = group2.create(400, 300, 'disk');
66+
67+
// Create a foreground image - everything should appear behind this, even when dragged
68+
var snot = game.add.sprite(game.world.centerX, game.world.height, 'snot');
69+
snot.anchor.setTo(0.5, 1);
70+
71+
// You can click and drag any sprite but Sonic sprites should always appear above the Atari sprites
72+
// and both types of sprite should only ever appear above the background and behind the
73+
74+
}
75+
76+
function update() {
77+
78+
if (game.input.keyboard.justReleased(Phaser.Keyboard.ONE))
79+
{
80+
coke.bringToTop();
81+
}
82+
83+
if (game.input.keyboard.justReleased(Phaser.Keyboard.TWO))
84+
{
85+
disk.bringToTop();
86+
}
87+
88+
}
89+
90+
function render() {
91+
game.debug.renderInputInfo(32, 32);
92+
}
93+
94+
})();
95+
96+
</script>
97+
98+
<?php
99+
require('../foot.php');
100+
?>
101+

examples/groups/bring to top.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
<?php
3+
$title = "Bring to Top using parent container";
4+
require('../head.php');
5+
?>
6+
7+
<script type="text/javascript">
8+
9+
(function () {
10+
11+
var game = new Phaser.Game(800, 600, Phaser.CANVAS, '', { preload: preload, create: create,render : render });
12+
13+
var container;
14+
15+
function preload() {
16+
game.load.spritesheet('button', 'assets/buttons/number-buttons.png', 160, 160);
17+
18+
}
19+
function create() {
20+
// Container for sorting the buttons, which we'll use to make buttons
21+
// to the top later.
22+
container = game.add.group();
23+
24+
// Add buttons to container.
25+
container.add(game.add.button(200, 100, 'button', bringMeToTop, this, 0, 0, 0));
26+
container.add(game.add.button(300, 100, 'button', bringMeToTop, this, 1, 1, 1));
27+
container.add(game.add.button(100, 200, 'button', bringMeToTop, this, 2, 2, 2));
28+
container.add(game.add.button(400, 200, 'button', bringMeToTop, this, 3, 3, 3));
29+
container.add(game.add.button(300, 300, 'button', bringMeToTop, this, 4, 4, 4));
30+
container.add(game.add.button(200, 300, 'button', bringMeToTop, this, 5, 5, 5));
31+
}
32+
function render() {
33+
34+
game.debug.renderText('Tap or click buttons to bring it to the top.', 32, 32);
35+
}
36+
function bringMeToTop(btn) {
37+
container.bringToTop(btn);
38+
}
39+
40+
})();
41+
42+
</script>
43+
44+
<?php
45+
require('../foot.php');
46+
?>

examples/groups/call all.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
2+
<?php
3+
$title = "Calling a function on all members of the group";
4+
require('../head.php');
5+
?>
6+
7+
<script type="text/javascript">
8+
9+
(function () {
10+
11+
var game = new Phaser.Game(800, 600, Phaser.CANVAS, '', { preload: preload, create: create,render : render });
12+
13+
function preload() {
14+
game.load.spritesheet('item', 'assets/buttons/number-buttons-90x90.png', 90, 90);
15+
game.load.image('reviveBtn', 'assets/buttons/revive-button.png');
16+
17+
}
18+
function create() {
19+
// Add some items.
20+
var item;
21+
for (var i = 0; i < 3; i++) {
22+
// Give the items a different alpha increase speed.
23+
item = game.add.sprite(290, 98 * (i + 1), 'item', i);
24+
// Enable input.
25+
item.input.start(0, true);
26+
item.events.onInputUp.add(kill);
27+
// An item besides the left one.
28+
item = game.add.sprite(388, 98 * (i + 1), 'item', i + 3);
29+
item.input.start(0, true);
30+
item.events.onInputUp.add(kill);
31+
}
32+
// Add a button to revive all the items.
33+
game.add.button(270, 400, 'reviveBtn', reviveAll, this, 0, 0, 0);
34+
}
35+
function kill(item) {
36+
item.kill();
37+
}
38+
function reviveAll() {
39+
game.world.group.callAll('revive');
40+
}
41+
function render() {
42+
43+
game.debug.renderText('Tap or click an item to kill it, and press the revive button to revive them all.', 160, 500);
44+
}
45+
46+
})();
47+
48+
</script>
49+
50+
<?php
51+
require('../foot.php');
52+
?>

examples/groups/create group.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
2+
<?php
3+
$title = "Calling a function on all children";
4+
require('../head.php');
5+
?>
6+
7+
<script type="text/javascript">
8+
9+
(function () {
10+
11+
var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create});
12+
13+
function preload() {
14+
15+
// Using Phasers asset loader we load up a PNG from the assets folder
16+
game.load.image('sonic', 'assets/sprites/sonic_havok_sanity.png');
17+
18+
19+
}
20+
21+
var firstGroup;
22+
23+
function create() {
24+
25+
// Here we'll create a new Group
26+
firstGroup = game.add.group();
27+
28+
// And add some sprites to it
29+
for (var i = 0; i < 10; i++)
30+
{
31+
// Create a new sprite at a random screen location
32+
var newSprite = new Phaser.Sprite(game, game.stage.randomX, game.stage.randomY, 'sonic');
33+
34+
// This set-ups a listener for the event, view your console.log output to see the result
35+
newSprite.events.onAddedToGroup.add(logGroupAdd);
36+
37+
// Add the sprite to the Group
38+
firstGroup.add(newSprite);
39+
}
40+
41+
}
42+
43+
function logGroupAdd(sprite, group) {
44+
45+
console.log('Sprite added to Group', group.ID, 'at z-index:', group.getIndex(sprite));
46+
47+
48+
49+
}
50+
51+
})();
52+
53+
</script>
54+
55+
<?php
56+
require('../foot.php');
57+
?>

0 commit comments

Comments
 (0)