Skip to content

Commit 7917eac

Browse files
committed
Added Matching Pairs game and updated fixed to camera example.
1 parent 39cae4c commit 7917eac

8 files changed

Lines changed: 321 additions & 44 deletions

File tree

README.md

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ By Richard Davey, [Photon Storm](http://www.photonstorm.com)
1212
View the [Official Website](http://phaser.io)<br />
1313
Follow on [Twitter](https://twitter.com/photonstorm)<br />
1414
Join the [Forum](http://www.html5gamedevs.com/forum/14-phaser/)<br />
15-
Try out 160+ [Phaser Examples](http://gametest.mobi/phaser/examples/)
15+
Try out 170+ [Phaser Examples](http://gametest.mobi/phaser/examples/)
1616

1717
[Subscribe to our new Phaser Newsletter](https://confirmsubscription.com/h/r/369DE48E3E86AF1E). We'll email you when new versions are released as well as send you our regular Phaser game making magazine.
1818

@@ -47,10 +47,9 @@ Version 1.1.4 - "Kandor" - In development
4747
New features:
4848

4949
* Added a stage.fullScreenScaleMode property to determine scaling when fullscreen (thanks oysterCrusher)
50-
* Added touch joystick example showing how to use the clay.io virtual game controller (thanks gabehollombe)
5150
* Added support for margin and spacing around a frame in Loader.spritesheet.
5251
* Added Device.vibration to check if the Vibration API is available or not.
53-
* Added Device.trident and Device.tridentVersion for testing IE11 and forced IE11 to Canvas renderer until pixi updates to support it.
52+
* Added Device.trident and Device.tridentVersion for testing IE11.
5453

5554
New Examples:
5655

@@ -59,6 +58,8 @@ New Examples:
5958
* Physics - Bounce accelerator (use the keyboard) by Patrick OReilly.
6059
* Physics - Bounce knock (use the keyboard) by Patrick OReilly.
6160
* Physics - Snake (use the keyboard to control the snake like creature) by Patrick OReilly and Richard Davey.
61+
* Added touch joystick example showing how to use the clay.io virtual game controller (thanks gabehollombe)
62+
* Games - Matching Pairs by Patrick OReilly.
6263

6364
Updates:
6465

@@ -72,6 +73,10 @@ Updates:
7273
* separateY updated to re-implement the 'riding platforms' special condition (thanks cocoademon)
7374
* SoundManager.onSoundDecode now dispatches the key followed by the sound object, also now dispatched by the Cache when doing an auto-decode on load.
7475
* Switch method of using trimmed sprites to support scaling and rotation (thanks cocoademon)
76+
* Most of the GameObjectFactory functions now have a group parameter, so you can do: game.add.sprite(x, y, frame, frameName, group) rather than defaulting to the World group.
77+
* Group.countLiving and countDead used to return -1 if the Group was empty, but now return 0.
78+
* Text can now be fixedToCamera, updated world/fixed to camera example to show this.
79+
7580

7681
Bug Fixes:
7782

@@ -203,11 +208,13 @@ Version 1.1.4 ("Kandor")
203208

204209
Versions 1.2 ("Saldaea")
205210

206-
* Integration with an advanced physics system. We've been experimenting with p2.js but have yet to conclude our research.
207-
* A more advanced Particle system, one that can render to a single canvas (rather than spawn hundreds of Sprites), more advanced effects, etc.
211+
* Integration with the p2.js physics system.
208212

209-
Version 1.2+
213+
Beyond version 1.2
210214

215+
* Dedicated CocoonJS packaging features (screencanvas, etc)
216+
* The ability to pass in a configuration option on Game boot, containing more advanced configuration features.
217+
* A more advanced Particle system, one that can render to a single canvas (rather than spawn hundreds of Sprites), more advanced effects, etc.
211218
* Massively enhance the audio side of Phaser. Although it does what it does well, it could do with taking more advantage of Web Audio - echo effects, positional sound, etc.
212219
* Comprehensive testing across Firefox OS devices, CocoonJS and Ejecta.
213220
* Integration with third party services like Google Play Game Services and Amazon JS SDK.
@@ -225,7 +232,7 @@ Version 1.2+
225232
Learn By Example
226233
----------------
227234

228-
Phaser comes with an ever growing suite of Examples. Personally I feel that we learn better by looking at small refined code examples, so we created over 150 of them and create new ones to test every new feature added. Inside the `examples` folder you'll find the current set. If you write a particularly good example then please send it to us.
235+
Phaser comes with an ever growing suite of Examples. Personally I feel that we learn better by looking at small refined code examples, so we created over 170 of them and create new ones to test every new feature added. Inside the `examples` folder you'll find the current set. If you write a particularly good example then please send it to us.
229236

230237
The examples need to be run through a local web server (in order to avoid file access permission errors from your browser). You can use your own web server, or start the included web server using grunt.
231238

examples/games/matching pairs.js

Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
1+
// mods by Patrick OReilly
2+
// Twitter: @pato_reilly Web: http://patricko.byethost9.com
3+
4+
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
5+
6+
function preload() {
7+
8+
game.load.tilemap('matching', 'assets/maps/phaser_tiles.json', null, Phaser.Tilemap.TILED_JSON);
9+
game.load.tileset('tiles', 'assets/tiles/phaser_tiles.png', 100, 100, -1, 1, 1);
10+
11+
}
12+
13+
var timeCheck = 0;
14+
var flipFlag = false;
15+
16+
var startList = new Array();
17+
var squareList = new Array();
18+
19+
var masterCounter = 0;
20+
var squareCounter = 0;
21+
var square1Num;
22+
var square2Num;
23+
var savedSquareX1;
24+
var savedSquareY1;
25+
var savedSquareX2;
26+
var savedSquareY2;
27+
28+
var map;
29+
var tileset;
30+
var layer;
31+
32+
var marker;
33+
var currentTile;
34+
var currentTilePosition;
35+
36+
var tileBack = 25;
37+
var timesUp = '+';
38+
var youWin = '+';
39+
40+
function create() {
41+
42+
map = game.add.tilemap('matching');
43+
44+
tileset = game.add.tileset('tiles');
45+
46+
layer = game.add.tilemapLayer(0, 0, 600, 600, tileset, map, 0);
47+
48+
marker = game.add.graphics();
49+
marker.lineStyle(2, 0x00FF00, 1);
50+
marker.drawRect(0, 0, 100, 100);
51+
52+
randomizeTiles();
53+
54+
}
55+
56+
function update() {
57+
58+
countDownTimer();
59+
60+
if (layer.getTileX(game.input.activePointer.worldX) <= 5) // to prevent the marker from going out of bounds
61+
{
62+
marker.x = layer.getTileX(game.input.activePointer.worldX) * 100;
63+
marker.y = layer.getTileY(game.input.activePointer.worldY) * 100;
64+
}
65+
66+
if (flipFlag == true)
67+
{
68+
if (game.time.now - timeCheck > 1000)
69+
{
70+
flipBack();
71+
}
72+
}
73+
else
74+
{
75+
processClick();
76+
}
77+
78+
}
79+
80+
function countDownTimer() {
81+
82+
var timeLimit = 120;
83+
84+
myTime = game.time.now;
85+
mySeconds = parseInt(myTime / 1000);
86+
myCountdownSeconds = timeLimit - mySeconds;
87+
88+
if (myCountdownSeconds <= 0)
89+
{
90+
// time is up
91+
timesUp = 'Time is up!';
92+
}
93+
94+
}
95+
96+
function processClick() {
97+
98+
currentTile = map.getTile(layer.getTileX(marker.x), layer.getTileY(marker.y));
99+
currentTilePosition = ((layer.getTileY(game.input.activePointer.worldY) + 1) * 6) - (6 - (layer.getTileX(game.input.activePointer.worldX) + 1));
100+
101+
if (game.input.mousePointer.isDown)
102+
{
103+
// check to make sure the tile is not already flipped
104+
if (currentTile == tileBack)
105+
{
106+
// get the corresponding item out of squareList
107+
currentNum = squareList[currentTilePosition - 1];
108+
flipOver();
109+
squareCounter++;
110+
111+
// is the second tile of pair flipped?
112+
if (squareCounter == 2)
113+
{
114+
// reset squareCounter
115+
squareCounter = 0;
116+
square2Num = currentNum;
117+
118+
// check for match
119+
if (square1Num == square2Num)
120+
{
121+
masterCounter++;
122+
123+
if (masterCounter == 18)
124+
{
125+
// go "win"
126+
youWin = 'Got them all!';
127+
}
128+
}
129+
else
130+
{
131+
savedSquareX2 = layer.getTileX(marker.x);
132+
savedSquareY2 = layer.getTileY(marker.y);
133+
flipFlag = true;
134+
timeCheck = game.time.now;
135+
}
136+
}
137+
else
138+
{
139+
savedSquareX1 = layer.getTileX(marker.x);
140+
savedSquareY1 = layer.getTileY(marker.y);
141+
square1Num = currentNum;
142+
}
143+
}
144+
}
145+
}
146+
147+
function flipOver() {
148+
149+
map.putTile(currentNum, layer.getTileX(marker.x), layer.getTileY(marker.y));
150+
}
151+
152+
function flipBack() {
153+
154+
flipFlag = false;
155+
156+
map.putTile(tileBack, savedSquareX1, savedSquareY1);
157+
map.putTile(tileBack, savedSquareX2, savedSquareY2);
158+
159+
}
160+
161+
function randomizeTiles() {
162+
163+
for (num = 1; num <= 18; num++)
164+
{
165+
startList.push(num);
166+
}
167+
168+
for (num = 1; num <= 18; num++)
169+
{
170+
startList.push(num);
171+
}
172+
173+
// for debugging
174+
myString1 = startList.toString();
175+
176+
// randomize squareList
177+
for (i = 1; i <= 36; i++)
178+
{
179+
randomPosition = game.rnd.integerInRange(0, startList.length);
180+
181+
thisNumber = startList[randomPosition];
182+
183+
squareList.push(thisNumber);
184+
185+
a = startList.indexOf(thisNumber);
186+
187+
startList.splice(a, 1);
188+
}
189+
190+
// for debugging
191+
myString2 = squareList.toString();
192+
193+
for (col = 0; col < 6; col++)
194+
{
195+
for (row = 0; row < 6; row++)
196+
{
197+
map.putTile(tileBack, col, row);
198+
}
199+
}
200+
}
201+
202+
function getHiddenTile() {
203+
204+
thisTile = squareList[currentTilePosition - 1];
205+
return thisTile;
206+
}
207+
208+
function render() {
209+
210+
game.debug.renderText(timesUp, 620, 208, 'rgb(0,255,0)');
211+
game.debug.renderText(youWin, 620, 240, 'rgb(0,255,0)');
212+
213+
game.debug.renderText('Time: ' + myCountdownSeconds, 620, 15, 'rgb(0,255,0)');
214+
game.debug.renderText('Matched Pairs: ' + masterCounter, 620, 304, 'rgb(0,0,255)');
215+
game.debug.renderText('Tile: ' + map.getTile(layer.getTileX(marker.x), layer.getTileY(marker.y)), 620, 48, 'rgb(255,0,0)');
216+
217+
game.debug.renderText('LayerX: ' + layer.getTileX(marker.x), 620, 80, 'rgb(255,0,0)');
218+
game.debug.renderText('LayerY: ' + layer.getTileY(marker.y), 620, 112, 'rgb(255,0,0)');
219+
220+
game.debug.renderText('Tile Position: ' + currentTilePosition, 620, 144, 'rgb(255,0,0)');
221+
game.debug.renderText('Hidden Tile: ' + getHiddenTile(), 620, 176, 'rgb(255,0,0)');
222+
223+
}

examples/wip/pivot.js

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,22 @@ function preload() {
44

55
game.load.image('atari', 'assets/sprites/atari130xe.png');
66
game.load.image('mushroom', 'assets/sprites/mushroom2.png');
7+
game.load.image('ball', 'assets/sprites/pangball.png');
78

89
}
910

1011
var sprite1;
11-
var sprite2;
12+
// var sprite2;
1213

1314
function create() {
1415

1516
game.stage.backgroundColor = '#2d2d2d';
1617

17-
// This will check Sprite vs. Sprite collision
18+
sprite1 = game.add.sprite(300, 300, 'ball');
1819

19-
sprite1 = game.add.sprite(300, 300, 'atari');
20-
sprite1.name = 'atari';
21-
sprite1.body.immovable = true;
22-
23-
sprite1.anchor.setTo(0.5, 0.5);
24-
sprite1.pivot.x = 250;
25-
sprite1.pivot.y = 300;
20+
sprite1.anchor.setTo(0, 1);
21+
sprite1.pivot.x = 100;
22+
sprite1.pivot.y = 100;
2623

2724
// sprite2 = game.add.sprite(0, 0, 'mushroom');
2825
// sprite2.name = 'mushroom';
@@ -32,8 +29,8 @@ function create() {
3229
function update() {
3330

3431
sprite1.angle += 1;
35-
sprite1.pivot.x = game.input.x;
36-
sprite1.pivot.y = game.input.y;
32+
// sprite1.pivot.x = game.input.x;
33+
// sprite1.pivot.y = game.input.y;
3734

3835
}
3936

@@ -42,7 +39,7 @@ function render() {
4239
game.debug.renderPixel(sprite1.pivot.x, sprite1.pivot.y);
4340

4441
// game.debug.renderSpriteInfo(sprite1, 100, 400);
45-
game.debug.renderSpriteBounds(sprite1);
42+
// game.debug.renderSpriteBounds(sprite1);
4643
// game.debug.renderSpriteInfo(sprite2, 100, 100);
4744
// game.debug.renderSpriteBounds(sprite2);
4845
}

examples/world/fixed to camera.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,29 @@ var logo2;
1818
function create() {
1919

2020
// Modify the world and camera bounds
21-
game.world.setBounds(-1000, -1000, 1000, 1000);
21+
game.world.setBounds(-1000, -1000, 2000, 2000);
2222

2323
for (var i = 0; i < 200; i++)
2424
{
2525
game.add.sprite(game.world.randomX, game.world.randomY, 'mushroom');
2626
}
2727

28-
logo1 = game.add.sprite(100, 100, 'phaser');
28+
game.add.text(0, 0, "this text scrolls\nwith the background", { font: "32px Arial", fill: "#f26c4f", align: "center" });
29+
30+
logo1 = game.add.sprite(0, 0, 'phaser');
2931
logo1.fixedToCamera = true;
32+
logo1.cameraOffset.setTo(100, 100);
3033

31-
logo2 = game.add.sprite(500, 100, 'phaser');
34+
logo2 = game.add.sprite(0, 0, 'phaser');
3235
logo2.fixedToCamera = true;
36+
logo2.cameraOffset.setTo(500, 100);
37+
38+
39+
var t = game.add.text(0, 0, "this text is fixed to the camera", { font: "32px Arial", fill: "#ffffff", align: "center" });
40+
t.fixedToCamera = true;
41+
t.cameraOffset.setTo(200, 500);
3342

34-
game.add.tween(logo2).to( { y: 400 }, 2000, Phaser.Easing.Back.InOut, true, 0, 2000, true);
43+
game.add.tween(logo2.cameraOffset).to( { y: 400 }, 2000, Phaser.Easing.Back.InOut, true, 0, 2000, true);
3544

3645
cursors = game.input.keyboard.createCursorKeys();
3746

0 commit comments

Comments
 (0)