Grid Gunner: Book demo game
|
@ -58,26 +58,23 @@ function create() {
|
|||
|
||||
createAliens();
|
||||
|
||||
// The score :
|
||||
// The score
|
||||
scoreString = 'Score : ';
|
||||
scoreText = game.add.text(10,10,scoreString+score,{fontSize : '34px',fill : '#fff'});
|
||||
scoreText = game.add.text(10, 10, scoreString + score, { fontSize: '34px', fill: '#fff' });
|
||||
|
||||
// Lives
|
||||
// Lives
|
||||
lives = game.add.group();
|
||||
game.add.text(game.world.width-100,10,'Lives : ',{fontSize : '34px',fill : '#fff'});
|
||||
game.add.text(game.world.width - 100, 10, 'Lives : ', { fontSize: '34px', fill: '#fff' });
|
||||
|
||||
//state text
|
||||
stateText = game.add.text(game.world.centerX,game.world.centerY,'',{fontSize : '84px',fill : '#fff'});
|
||||
stateText.anchor.setTo(0.5,0.5);
|
||||
stateText.visible=false;
|
||||
|
||||
|
||||
// Text
|
||||
stateText = game.add.text(game.world.centerX,game.world.centerY,'', { fontSize: '84px', fill: '#fff' });
|
||||
stateText.anchor.setTo(0.5, 0.5);
|
||||
stateText.visible = false;
|
||||
|
||||
for (var i = 0; i < 3; i++)
|
||||
{
|
||||
|
||||
var ship = lives.create(game.world.width-100+(30*i),60,'ship');
|
||||
ship.anchor.setTo(0.5,0.5);
|
||||
var ship = lives.create(game.world.width - 100 + (30 * i), 60, 'ship');
|
||||
ship.anchor.setTo(0.5, 0.5);
|
||||
ship.angle = 90;
|
||||
ship.alpha = 0.4;
|
||||
}
|
||||
|
@ -90,7 +87,6 @@ function create() {
|
|||
// And some controls to play the game with
|
||||
cursors = game.input.keyboard.createCursorKeys();
|
||||
fireButton = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
|
||||
|
||||
}
|
||||
|
||||
function createAliens () {
|
||||
|
@ -100,8 +96,8 @@ function createAliens () {
|
|||
for (var x = 0; x < 10; x++)
|
||||
{
|
||||
var alien = aliens.create(x * 48, y * 50, 'invader');
|
||||
alien.anchor.setTo(0.5,0.5);
|
||||
alien.animations.add('fly', [0,1,2,3], 20, true);
|
||||
alien.anchor.setTo(0.5, 0.5);
|
||||
alien.animations.add('fly', [ 0, 1, 2, 3 ], 20, true);
|
||||
alien.play('fly');
|
||||
}
|
||||
}
|
||||
|
@ -110,7 +106,7 @@ function createAliens () {
|
|||
aliens.y = 50;
|
||||
|
||||
// All this does is basically start the invaders moving. Notice we're moving the Group they belong to, rather than the invaders directly.
|
||||
var tween = game.add.tween(aliens).to({x: 200}, 2000, Phaser.Easing.Linear.None, true, 0, 1000, true);
|
||||
var tween = game.add.tween(aliens).to( { x: 200 }, 2000, Phaser.Easing.Linear.None, true, 0, 1000, true);
|
||||
|
||||
// When the tween completes it calls descend, before looping again
|
||||
tween.onComplete.add(descend, this);
|
||||
|
@ -153,7 +149,7 @@ function update() {
|
|||
fireBullet();
|
||||
}
|
||||
|
||||
if(game.time.now > firingTimer)
|
||||
if (game.time.now > firingTimer)
|
||||
{
|
||||
enemyFires();
|
||||
}
|
||||
|
@ -162,7 +158,6 @@ function update() {
|
|||
game.physics.collide(bullets, aliens, collisionHandler, null, this);
|
||||
game.physics.collide(enemyBullets, player, enemyHitsPlayer, null, this);
|
||||
|
||||
|
||||
}
|
||||
|
||||
function collisionHandler (bullet, alien) {
|
||||
|
@ -182,7 +177,6 @@ function collisionHandler (bullet, alien) {
|
|||
|
||||
if (aliens.countLiving() == 0)
|
||||
{
|
||||
|
||||
score += 1000;
|
||||
scoreText.content = scoreString + score;
|
||||
|
||||
|
@ -192,8 +186,6 @@ function collisionHandler (bullet, alien) {
|
|||
|
||||
//the "click to restart" handler
|
||||
game.input.onTap.addOnce(restart,this);
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -204,21 +196,19 @@ function enemyHitsPlayer (player,bullet) {
|
|||
|
||||
live = lives.getFirstAlive();
|
||||
|
||||
if(live)
|
||||
if (live)
|
||||
{
|
||||
live.kill();
|
||||
}
|
||||
|
||||
|
||||
// And create an explosion :)
|
||||
var explosion = explosions.getFirstDead();
|
||||
explosion.reset(player.body.x, player.body.y);
|
||||
explosion.play('kaboom', 30, false, true);
|
||||
|
||||
// When the player dies
|
||||
if(lives.countLiving() < 1)
|
||||
if (lives.countLiving() < 1)
|
||||
{
|
||||
|
||||
player.kill();
|
||||
enemyBullets.callAll('kill');
|
||||
|
||||
|
@ -227,8 +217,6 @@ function enemyHitsPlayer (player,bullet) {
|
|||
|
||||
//the "click to restart" handler
|
||||
game.input.onTap.addOnce(restart,this);
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -247,6 +235,7 @@ function enemyFires () {
|
|||
game.physics.moveToObject(enemyBullet,player,120);
|
||||
firingTimer = game.time.now + 2000;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function fireBullet () {
|
||||
|
|
BIN
examples/wip/book/audio/explode.wav
Normal file
BIN
examples/wip/book/audio/shot.wav
Normal file
BIN
examples/wip/book/audio/title.mp3
Normal file
BIN
examples/wip/book/images/bullet.png
Normal file
After Width: | Height: | Size: 156 B |
BIN
examples/wip/book/images/explode.png
Normal file
After Width: | Height: | Size: 7.7 KiB |
BIN
examples/wip/book/images/invader32x32x4.png
Normal file
After Width: | Height: | Size: 548 B |
BIN
examples/wip/book/images/landscape.png
Normal file
After Width: | Height: | Size: 140 KiB |
BIN
examples/wip/book/images/logo.png
Normal file
After Width: | Height: | Size: 168 KiB |
Before Width: | Height: | Size: 78 KiB |
BIN
examples/wip/book/images/robofont.png
Normal file
After Width: | Height: | Size: 927 B |
100
examples/wip/book/images/robofont.xml
Normal file
|
@ -0,0 +1,100 @@
|
|||
<?xml version="1.0"?>
|
||||
<font>
|
||||
<info face="AtomicRoboKid" size="64" bold="0" italic="0" charset="" unicode="1" stretchH="100" smooth="0" aa="1" padding="0,0,0,0" spacing="1,1" outline="0"/>
|
||||
<common lineHeight="64" base="56" scaleW="512" scaleH="512" pages="1" packed="0" alphaChnl="1" redChnl="0" greenChnl="0" blueChnl="0"/>
|
||||
<pages>
|
||||
<page id="0" file="robofont_0.png" />
|
||||
</pages>
|
||||
<chars count="90">
|
||||
<char id="32" x="504" y="0" width="3" height="1" xoffset="-1" yoffset="63" xadvance="63" page="0" chnl="15" />
|
||||
<char id="33" x="472" y="280" width="31" height="55" xoffset="24" yoffset="1" xadvance="63" page="0" chnl="15" />
|
||||
<char id="34" x="178" y="392" width="23" height="24" xoffset="16" yoffset="1" xadvance="63" page="0" chnl="15" />
|
||||
<char id="39" x="145" y="392" width="16" height="31" xoffset="8" yoffset="1" xadvance="63" page="0" chnl="15" />
|
||||
<char id="40" x="465" y="336" width="16" height="47" xoffset="16" yoffset="9" xadvance="63" page="0" chnl="15" />
|
||||
<char id="41" x="482" y="336" width="16" height="47" xoffset="16" yoffset="9" xadvance="63" page="0" chnl="15" />
|
||||
<char id="43" x="48" y="392" width="39" height="39" xoffset="16" yoffset="1" xadvance="63" page="0" chnl="15" />
|
||||
<char id="44" x="162" y="392" width="15" height="31" xoffset="24" yoffset="17" xadvance="63" page="0" chnl="15" />
|
||||
<char id="45" x="242" y="392" width="39" height="16" xoffset="8" yoffset="24" xadvance="63" page="0" chnl="15" />
|
||||
<char id="46" x="282" y="392" width="15" height="16" xoffset="24" yoffset="32" xadvance="63" page="0" chnl="15" />
|
||||
<char id="47" x="0" y="392" width="47" height="47" xoffset="8" yoffset="9" xadvance="63" page="0" chnl="15" />
|
||||
<char id="48" x="0" y="0" width="55" height="55" xoffset="0" yoffset="1" xadvance="63" page="0" chnl="15" />
|
||||
<char id="49" x="449" y="336" width="15" height="55" xoffset="24" yoffset="1" xadvance="63" page="0" chnl="15" />
|
||||
<char id="50" x="224" y="56" width="55" height="55" xoffset="0" yoffset="1" xadvance="63" page="0" chnl="15" />
|
||||
<char id="51" x="280" y="56" width="55" height="55" xoffset="0" yoffset="1" xadvance="63" page="0" chnl="15" />
|
||||
<char id="52" x="336" y="56" width="55" height="55" xoffset="0" yoffset="1" xadvance="63" page="0" chnl="15" />
|
||||
<char id="53" x="392" y="56" width="55" height="55" xoffset="0" yoffset="1" xadvance="63" page="0" chnl="15" />
|
||||
<char id="54" x="448" y="56" width="55" height="55" xoffset="0" yoffset="1" xadvance="63" page="0" chnl="15" />
|
||||
<char id="55" x="336" y="336" width="47" height="55" xoffset="8" yoffset="1" xadvance="63" page="0" chnl="15" />
|
||||
<char id="56" x="56" y="112" width="55" height="55" xoffset="0" yoffset="1" xadvance="63" page="0" chnl="15" />
|
||||
<char id="57" x="328" y="280" width="47" height="55" xoffset="0" yoffset="1" xadvance="63" page="0" chnl="15" />
|
||||
<char id="58" x="128" y="392" width="16" height="39" xoffset="16" yoffset="9" xadvance="63" page="0" chnl="15" />
|
||||
<char id="59" x="432" y="336" width="16" height="55" xoffset="16" yoffset="9" xadvance="63" page="0" chnl="15" />
|
||||
<char id="61" x="202" y="392" width="39" height="23" xoffset="8" yoffset="17" xadvance="63" page="0" chnl="15" />
|
||||
<char id="63" x="336" y="112" width="55" height="55" xoffset="0" yoffset="1" xadvance="63" page="0" chnl="15" />
|
||||
<char id="64" x="392" y="112" width="55" height="55" xoffset="0" yoffset="1" xadvance="63" page="0" chnl="15" />
|
||||
<char id="65" x="448" y="112" width="55" height="55" xoffset="0" yoffset="1" xadvance="63" page="0" chnl="15" />
|
||||
<char id="66" x="0" y="168" width="55" height="55" xoffset="0" yoffset="1" xadvance="63" page="0" chnl="15" />
|
||||
<char id="67" x="56" y="168" width="55" height="55" xoffset="0" yoffset="1" xadvance="63" page="0" chnl="15" />
|
||||
<char id="68" x="112" y="168" width="55" height="55" xoffset="0" yoffset="1" xadvance="63" page="0" chnl="15" />
|
||||
<char id="69" x="48" y="336" width="47" height="55" xoffset="0" yoffset="1" xadvance="63" page="0" chnl="15" />
|
||||
<char id="70" x="224" y="168" width="55" height="55" xoffset="0" yoffset="1" xadvance="63" page="0" chnl="15" />
|
||||
<char id="71" x="280" y="168" width="55" height="55" xoffset="0" yoffset="1" xadvance="63" page="0" chnl="15" />
|
||||
<char id="72" x="336" y="168" width="55" height="55" xoffset="0" yoffset="1" xadvance="63" page="0" chnl="15" />
|
||||
<char id="73" x="384" y="336" width="23" height="55" xoffset="16" yoffset="1" xadvance="63" page="0" chnl="15" />
|
||||
<char id="74" x="96" y="336" width="47" height="55" xoffset="0" yoffset="1" xadvance="63" page="0" chnl="15" />
|
||||
<char id="75" x="0" y="224" width="55" height="55" xoffset="0" yoffset="1" xadvance="63" page="0" chnl="15" />
|
||||
<char id="76" x="144" y="336" width="47" height="55" xoffset="0" yoffset="1" xadvance="63" page="0" chnl="15" />
|
||||
<char id="77" x="112" y="224" width="55" height="55" xoffset="0" yoffset="1" xadvance="63" page="0" chnl="15" />
|
||||
<char id="78" x="192" y="336" width="47" height="55" xoffset="8" yoffset="1" xadvance="63" page="0" chnl="15" />
|
||||
<char id="79" x="224" y="224" width="55" height="55" xoffset="0" yoffset="1" xadvance="63" page="0" chnl="15" />
|
||||
<char id="80" x="280" y="224" width="55" height="55" xoffset="0" yoffset="1" xadvance="63" page="0" chnl="15" />
|
||||
<char id="81" x="336" y="224" width="55" height="55" xoffset="0" yoffset="1" xadvance="63" page="0" chnl="15" />
|
||||
<char id="82" x="392" y="224" width="55" height="55" xoffset="0" yoffset="1" xadvance="63" page="0" chnl="15" />
|
||||
<char id="83" x="448" y="224" width="55" height="55" xoffset="0" yoffset="1" xadvance="63" page="0" chnl="15" />
|
||||
<char id="84" x="392" y="168" width="55" height="55" xoffset="0" yoffset="1" xadvance="63" page="0" chnl="15" />
|
||||
<char id="85" x="56" y="280" width="55" height="55" xoffset="0" yoffset="1" xadvance="63" page="0" chnl="15" />
|
||||
<char id="86" x="112" y="280" width="55" height="55" xoffset="0" yoffset="1" xadvance="63" page="0" chnl="15" />
|
||||
<char id="87" x="168" y="280" width="55" height="55" xoffset="0" yoffset="1" xadvance="63" page="0" chnl="15" />
|
||||
<char id="88" x="224" y="280" width="55" height="55" xoffset="0" yoffset="1" xadvance="63" page="0" chnl="15" />
|
||||
<char id="89" x="288" y="336" width="47" height="55" xoffset="0" yoffset="1" xadvance="63" page="0" chnl="15" />
|
||||
<char id="90" x="56" y="0" width="55" height="55" xoffset="0" yoffset="1" xadvance="63" page="0" chnl="15" />
|
||||
<char id="95" x="298" y="392" width="55" height="8" xoffset="0" yoffset="48" xadvance="63" page="0" chnl="15" />
|
||||
<char id="97" x="112" y="0" width="55" height="55" xoffset="0" yoffset="1" xadvance="63" page="0" chnl="15" />
|
||||
<char id="98" x="168" y="0" width="55" height="55" xoffset="0" yoffset="1" xadvance="63" page="0" chnl="15" />
|
||||
<char id="99" x="224" y="0" width="55" height="55" xoffset="0" yoffset="1" xadvance="63" page="0" chnl="15" />
|
||||
<char id="100" x="112" y="56" width="55" height="55" xoffset="0" yoffset="1" xadvance="63" page="0" chnl="15" />
|
||||
<char id="101" x="0" y="336" width="47" height="55" xoffset="0" yoffset="1" xadvance="63" page="0" chnl="15" />
|
||||
<char id="102" x="280" y="0" width="55" height="55" xoffset="0" yoffset="1" xadvance="63" page="0" chnl="15" />
|
||||
<char id="103" x="336" y="0" width="55" height="55" xoffset="0" yoffset="1" xadvance="63" page="0" chnl="15" />
|
||||
<char id="104" x="392" y="0" width="55" height="55" xoffset="0" yoffset="1" xadvance="63" page="0" chnl="15" />
|
||||
<char id="105" x="408" y="336" width="23" height="55" xoffset="16" yoffset="1" xadvance="63" page="0" chnl="15" />
|
||||
<char id="106" x="280" y="280" width="47" height="55" xoffset="0" yoffset="1" xadvance="63" page="0" chnl="15" />
|
||||
<char id="107" x="448" y="0" width="55" height="55" xoffset="0" yoffset="1" xadvance="63" page="0" chnl="15" />
|
||||
<char id="108" x="424" y="280" width="47" height="55" xoffset="0" yoffset="1" xadvance="63" page="0" chnl="15" />
|
||||
<char id="109" x="0" y="56" width="55" height="55" xoffset="0" yoffset="1" xadvance="63" page="0" chnl="15" />
|
||||
<char id="110" x="376" y="280" width="47" height="55" xoffset="8" yoffset="1" xadvance="63" page="0" chnl="15" />
|
||||
<char id="111" x="56" y="56" width="55" height="55" xoffset="0" yoffset="1" xadvance="63" page="0" chnl="15" />
|
||||
<char id="112" x="168" y="56" width="55" height="55" xoffset="0" yoffset="1" xadvance="63" page="0" chnl="15" />
|
||||
<char id="113" x="0" y="112" width="55" height="55" xoffset="0" yoffset="1" xadvance="63" page="0" chnl="15" />
|
||||
<char id="114" x="112" y="112" width="55" height="55" xoffset="0" yoffset="1" xadvance="63" page="0" chnl="15" />
|
||||
<char id="115" x="168" y="112" width="55" height="55" xoffset="0" yoffset="1" xadvance="63" page="0" chnl="15" />
|
||||
<char id="116" x="224" y="112" width="55" height="55" xoffset="0" yoffset="1" xadvance="63" page="0" chnl="15" />
|
||||
<char id="117" x="280" y="112" width="55" height="55" xoffset="0" yoffset="1" xadvance="63" page="0" chnl="15" />
|
||||
<char id="118" x="168" y="168" width="55" height="55" xoffset="0" yoffset="1" xadvance="63" page="0" chnl="15" />
|
||||
<char id="119" x="448" y="168" width="55" height="55" xoffset="0" yoffset="1" xadvance="63" page="0" chnl="15" />
|
||||
<char id="120" x="56" y="224" width="55" height="55" xoffset="0" yoffset="1" xadvance="63" page="0" chnl="15" />
|
||||
<char id="121" x="240" y="336" width="47" height="55" xoffset="0" yoffset="1" xadvance="63" page="0" chnl="15" />
|
||||
<char id="122" x="0" y="280" width="55" height="55" xoffset="0" yoffset="1" xadvance="63" page="0" chnl="15" />
|
||||
<char id="160" x="508" y="0" width="3" height="1" xoffset="-1" yoffset="63" xadvance="63" page="0" chnl="15" />
|
||||
<char id="169" x="168" y="224" width="55" height="55" xoffset="0" yoffset="1" xadvance="63" page="0" chnl="15" />
|
||||
<char id="215" x="88" y="392" width="39" height="39" xoffset="16" yoffset="9" xadvance="63" page="0" chnl="15" />
|
||||
<char id="119155" x="504" y="2" width="3" height="1" xoffset="-1" yoffset="63" xadvance="63" page="0" chnl="15" />
|
||||
<char id="119156" x="508" y="2" width="3" height="1" xoffset="-1" yoffset="63" xadvance="63" page="0" chnl="15" />
|
||||
<char id="119157" x="504" y="4" width="3" height="1" xoffset="-1" yoffset="63" xadvance="63" page="0" chnl="15" />
|
||||
<char id="119158" x="508" y="4" width="3" height="1" xoffset="-1" yoffset="63" xadvance="63" page="0" chnl="15" />
|
||||
<char id="119159" x="504" y="6" width="3" height="1" xoffset="-1" yoffset="63" xadvance="63" page="0" chnl="15" />
|
||||
<char id="119160" x="508" y="6" width="3" height="1" xoffset="-1" yoffset="63" xadvance="63" page="0" chnl="15" />
|
||||
<char id="119161" x="504" y="8" width="3" height="1" xoffset="-1" yoffset="63" xadvance="63" page="0" chnl="15" />
|
||||
<char id="119162" x="508" y="8" width="3" height="1" xoffset="-1" yoffset="63" xadvance="63" page="0" chnl="15" />
|
||||
</chars>
|
||||
</font>
|
BIN
examples/wip/book/images/ship.png
Normal file
After Width: | Height: | Size: 299 B |
BIN
examples/wip/book/images/sky.png
Normal file
After Width: | Height: | Size: 1.3 KiB |
319
examples/wip/book/part2.html
Normal file
|
@ -0,0 +1,319 @@
|
|||
<!DOCTYPE HTML>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<title>Creating a game in Phaser, part 2</title>
|
||||
<style>
|
||||
body {
|
||||
background-color: #000;
|
||||
margin: 0;
|
||||
}
|
||||
</style>
|
||||
<script src="phaser.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<div id="gameContainer"></div>
|
||||
|
||||
<script type="text/javascript">
|
||||
|
||||
window.onload = function() {
|
||||
|
||||
var game = new Phaser.Game(1024, 672, Phaser.CANVAS, 'gameContainer', { preload: preload, create: create, update: update });
|
||||
|
||||
var sky;
|
||||
var land;
|
||||
var logo;
|
||||
|
||||
var player;
|
||||
var bullets;
|
||||
var aliens;
|
||||
var explosions;
|
||||
|
||||
var started = false;
|
||||
var alienSpeed = 200;
|
||||
var alienReleaseRate = 1000;
|
||||
var alienReleaseQuantity = 3;
|
||||
|
||||
var scrollSpeed = 8;
|
||||
var alienTimer = 0;
|
||||
var bulletTime = 0;
|
||||
|
||||
var cursors;
|
||||
var fireButton;
|
||||
var score = 0;
|
||||
var text;
|
||||
|
||||
var music; // note: music by teque of aggression from the Atari STE version of Stardust
|
||||
var sfxLazer;
|
||||
var sfxExplode;
|
||||
|
||||
function preload () {
|
||||
|
||||
game.load.image('background', 'images/sky.png');
|
||||
game.load.image('land', 'images/landscape.png');
|
||||
game.load.image('logo', 'images/logo.png');
|
||||
game.load.image('player', 'images/ship.png');
|
||||
game.load.image('bullet', 'images/bullet.png');
|
||||
game.load.spritesheet('kaboom', 'images/explode.png', 128, 128);
|
||||
game.load.spritesheet('alien', 'images/invader32x32x4.png', 32, 32);
|
||||
game.load.bitmapFont('robofont', 'images/robofont.png', 'images/robofont.xml');
|
||||
|
||||
game.load.audio('music', [ 'audio/title.mp3']);
|
||||
game.load.audio('sfxLazer', [ 'audio/shot.wav']);
|
||||
game.load.audio('sfxExplode', [ 'audio/explode.wav']);
|
||||
|
||||
}
|
||||
|
||||
function create () {
|
||||
|
||||
game.world.setBounds(-64, 0, 1300, 672);
|
||||
|
||||
sfxLazer = game.add.audio('sfxLazer', 0.1, false);
|
||||
sfxExplode = game.add.audio('sfxExplode', 0.5, false);
|
||||
|
||||
music = game.add.audio('music');
|
||||
|
||||
sky = game.add.sprite(0, 0, 'background');
|
||||
land = game.add.tileSprite(0, 544, 1024, 128, 'land');
|
||||
|
||||
bullets = game.add.group();
|
||||
bullets.createMultiple(30, 'bullet');
|
||||
bullets.setAll('outOfBoundsKill', true);
|
||||
|
||||
// The aliens
|
||||
aliens = game.add.group();
|
||||
aliens.createMultiple(50, 'alien');
|
||||
aliens.setAll('outOfBoundsKill', true);
|
||||
aliens.callAll('animations.add', 'animations', 'fly', [ 0, 1, 2, 3 ], 20, true);
|
||||
|
||||
player = game.add.sprite(64, 256, 'player');
|
||||
player.body.collideWorldBounds = true;
|
||||
player.visible = false;
|
||||
|
||||
// An explosion pool
|
||||
explosions = game.add.group();
|
||||
explosions.createMultiple(30, 'kaboom');
|
||||
explosions.callAll('animations.add', 'animations', 'kaboom');
|
||||
explosions.setAll('anchor.x', 0.5);
|
||||
explosions.setAll('anchor.y', 0.5);
|
||||
|
||||
text = game.add.bitmapText(8, 8, 'click to start', { font: '16px AtomicRoboKid' });
|
||||
text.alpha = 0.9;
|
||||
|
||||
logo = game.add.sprite(0, 24, 'logo');
|
||||
|
||||
// And some controls to play the game with
|
||||
cursors = game.input.keyboard.createCursorKeys();
|
||||
fireButton = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
|
||||
|
||||
sky.alpha = 0;
|
||||
land.alpha = 0;
|
||||
logo.alpha = 0;
|
||||
|
||||
showTitle();
|
||||
|
||||
}
|
||||
|
||||
function showTitle () {
|
||||
|
||||
game.input.onUp.add(hideTitle, this);
|
||||
|
||||
game.add.tween(logo).to( { alpha: 1 }, 2000, Phaser.Easing.Linear.None, true);
|
||||
game.add.tween(sky).to( { alpha: 1 }, 3000, Phaser.Easing.Linear.None, true);
|
||||
game.add.tween(land).to( { alpha: 1 }, 1000, Phaser.Easing.Linear.None, true);
|
||||
|
||||
logo.visible = true;
|
||||
|
||||
started = false;
|
||||
|
||||
music.play('', 0, 1, true);
|
||||
|
||||
}
|
||||
|
||||
function hideTitle () {
|
||||
|
||||
game.input.onUp.remove(hideTitle, this);
|
||||
|
||||
var tween = game.add.tween(logo).to( { alpha: 0 }, 1000, Phaser.Easing.Linear.None, true);
|
||||
tween.onComplete.add(hideLogo, this);
|
||||
|
||||
game.add.tween(sky).to( { alpha: 0.4 }, 2000, Phaser.Easing.Linear.None, true);
|
||||
|
||||
player.reset(-64, 256);
|
||||
player.visible = true;
|
||||
|
||||
game.add.tween(player).to( { x: 64 }, 1000, Phaser.Easing.Linear.None, true);
|
||||
|
||||
score = 0;
|
||||
text.setText('score: ' + score.toString());
|
||||
|
||||
alienSpeed = 200;
|
||||
alienReleaseRate = 1000;
|
||||
alienReleaseQuantity = 3;
|
||||
alienTimer = 0;
|
||||
bulletTime = 0;
|
||||
|
||||
started = true;
|
||||
|
||||
music.stop();
|
||||
|
||||
}
|
||||
|
||||
function hideLogo () {
|
||||
|
||||
logo.visible = false;
|
||||
|
||||
}
|
||||
|
||||
function update () {
|
||||
|
||||
land.tilePosition.x -= scrollSpeed;
|
||||
|
||||
if (started == false)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Reset the player, then check for movement keys
|
||||
player.body.velocity.setTo(0, 0);
|
||||
|
||||
if (cursors.up.isDown)
|
||||
{
|
||||
player.body.velocity.y = -400;
|
||||
}
|
||||
else if (cursors.down.isDown)
|
||||
{
|
||||
player.body.velocity.y = 400;
|
||||
}
|
||||
|
||||
if (cursors.left.isDown)
|
||||
{
|
||||
player.body.velocity.x = -400;
|
||||
}
|
||||
else if (cursors.right.isDown)
|
||||
{
|
||||
player.body.velocity.x = 400;
|
||||
}
|
||||
|
||||
// Firing?
|
||||
if (fireButton.isDown)
|
||||
{
|
||||
fireBullet();
|
||||
}
|
||||
|
||||
// Release a new alien?
|
||||
if (game.time.now > alienTimer)
|
||||
{
|
||||
releaseAliens();
|
||||
}
|
||||
|
||||
// Run collision
|
||||
game.physics.collide(bullets, aliens, collisionHandler, null, this);
|
||||
game.physics.collide(aliens, player, alienHitsPlayer, null, this);
|
||||
|
||||
}
|
||||
|
||||
function releaseAliens () {
|
||||
|
||||
for (var i = 0; i < alienReleaseQuantity; i++)
|
||||
{
|
||||
alien = aliens.getFirstExists(false);
|
||||
|
||||
if (alien)
|
||||
{
|
||||
alien.reset(game.rnd.integerInRange(1024, 1200), game.rnd.integerInRange(200, 400));
|
||||
alien.body.velocity.x = -alienSpeed;
|
||||
alien.body.velocity.y = game.rnd.integerInRange(-100, 100);
|
||||
alien.play('fly');
|
||||
}
|
||||
}
|
||||
|
||||
alienTimer = game.time.now + alienReleaseRate;
|
||||
|
||||
}
|
||||
|
||||
function alienHitsPlayer (alien, player) {
|
||||
|
||||
alien.kill();
|
||||
player.kill();
|
||||
|
||||
sfxExplode.play('', 0, 1);
|
||||
|
||||
// And create an explosion :)
|
||||
var explosion = explosions.getFirstDead();
|
||||
explosion.reset(player.x, player.y);
|
||||
explosion.play('kaboom', 30, false, true);
|
||||
|
||||
showTitle();
|
||||
|
||||
}
|
||||
|
||||
function fireBullet () {
|
||||
|
||||
// To avoid them being allowed to fire too fast we set a time limit
|
||||
if (game.time.now > bulletTime)
|
||||
{
|
||||
// Grab the first bullet we can from the pool
|
||||
bullet = bullets.getFirstExists(false);
|
||||
|
||||
if (bullet)
|
||||
{
|
||||
// And fire it
|
||||
bullet.reset(player.x + 8, player.y + 10);
|
||||
bullet.body.velocity.x = 700;
|
||||
bulletTime = game.time.now + 100;
|
||||
sfxLazer.play('', 0, 0.1);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function collisionHandler (bullet, alien) {
|
||||
|
||||
// Increase the score
|
||||
score += 20;
|
||||
text.setText('score: ' + score.toString());
|
||||
|
||||
// For every alien we kill we're going to increase their speed slightly and reduce the release rate
|
||||
|
||||
if (alienReleaseRate > 250)
|
||||
{
|
||||
alienReleaseRate -= 2;
|
||||
}
|
||||
|
||||
if (alienSpeed < 600)
|
||||
{
|
||||
alienSpeed += 2;
|
||||
}
|
||||
|
||||
if (alienReleaseRate % 300 == 0)
|
||||
{
|
||||
alienReleaseQuantity++;
|
||||
}
|
||||
|
||||
// And create an explosion :)
|
||||
var explosion = explosions.getFirstDead();
|
||||
|
||||
if (explosion)
|
||||
{
|
||||
explosion.reset(alien.x + 8, alien.y + 16);
|
||||
explosion.body.velocity.x = 400;
|
||||
explosion.body.velocity.y = 10;
|
||||
explosion.play('kaboom', 30, false, true);
|
||||
|
||||
sfxExplode.play('', 0, 0.5);
|
||||
}
|
||||
|
||||
// When a bullet hits an alien we kill both sprites, putting them back into the pool
|
||||
bullet.kill();
|
||||
alien.kill();
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
</script>
|
||||
|
||||
</body>
|
||||
</html>
|