Skip to content

Commit 712858c

Browse files
committed
More work on the Invaders game.
1 parent ddf1597 commit 712858c

14 files changed

Lines changed: 147 additions & 54 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ Version 1.1.2
4949
* Updated: Fixed the Star Struck game sample and enhanced it.
5050
* Updated: If you pause an Animation, when you next play it it'll resume (un-pause itself).
5151
* Updated: hexToRGB now accepts short hex codes (#EEE) (thanks beeglebug)
52+
* Updated: State functions (preload, update, render, etc) are now passed the current game as a parameter (thanks beeglebug)
53+
* Updated: If your game is running in Canvas (not WebGL) you can now set Stage.backgroundColor to rgba style CSS strings, allowing for semi-transparent game backgrounds.
5254
* Fixed issue 135 - Added typeof checks into most ArcadePhysics functions to avoid errors with zero values.
5355
* Fixed issue 136 - distanceTo using worldX/Y instead of x/y.
5456
* Fixed lots of examples where the cursor keys / space bar were not locked from moving the browser page (if you find any more, please tell us!)
2.82 KB
Loading
222 Bytes
Loading
18.3 KB
Loading
456 Bytes
Loading
1.09 KB
Loading
749 Bytes
Loading
331 KB
Loading

examples/games/invaders.js

Lines changed: 62 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
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

44
function 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;
1920
var cursors;
2021
var fireButton;
2122
var explosions;
22-
23-
function loadUpdate() {
24-
25-
console.log('state loadUpdate');
26-
27-
}
23+
var starfield;
2824

2925
function 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

7683
function descend() {
84+
7785
aliens.y += 10;
86+
7887
}
7988

8089
function 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

103117
function 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
120136
function resetBullet (bullet) {
137+
138+
// Called if the bullet goes out of the screen
121139
bullet.kill();
140+
122141
}
123142

124143
function 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
}

examples/wip/forum.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
var mainMenu = {
2+
3+
preload: function () {
4+
5+
// game.load.image('bg', 'assets/bg.png');
6+
// game.load.image('mainstart', 'assets/mainstart.png');
7+
game.load.image('bg', 'assets/pics/louie-inga.png');
8+
game.load.image('mainstart', 'assets/pics/contra2.png');
9+
10+
},
11+
12+
create: function () {
13+
14+
game.add.sprite(0, 0, 'bg');
15+
16+
var mainstart = game.add.sprite(0, 0, 'mainstart');
17+
mainstart.name = "mainstart";
18+
mainstart.inputEnabled = true;
19+
mainstart.events.onInputDown.add(this.listener, this);
20+
21+
},
22+
23+
listener: function ()
24+
{
25+
game.state.start('levelMenu', true, true);
26+
}
27+
28+
}
29+
30+
var levelSelect = {
31+
32+
preload: function () {
33+
},
34+
35+
create: function () {
36+
37+
game.add.sprite(0, 0, 'bg');
38+
39+
}
40+
41+
}
42+
43+
var game = new Phaser.Game(640, 480);
44+
45+
game.state.add('menu', mainMenu, true);
46+
game.state.add('levelMenu', levelSelect);
47+

0 commit comments

Comments
 (0)