This repository has been archived by the owner on Jan 9, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
preloading.js
78 lines (66 loc) · 2.32 KB
/
preloading.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
'use strict';
// We create our own custom loader class extending Phaser.Loader.
// This new loader will support webfonts
function CustomLoader(game) {
Phaser.Loader.call(this, game);
}
CustomLoader.prototype = Object.create(Phaser.Loader.prototype);
CustomLoader.prototype.constructor = CustomLoader;
// new method to load webfonts
// this follows the structure of all of the file assets loading methods
CustomLoader.prototype.webfont = function (key, fontName, overwrite) {
if (typeof overwrite === 'undefined') { overwrite = false; }
// here fontName will be stored in file's `url` property
// after being added to the file list
this.addToFileList('webfont', key, fontName);
return this;
};
CustomLoader.prototype.loadFile = function (file) {
Phaser.Loader.prototype.loadFile.call(this, file);
// we need to call asyncComplete once the file has loaded
if (file.type === 'webfont') {
var _this = this;
// note: file.url contains font name
var font = new FontFaceObserver(file.url);
font.load(null, 10000).then(function () {
_this.asyncComplete(file);
}, function () {
_this.asyncComplete(file, 'Error loading font ' + file.url);
});
}
};
var PreloaderScene = {
init: function () {
// swap Phaser.Loader for our custom one
this.game.load = new CustomLoader(this.game);
},
preload: function () {
this.game.add.text(300, 150, 'Loading', {
font: '60px monospace',
fill: '#fff',
}).anchor.setTo(0.5);
this.game.load.webfont('fancy', 'Amatica SC');
this.game.load.image('background', 'background.png');
},
create: function () {
this.game.state.start('play');
}
};
var GameScene = {
create: function () {
this.game.add.image(0, 0, 'background');
var fancyStyle = { font: '30px "Amatica SC"', fill: '#fff'};
this.game.add.text(
this.game.world.width / 2,
this.game.world.height,
'Duelo a Garrotazos – Goya',
fancyStyle
).anchor.setTo(0.5, 1);
}
};
window.onload = function () {
var game = new Phaser.Game(625, 300, Phaser.AUTO, 'game');
game.state.add('preloader', PreloaderScene);
game.state.add('play', GameScene);
game.state.start('preloader');
};