11
2- var game = new Phaser . Game ( 800 , 600 , Phaser . CANVAS , 'phaser-example' , { preload : preload , create : create , update : update , render : render } ) ;
2+ var game = new Phaser . Game ( 800 , 600 , Phaser . CANVAS , 'phaser-example' , { preload : preload , create : create , update : update } ) ;
33
44function preload ( ) {
55
6- game . load . image ( 'phaser' , 'assets/sprites/phaser-dude.png' ) ;
7- game . load . image ( 'bullet' , 'assets/misc/bullet0.png' ) ;
8- game . load . image ( 'alien' , 'assets/sprites/space-baddie.png' ) ;
9- game . load . image ( 'ship' , 'assets/sprites/shmup-ship.png' ) ;
10- game . load . spritesheet ( 'kaboom' , 'assets/games/tanks/explosion.png' , 64 , 64 , 23 ) ;
11- game . load . image ( 'starfield' , 'assets/misc/starfield.jpg' ) ;
6+ game . load . image ( 'bullet' , 'assets/games/invaders/bullet.png' ) ;
7+ game . load . image ( 'enemyBullet' , 'assets/games/invaders/enemy-bullet.png' ) ;
8+ game . load . spritesheet ( 'invader' , 'assets/games/invaders/invader32x32x4.png' , 32 , 32 ) ;
9+ game . load . image ( 'ship' , 'assets/games/invaders/player.png' ) ;
10+ game . load . spritesheet ( 'kaboom' , 'assets/games/invaders/explode.png' , 128 , 128 ) ;
11+ game . load . image ( 'starfield' , 'assets/games/invaders/starfield.png' ) ;
12+ game . load . image ( 'background' , 'assets/games/starstruck/background2.png' ) ;
1213
1314}
1415
@@ -19,68 +20,79 @@ var bulletTime = 0;
1920var cursors ;
2021var fireButton ;
2122var explosions ;
22-
23- function loadUpdate ( ) {
24-
25- console . log ( 'state loadUpdate' ) ;
26-
27- }
23+ var starfield ;
2824
2925function create ( ) {
3026
27+ // The scrolling starfield background
28+ starfield = game . add . tileSprite ( 0 , 0 , 800 , 600 , 'starfield' ) ;
29+ // game.add.tileSprite(0, 0, 800, 600, 'background');
3130
32- s = game . add . tileSprite ( 0 , 0 , 800 , 600 , 'starfield' ) ;
31+ // Our bullet group
32+ bullets = game . add . group ( ) ;
33+ bullets . createMultiple ( 30 , 'bullet' ) ;
34+ bullets . setAll ( 'anchor.x' , 0.5 ) ;
35+ bullets . setAll ( 'anchor.y' , 1 ) ;
36+ bullets . setAll ( 'outOfBoundsKill' , true ) ;
3337
38+ // The hero!
3439 player = game . add . sprite ( 400 , 500 , 'ship' ) ;
3540 player . anchor . setTo ( 0.5 , 0.5 ) ;
3641
37- aliens = game . add . group ( null , 'aliens' ) ;
42+ // The baddies!
43+ aliens = game . add . group ( ) ;
3844
3945 for ( var y = 0 ; y < 4 ; y ++ )
4046 {
4147 for ( var x = 0 ; x < 10 ; x ++ )
4248 {
43- aliens . create ( x * 48 , y * 50 , 'alien' ) ;
49+ var alien = aliens . create ( x * 48 , y * 50 , 'invader' ) ;
50+ alien . animations . add ( 'fly' , [ 0 , 1 , 2 , 3 ] , 20 , true ) ;
51+ alien . play ( 'fly' ) ;
4452 }
4553 }
4654
4755 aliens . x = 100 ;
4856 aliens . y = 50 ;
4957
50- // Our bullet group
51- bullets = game . add . group ( ) ;
52- bullets . createMultiple ( 30 , 'bullet' ) ;
53- bullets . setAll ( 'anchor.x' , 0.5 ) ;
54- bullets . setAll ( 'anchor.y' , 1 ) ;
55- bullets . setAll ( 'outOfBoundsKill' , true ) ;
56-
57- // Explosion pool
58+ // An explosion pool
5859 explosions = game . add . group ( ) ;
60+ explosions . createMultiple ( 30 , 'kaboom' ) ;
61+ explosions . forEach ( setupInvader , this ) ;
5962
60- for ( var i = 0 ; i < 10 ; i ++ )
61- {
62- var explosionAnimation = explosions . create ( 0 , 0 , 'kaboom' , [ 0 ] , false ) ;
63- explosionAnimation . anchor . setTo ( 0.5 , 0.5 ) ;
64- explosionAnimation . animations . add ( 'kaboom' ) ;
65- }
63+ // All this does is basically start the invaders moving. Notice we're move the Group they belong to, rather than the invaders directly.
64+ var tween = game . add . tween ( aliens ) . to ( { x : 200 } , 2000 , Phaser . Easing . Linear . None , true , 0 , 1000 , true ) ;
6665
67- var tween = game . add . tween ( aliens ) . to ( { x : 200 } , 3000 , Phaser . Easing . Linear . None , true , 0 , 1000 , true ) ;
66+ // When the tween completes it calls descend, before looping again
6867 tween . onComplete . add ( descend , this ) ;
6968
69+ // And some controls to play the game with
7070 cursors = game . input . keyboard . createCursorKeys ( ) ;
7171 fireButton = game . input . keyboard . addKey ( Phaser . Keyboard . SPACEBAR ) ;
7272
7373}
7474
75+ function setupInvader ( invader ) {
76+
77+ invader . anchor . x = 0.5 ;
78+ invader . anchor . y = 0.5 ;
79+ invader . animations . add ( 'kaboom' ) ;
80+
81+ }
7582
7683function descend ( ) {
84+
7785 aliens . y += 10 ;
86+
7887}
7988
8089function update ( ) {
8190
82- player . body . velocity . x = 0 ;
83- player . body . velocity . y = 0 ;
91+ // Scroll the background
92+ starfield . tilePosition . y += 2 ;
93+
94+ // Reset the player, then check for movement keys
95+ player . body . velocity . setTo ( 0 , 0 ) ;
8496
8597 if ( cursors . left . isDown )
8698 {
@@ -91,46 +103,54 @@ function update() {
91103 player . body . velocity . x = 200 ;
92104 }
93105
106+ // Firing?
94107 if ( fireButton . isDown )
95108 {
96109 fireBullet ( ) ;
97110 }
98111
112+ // Run collision
99113 game . physics . collide ( bullets , aliens , collisionHandler , null , this ) ;
100114
101115}
102116
103117function fireBullet ( ) {
104118
119+ // To avoid them being allowed to fire too fast we set a time limit
105120 if ( game . time . now > bulletTime )
106121 {
122+ // Grab the first bullet we can from the pool
107123 bullet = bullets . getFirstExists ( false ) ;
108124
109125 if ( bullet )
110126 {
111- bullet . reset ( player . x , player . y - 8 ) ;
112- bullet . body . velocity . y = - 300 ;
113- bulletTime = game . time . now + 250 ;
127+ // And fire it
128+ bullet . reset ( player . x , player . y + 8 ) ;
129+ bullet . body . velocity . y = - 400 ;
130+ bulletTime = game . time . now + 200 ;
114131 }
115132 }
116133
117134}
118135
119- // Called if the bullet goes out of the screen
120136function resetBullet ( bullet ) {
137+
138+ // Called if the bullet goes out of the screen
121139 bullet . kill ( ) ;
140+
122141}
123142
124143function collisionHandler ( bullet , alien ) {
125144
145+ // When a bullet hits an alien we kill them both
126146 bullet . kill ( ) ;
127147 alien . kill ( ) ;
128148
129- var explosionAnimation = explosions . getFirstDead ( ) ;
130- explosionAnimation . reset ( alien . body . x , alien . body . y ) ;
131- explosionAnimation . play ( 'kaboom' , 30 , false , true ) ;
149+ // Increase the score
132150
133- }
151+ // And create an explosion :)
152+ var explosion = explosions . getFirstDead ( ) ;
153+ explosion . reset ( alien . body . x , alien . body . y ) ;
154+ explosion . play ( 'kaboom' , 30 , false , true ) ;
134155
135- function render ( ) {
136156}
0 commit comments