11// mods by Patrick OReilly
2- // Twitter : @pato_reilly Web: http://patricko.byethost9.com
2+ // twitter : @pato_reilly
33
44var game = new Phaser . Game ( 800 , 600 , Phaser . CANVAS , 'phaser-example' , { preload : preload , create : create , update : update , render : render } ) ;
55
66function preload ( ) {
77
8- game . load . tilemap ( 'matching' , 'assets/maps/phaser_tiles.json' , null , Phaser . Tilemap . TILED_JSON ) ;
9- game . load . image ( 'tiles' , 'assets/tiles/phaser_tiles.png' ) ;
10-
8+ game . load . tilemap ( 'matching' , 'assets/tilemaps/maps/phaser_tiles.json' , null , Phaser . Tilemap . TILED_JSON ) ;
9+ game . load . tileset ( 'tiles' , 'assets/tilemaps/tiles/phaser_tiles.png' , 100 , 100 , - 1 , 1 , 1 ) ;
1110}
1211
1312var timeCheck = 0 ;
@@ -37,159 +36,158 @@ var tileBack = 25;
3736var timesUp = '+' ;
3837var youWin = '+' ;
3938
40- function create ( ) {
4139
42- map = game . add . tilemap ( 'matching' ) ;
40+ function create ( ) {
4341
44- // tileset = game.add.tileset('tiles ');
42+ map = game . add . tilemap ( 'matching ') ;
4543
46- // layer = game.add.tilemapLayer(0, 0, 600, 600, tileset, map, 0);
44+ tileset = game . add . tileset ( 'tiles' ) ;
45+
46+ layer = game . add . tilemapLayer ( 0 , 0 , 600 , 600 , tileset , map , 0 ) ;
4747
48- marker = game . add . graphics ( ) ;
49- marker . lineStyle ( 2 , 0x00FF00 , 1 ) ;
50- marker . drawRect ( 0 , 0 , 100 , 100 ) ;
48+ //layer.resizeWorld();
5149
50+ marker = game . add . graphics ( ) ;
51+ marker . lineStyle ( 2 , 0x00FF00 , 1 ) ;
52+ marker . drawRect ( 0 , 0 , 100 , 100 ) ;
53+
5254 randomizeTiles ( ) ;
5355
5456}
5557
5658function update ( ) {
57-
59+
5860 countDownTimer ( ) ;
59-
61+
6062 if ( layer . getTileX ( game . input . activePointer . worldX ) <= 5 ) // to prevent the marker from going out of bounds
6163 {
6264 marker . x = layer . getTileX ( game . input . activePointer . worldX ) * 100 ;
6365 marker . y = layer . getTileY ( game . input . activePointer . worldY ) * 100 ;
6466 }
6567
66- if ( flipFlag == true )
68+ if ( flipFlag == true )
6769 {
68- if ( game . time . now - timeCheck > 1000 )
69- {
70+ if ( game . time . now - timeCheck > 1000 )
71+ {
7072 flipBack ( ) ;
7173 }
7274 }
7375 else
7476 {
7577 processClick ( ) ;
7678 }
77-
7879}
79-
80+
81+
8082function countDownTimer ( ) {
81-
83+
8284 var timeLimit = 120 ;
83-
85+
8486 myTime = game . time . now ;
85- mySeconds = parseInt ( myTime / 1000 ) ;
87+ mySeconds = parseInt ( myTime / 1000 ) ;
8688 myCountdownSeconds = timeLimit - mySeconds ;
87-
88- if ( myCountdownSeconds <= 0 )
89- {
89+
90+ if ( myCountdownSeconds <= 0 )
91+ {
9092 // time is up
91- timesUp = 'Time is up!' ;
93+ timesUp = 'Time is up!' ;
9294 }
93-
9495}
9596
9697function processClick ( ) {
97-
98+
9899 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-
100+ currentTilePosition = ( ( layer . getTileY ( game . input . activePointer . worldY ) + 1 ) * 6 ) - ( 6 - ( layer . getTileX ( game . input . activePointer . worldX ) + 1 ) ) ;
101+
101102 if ( game . input . mousePointer . isDown )
102- {
103+ {
103104 // check to make sure the tile is not already flipped
104- if ( currentTile == tileBack )
105+ if ( currentTile == tileBack )
105106 {
106107 // get the corresponding item out of squareList
107- currentNum = squareList [ currentTilePosition - 1 ] ;
108+ currentNum = squareList [ currentTilePosition - 1 ] ;
108109 flipOver ( ) ;
109- squareCounter ++ ;
110-
110+ squareCounter ++ ;
111111 // is the second tile of pair flipped?
112- if ( squareCounter == 2 )
112+ if ( squareCounter == 2 )
113113 {
114114 // reset squareCounter
115115 squareCounter = 0 ;
116116 square2Num = currentNum ;
117-
118117 // check for match
119118 if ( square1Num == square2Num )
120119 {
121- masterCounter ++ ;
122-
123- if ( masterCounter == 18 )
120+ masterCounter ++ ;
121+
122+ if ( masterCounter == 18 )
124123 {
125124 // go "win"
126125 youWin = 'Got them all!' ;
127- }
126+ }
128127 }
129128 else
130129 {
131130 savedSquareX2 = layer . getTileX ( marker . x ) ;
132131 savedSquareY2 = layer . getTileY ( marker . y ) ;
133- flipFlag = true ;
134- timeCheck = game . time . now ;
135- }
136- }
132+ flipFlag = true ;
133+ timeCheck = game . time . now ;
134+ }
135+ }
137136 else
138137 {
139138 savedSquareX1 = layer . getTileX ( marker . x ) ;
140139 savedSquareY1 = layer . getTileY ( marker . y ) ;
141- square1Num = currentNum ;
142- }
143- }
144- }
140+ square1Num = currentNum ;
141+ }
142+ }
143+ }
145144}
146-
145+
147146function flipOver ( ) {
148-
147+
149148 map . putTile ( currentNum , layer . getTileX ( marker . x ) , layer . getTileY ( marker . y ) ) ;
150149}
151-
150+
152151function flipBack ( ) {
153-
152+
154153 flipFlag = false ;
155-
154+
156155 map . putTile ( tileBack , savedSquareX1 , savedSquareY1 ) ;
157156 map . putTile ( tileBack , savedSquareX2 , savedSquareY2 ) ;
158-
157+
159158}
160-
159+
161160function randomizeTiles ( ) {
162161
163162 for ( num = 1 ; num <= 18 ; num ++ )
164163 {
165164 startList . push ( num ) ;
166165 }
167-
168166 for ( num = 1 ; num <= 18 ; num ++ )
169167 {
170168 startList . push ( num ) ;
171169 }
172170
173171 // for debugging
174172 myString1 = startList . toString ( ) ;
175-
173+
176174 // randomize squareList
177- for ( i = 1 ; i <= 36 ; i ++ )
175+ for ( i = 1 ; i <= 36 ; i ++ )
178176 {
179- randomPosition = game . rnd . integerInRange ( 0 , startList . length ) ;
180-
181- thisNumber = startList [ randomPosition ] ;
182-
177+ randomPosition = game . rnd . integerInRange ( 0 , startList . length ) ;
178+
179+ thisNumber = startList [ randomPosition ] ;
180+
183181 squareList . push ( thisNumber ) ;
184-
182+
185183 a = startList . indexOf ( thisNumber ) ;
186-
187- startList . splice ( a , 1 ) ;
184+
185+ startList . splice ( a , 1 ) ;
188186 }
189-
187+
190188 // for debugging
191189 myString2 = squareList . toString ( ) ;
192-
190+
193191 for ( col = 0 ; col < 6 ; col ++ )
194192 {
195193 for ( row = 0 ; row < 6 ; row ++ )
@@ -200,8 +198,8 @@ function randomizeTiles() {
200198}
201199
202200function getHiddenTile ( ) {
203-
204- thisTile = squareList [ currentTilePosition - 1 ] ;
201+
202+ thisTile = squareList [ currentTilePosition - 1 ] ;
205203 return thisTile ;
206204}
207205
@@ -211,13 +209,18 @@ function render() {
211209 game . debug . renderText ( youWin , 620 , 240 , 'rgb(0,255,0)' ) ;
212210
213211 game . debug . renderText ( 'Time: ' + myCountdownSeconds , 620 , 15 , 'rgb(0,255,0)' ) ;
212+
213+ //game.debug.renderText('squareCounter: ' + squareCounter, 620, 272, 'rgb(0,0,255)');
214214 game . debug . renderText ( 'Matched Pairs: ' + masterCounter , 620 , 304 , 'rgb(0,0,255)' ) ;
215+
216+ //game.debug.renderText('startList: ' + myString1, 620, 208, 'rgb(255,0,0)');
217+ //game.debug.renderText('squareList: ' + myString2, 620, 240, 'rgb(255,0,0)');
218+
215219 game . debug . renderText ( 'Tile: ' + map . getTile ( layer . getTileX ( marker . x ) , layer . getTileY ( marker . y ) ) , 620 , 48 , 'rgb(255,0,0)' ) ;
216220
217221 game . debug . renderText ( 'LayerX: ' + layer . getTileX ( marker . x ) , 620 , 80 , 'rgb(255,0,0)' ) ;
218222 game . debug . renderText ( 'LayerY: ' + layer . getTileY ( marker . y ) , 620 , 112 , 'rgb(255,0,0)' ) ;
219223
220224 game . debug . renderText ( 'Tile Position: ' + currentTilePosition , 620 , 144 , 'rgb(255,0,0)' ) ;
221225 game . debug . renderText ( 'Hidden Tile: ' + getHiddenTile ( ) , 620 , 176 , 'rgb(255,0,0)' ) ;
222-
223- }
226+ }
0 commit comments