Skip to content

Commit fc584cf

Browse files
author
Webeled
committed
Examples (audio, button,camera), and docs
Created some examples (audio, button,camera), and documented the source code along the way
1 parent 17e208a commit fc584cf

15 files changed

Lines changed: 393 additions & 8 deletions

File tree

4.88 KB
Binary file not shown.
5.32 KB
Binary file not shown.

examples/audio/loop.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
$title = "Using samples and looping";
3+
require('../head.php');
4+
?>
5+
6+
<script type="text/javascript">
7+
8+
(function () {
9+
10+
var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update, render: render });
11+
12+
function preload() {
13+
14+
game.load.image('spyro', 'assets/pics/spyro.png');
15+
16+
// Firefox doesn't support mp3 files, so use ogg
17+
game.load.audio('squit', ['assets/audio/SoundEffects/squit.mp3', 'assets/audio/SoundEffects/squit.ogg']);
18+
19+
}
20+
21+
var s;
22+
var music;
23+
24+
function create() {
25+
26+
game.stage.backgroundColor = '#255d3b';
27+
28+
29+
music = game.add.audio('squit',1,true);
30+
music.play();
31+
32+
s = game.add.sprite(game.world.centerX, game.world.centerY, 'spyro');
33+
s.anchor.setTo(0.5, 0.5);
34+
35+
}
36+
37+
function update() {
38+
//s.rotation += 0.01;
39+
}
40+
41+
function render() {
42+
game.debug.renderSoundInfo(music, 20, 32);
43+
}
44+
45+
})();
46+
47+
</script>
48+
49+
<?php
50+
require('../foot.php');
51+
?>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
$title = "Clicking on a button ";
3+
require('../head.php');
4+
?>
5+
6+
<script type="text/javascript">
7+
8+
(function () {
9+
10+
var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create });
11+
12+
function preload() {
13+
14+
game.load.spritesheet('button', 'assets/buttons/button_sprite_sheet.png', 193, 71);
15+
game.load.image('background','assets/misc/starfield.jpg');
16+
17+
18+
}
19+
var button,
20+
background;
21+
22+
23+
function create() {
24+
25+
game.stage.backgroundColor = '#182d3b';
26+
27+
28+
background=game.add.tileSprite(0, 0, 800, 600, 'background');
29+
30+
button = game.add.button(game.world.centerX, 400, 'button', actionOnClick, this, 2, 1, 0);
31+
32+
33+
34+
}
35+
36+
function actionOnClick () {
37+
38+
background.visible=!background.visible;
39+
40+
}
41+
42+
})();
43+
44+
</script>
45+
46+
<?php
47+
require('../foot.php');
48+
?>
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
$title = "Programmatically changing the frames";
3+
require('../head.php');
4+
?>
5+
6+
<script type="text/javascript">
7+
8+
(function () {
9+
10+
var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create });
11+
12+
function preload() {
13+
14+
game.load.spritesheet('button', 'assets/buttons/number-buttons-90x90.png', 90,90);
15+
game.load.image('background','assets/misc/starfield.jpg');
16+
17+
18+
}
19+
var button,
20+
background;
21+
22+
23+
function create() {
24+
25+
26+
//setting the background colour
27+
game.stage.backgroundColor = '#182d3b';
28+
29+
// the numbers given in parameters are the indexes of the frames, in this order :
30+
// over,out,down
31+
button = game.add.button(game.world.centerX, game.world.centerY, 'button', actionOnClick, this, 1, 0, 2);
32+
33+
//setting the anchor to the center
34+
button.anchor.setTo(0.5,0.5);
35+
36+
37+
38+
}
39+
40+
function actionOnClick () {
41+
42+
//manually changing the frames of the button, i.e, how it will look when you play with it
43+
button.setFrames(4,3,5);
44+
console.log('You clicked on the button');
45+
46+
47+
}
48+
49+
})();
50+
51+
</script>
52+
53+
<?php
54+
require('../foot.php');
55+
?>
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
$title = "Rotating a button";
3+
require('../head.php');
4+
?>
5+
6+
<script type="text/javascript">
7+
8+
(function () {
9+
10+
var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create,update : update });
11+
12+
function preload() {
13+
14+
game.load.spritesheet('button', 'assets/buttons/button_sprite_sheet.png', 193, 71);
15+
game.load.image('background','assets/misc/starfield.jpg');
16+
17+
18+
}
19+
var button,
20+
background;
21+
22+
23+
function create() {
24+
25+
game.stage.backgroundColor = '#cccccc';
26+
27+
// the numbers given in parameters are the indexes of the frames, in this order :
28+
// over,out,down
29+
button = game.add.button(game.world.centerX, game.world.centerY, 'button', actionOnClick, this, 1, 0, 2);
30+
31+
//set the anchor of the sprite in the center, otherwise it would rotate around the top-left corner
32+
button.anchor.setTo(0.5,0.5);
33+
34+
35+
36+
}
37+
38+
function actionOnClick () {
39+
40+
alert("Though I'm turning around, you can still click on me");
41+
42+
43+
}
44+
45+
function update () {
46+
47+
button.angle+=1;
48+
}
49+
50+
})();
51+
52+
</script>
53+
54+
<?php
55+
require('../foot.php');
56+
?>

