11
2- var game = new Phaser . Game ( 800 , 600 , Phaser . CANVAS , 'phaser-example' , { preload : preload , create : create , update : update } ) ;
2+ var game = new Phaser . Game ( 800 , 600 , Phaser . AUTO , 'phaser-example' , { preload : preload , create : create , update : update } ) ;
33
44function preload ( ) {
55
@@ -21,12 +21,12 @@ var cursors;
2121var fireButton ;
2222var explosions ;
2323var starfield ;
24- var score = 0 ;
25- var scoreString = '' ;
24+ var score = 0 ;
25+ var scoreString = '' ;
2626var scoreText ;
27- var lives ;
27+ var lives ;
2828var enemyBullet ;
29- var firingTimer = 0 ;
29+ var firingTimer = 0 ;
3030var stateText ;
3131
3232function create ( ) {
@@ -54,34 +54,33 @@ function create() {
5454 player . anchor . setTo ( 0.5 , 0.5 ) ;
5555
5656 // The baddies!
57-
5857 aliens = game . add . group ( ) ;
5958
6059 createAliens ( ) ;
6160
6261 // The score :
63-
64- scoreString = 'Score : ' ;
65- scoreText = game . add . text ( 10 , 10 , scoreString + score , { fontSize : '34px' , fill : '#fff' } ) ;
62+ scoreString = 'Score : ' ;
63+ scoreText = game . add . text ( 10 , 10 , scoreString + score , { fontSize : '34px' , fill : '#fff' } ) ;
6664
6765 // Lives
68- lives = game . add . group ( ) ;
66+ lives = game . add . group ( ) ;
6967 game . add . text ( game . world . width - 100 , 10 , 'Lives : ' , { fontSize : '34px' , fill : '#fff' } ) ;
7068
7169 //state text
72- stateText = game . add . text ( game . world . centerX , game . world . centerY , '' , { fontSize : '84px' , fill : '#fff' } ) ;
70+ stateText = game . add . text ( game . world . centerX , game . world . centerY , '' , { fontSize : '84px' , fill : '#fff' } ) ;
7371 stateText . anchor . setTo ( 0.5 , 0.5 ) ;
7472 stateText . visible = false ;
7573
7674
7775
78- for ( var i = 0 ; i < 3 ; i ++ ) {
76+ for ( var i = 0 ; i < 3 ; i ++ )
77+ {
7978
80- var ship = lives . create ( game . world . width - 100 + ( 30 * i ) , 60 , 'ship' ) ;
79+ var ship = lives . create ( game . world . width - 100 + ( 30 * i ) , 60 , 'ship' ) ;
8180 ship . anchor . setTo ( 0.5 , 0.5 ) ;
82- ship . angle = 90 ;
83- ship . alpha = 0.4 ;
84- } ;
81+ ship . angle = 90 ;
82+ ship . alpha = 0.4 ;
83+ }
8584
8685 // An explosion pool
8786 explosions = game . add . group ( ) ;
@@ -95,7 +94,8 @@ function create() {
9594}
9695
9796function createAliens ( ) {
98- for ( var y = 0 ; y < 4 ; y ++ )
97+
98+ for ( var y = 0 ; y < 4 ; y ++ )
9999 {
100100 for ( var x = 0 ; x < 10 ; x ++ )
101101 {
@@ -153,7 +153,8 @@ function update() {
153153 fireBullet ( ) ;
154154 }
155155
156- if ( game . time . now > firingTimer ) {
156+ if ( game . time . now > firingTimer )
157+ {
157158 enemyFires ( ) ;
158159 }
159160
@@ -164,66 +165,88 @@ function update() {
164165
165166}
166167
167- function enemyHitsPlayer ( player , bullet ) {
168-
169- bullet . kill ( ) ;
170-
171- live = lives . getFirstAlive ( ) ;
168+ function collisionHandler ( bullet , alien ) {
172169
173- if ( live ) {
174- live . kill ( ) ;
175- }
170+ // When a bullet hits an alien we kill them both
171+ bullet . kill ( ) ;
172+ alien . kill ( ) ;
176173
174+ // Increase the score
175+ score += 20 ;
176+ scoreText . content = scoreString + score ;
177177
178178 // And create an explosion :)
179179 var explosion = explosions . getFirstDead ( ) ;
180- explosion . reset ( player . body . x , player . body . y ) ;
180+ explosion . reset ( alien . body . x , alien . body . y ) ;
181181 explosion . play ( 'kaboom' , 30 , false , true ) ;
182182
183- if ( lives . countLiving ( ) < 1 ) {
183+ if ( aliens . countLiving ( ) == 0 )
184+ {
184185
185- player . kill ( ) ;
186- enemyBullets . callAll ( 'kill' ) ;
187- stateText . content = " GAME OVER \n Click to restart" ;
188- stateText . visible = true ;
186+ score += 1000 ;
187+ scoreText . content = scoreString + score ;
189188
190- game . input . onTap . addOnce ( restart , this ) ;
189+ enemyBullets . callAll ( 'kill' , this ) ;
190+ stateText . content = " You Won, \n Click to restart" ;
191+ stateText . visible = true ;
191192
193+ //the "click to restart" handler
194+ game . input . onTap . addOnce ( restart , this ) ;
192195
196+
193197 }
194198
195199}
196200
197- function restart ( ) {
201+ function enemyHitsPlayer ( player , bullet ) {
198202
203+ bullet . kill ( ) ;
199204
200- lives . callAll ( 'revive' ) ;
201- // And brings the aliens back from the dead :)
202- aliens . removeAll ( ) ;
205+ live = lives . getFirstAlive ( ) ;
203206
204- createAliens ( ) ;
207+ if ( live )
208+ {
209+ live . kill ( ) ;
210+ }
205211
206- player . revive ( ) ;
207212
208- stateText . visible = false ;
213+ // And create an explosion :)
214+ var explosion = explosions . getFirstDead ( ) ;
215+ explosion . reset ( player . body . x , player . body . y ) ;
216+ explosion . play ( 'kaboom' , 30 , false , true ) ;
217+
218+ // When the player dies
219+ if ( lives . countLiving ( ) < 1 )
220+ {
221+
222+ player . kill ( ) ;
223+ enemyBullets . callAll ( 'kill' ) ;
224+
225+ stateText . content = " GAME OVER \n Click to restart" ;
226+ stateText . visible = true ;
227+
228+ //the "click to restart" handler
229+ game . input . onTap . addOnce ( restart , this ) ;
209230
210231
232+ }
233+
211234}
212235
213236function enemyFires ( ) {
214237
215- // Grab the first bullet we can from the pool
216- enemyBullet = enemyBullets . getFirstExists ( false ) ;
217-
218- if ( enemyBullet ) {
238+ // Grab the first bullet we can from the pool
239+ enemyBullet = enemyBullets . getFirstExists ( false ) ;
219240
220- var shooter = aliens . getRandom ( ) ;
221- // And fire it
222- enemyBullet . reset ( shooter . body . x , shooter . body . y ) ;
223- // enemyBullet.body.velocity.y = 400;
224- game . physics . moveToObject ( enemyBullet , player , 120 ) ;
225- firingTimer = game . time . now + 2000 ;
226- }
241+ if ( enemyBullet )
242+ {
243+ var shooter = aliens . getRandom ( ) ;
244+ // And fire it
245+ enemyBullet . reset ( shooter . body . x , shooter . body . y ) ;
246+ // enemyBullet.body.velocity.y = 400;
247+ game . physics . moveToObject ( enemyBullet , player , 120 ) ;
248+ firingTimer = game . time . now + 2000 ;
249+ }
227250}
228251
229252function fireBullet ( ) {
@@ -252,34 +275,19 @@ function resetBullet (bullet) {
252275
253276}
254277
255- function collisionHandler ( bullet , alien ) {
256-
257- // When a bullet hits an alien we kill them both
258- bullet . kill ( ) ;
259- alien . kill ( ) ;
260-
261- // Increase the score
262- score += 20 ;
263- scoreText . content = scoreString + score ;
264-
265- // And create an explosion :)
266- var explosion = explosions . getFirstDead ( ) ;
267- explosion . reset ( alien . body . x , alien . body . y ) ;
268- explosion . play ( 'kaboom' , 30 , false , true ) ;
269-
270- if ( aliens . countLiving ( ) == 0 ) {
271- // New level starts
272- score += 1000 ;
273- scoreText . content = scoreString + score ;
274-
275- enemyBullets . callAll ( 'kill' , this ) ;
276- stateText . content = " You Won, \n Click to restart" ;
277- stateText . visible = true ;
278-
279- game . input . onTap . addOnce ( restart , this ) ;
278+ function restart ( ) {
280279
281-
280+ // A new level starts
281+
282+ //resets the life count
283+ lives . callAll ( 'revive' ) ;
284+ // And brings the aliens back from the dead :)
285+ aliens . removeAll ( ) ;
286+ createAliens ( ) ;
282287
283- }
288+ //revives the player
289+ player . revive ( ) ;
290+ //hides the text
291+ stateText . visible = false ;
284292
285293}
0 commit comments