-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
89 lines (72 loc) · 2.75 KB
/
game.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
window.onload = function () {
// Note that this html file is set to pull down Phaser 2.5.0 from the JS Delivr CDN.
// Although it will work fine with this tutorial, it's almost certainly not the most current version.
// Be sure to replace it with an updated version before you start experimenting with adding your own code.
var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload,
create: create,
update: update});
function preload () {
game.load.image('logo', '/sprites/Filthy.png');
game.load.spritesheet('hero', 'sprites/hero2.png', 16, 32);
game.load.audio('intro', 'music/intro.mp3');
game.load.audio('mellow', 'music/mellow.mp3');
game.load.audio('waves', 'music/waves.mp3')
}
var songs;
var currentSong = 0;
var intro;
var mellow;
var waves;
var introPlaying = true;
var cursors;
var jumpButton;
var hero;
function create () {
game.physics.startSystem(Phaser.Physics.ARCADE);
game.stage.backgroundColor = "#4488AA";
game.time.desiredFps = 30;
var logo = game.add.sprite(game.world.centerX, game.world.centerY, 'logo');
game.physics.arcade.gravity.y = 250;
hero = game.add.sprite(400, 300, 'hero');
game.physics.enable(hero, Phaser.Physics.ARCADE);
hero.body.collideWorldBounds = true;
//hero.body.velocity.setTo(200, 200);
hero.animations.add('test', [0, 1]);
hero.animations.play('test', 6, true);
//hero.body.gravity.set(800);
hero.body.bounce.y = 0.2;
cursors = game.input.keyboard.createCursorKeys();
jumpButton = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
//Music:
intro = game.add.audio('intro');
mellow = game.add.audio('mellow');
waves = game.add.audio('waves');
songs = [intro, mellow, waves];
intro.play();
game.input.onDown.add(nextSong, this);
logo.anchor.setTo(0.5, 0.5);
}
function update() {
hero.body.velocity.x = 0;
if (cursors.left.isDown){
hero.body.velocity.x = -150;
} else if (cursors.right.isDown) {
hero.body.velocity.x = 150;
} else {
//Stand still
}
if (jumpButton.isDown && hero.body.onFloor()) {
hero.body.velocity.y = -350;
}
}
function nextSong() {
songs[currentSong].stop();
if (currentSong >= 2) {
currentSong = 0;
songs[currentSong].play();
} else {
currentSong += 1;
songs[currentSong].play();
}
}
};