examples/camera/basic follow.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
2+
<?php
3+
$title = "Following the player";
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, update: update });
12+
13+
var baddie,
14+
keys=Phaser.Keyboard;
15+
16+
function preload() {
17+
18+
game.load.image('background','assets/misc/starfield.jpg');
19+
game.load.image('ufo','assets/sprites/ufo.png');
20+
game.load.image('baddie','assets/sprites/space-baddie.png');
21+
22+
23+
}
24+
25+
26+
27+
function create() {
28+
29+
game.add.tileSprite(0, 0, 2000, 2000, 'background');
30+
31+
game.world.setSize(1400,1400);
32+
33+
for(var i=0,nb=10;i<nb;i++){
34+
35+
game.add.sprite(game.world.randomX,game.world.randomY,'ufo');
36+
}
37+
38+
baddie=game.add.sprite(150,320,'baddie');
39+
40+
game.camera.follow(baddie);
41+
42+
}
43+
44+
function update() {
45+
46+
baddie.body.velocity.x=baddie.body.velocity.y=0;
47+
48+
49+
if(game.input.keyboard.isDown(keys.LEFT) && !game.input.keyboard.isDown(keys.RIGHT)){
50+
51+
baddie.body.velocity.x=-155;
52+
53+
}
54+
else if(game.input.keyboard.isDown(keys.RIGHT) && !game.input.keyboard.isDown(keys.LEFT)){
55+
baddie.body.velocity.x=155;
56+
57+
}
58+
else if(game.input.keyboard.isDown(keys.UP) && !game.input.keyboard.isDown(keys.DOWN)){
59+
60+
baddie.angle=90;
61+
baddie.body.velocity.y=-155;
62+
63+
}
64+
else if(game.input.keyboard.isDown(keys.DOWN) && !game.input.keyboard.isDown(keys.UP)){
65+
baddie.angle=90;
66+
baddie.body.velocity.y=155;
67+
}
68+
}
69+
70+
})();
71+
72+
</script>
73+
74+
<?php
75+
require('../foot.php');
76+
?>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
2+
<?php
3+
$title = "Moving the game camera with the keyboard";
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, update: update });
12+
13+
function preload() {
14+
15+
game.load.tilemap('snes', 'assets/maps/smb_tiles.png', 'assets/maps/smb_level1.json', null, Phaser.Tilemap.JSON);
16+
}
17+
18+
function create() {
19+
20+
21+
//setting the size of the game world larger than the tilemap's size
22+
game.world.setSize(2000,2000);
23+
24+
// game.camera.width=150;
25+
// game.camera.height=150;
26+
27+
game.stage.backgroundColor = '#255d3b';
28+
29+
// adding the tilemap
30+
game.add.tilemap(0, 168, 'snes');
31+
32+
}
33+
34+
function update() {
35+
if (game.input.keyboard.isDown(Phaser.Keyboard.LEFT))
36+
{
37+
game.camera.x -= 8;
38+
}
39+
else if (game.input.keyboard.isDown(Phaser.Keyboard.RIGHT))
40+
{
41+
game.camera.x += 8;
42+
}
43+
44+
if (game.input.keyboard.isDown(Phaser.Keyboard.UP))
45+
{
46+
game.camera.y -= 8;
47+
}
48+
else if (game.input.keyboard.isDown(Phaser.Keyboard.DOWN))
49+
{
50+
game.camera.y += 8;
51+
}
52+
}
53+
54+
})();
55+
56+
</script>
57+
58+
<?php
59+
require('../foot.php');
60+
?>

examples/sprites/sprite4.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ function preload() {
1717

1818
function create() {
1919

20-
// game.world._stage.backgroundColorString = '#182d3b';
21-
2220
s = game.add.sprite(game.world.centerX, game.world.centerY, 'bot');
2321
// s.anchor.setTo(0.5, 0.5);
2422
s.scale.setTo(2, 2);

0 commit comments

Comments
 (0)