-
Notifications
You must be signed in to change notification settings - Fork 0
/
8-Text.html
169 lines (140 loc) · 4.53 KB
/
8-Text.html
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title> Building your first Phaser 3 Game</title>
<script src="//cdn.jsdelivr.net/npm/phaser@3.11.0/dist/phaser.js"></script>
<style type="text/css">
body { margin: 0; }
canvas { display: block; margin: auto; }
</style>
</head>
<body>
<script type="text/javascript">
var config = {
type: Phaser.AUTO,
width: 800,
height: 576,
backgroundColor: '#2d2d2d',
pixelArt: true,
physics: {
default: 'arcade',
arcade: {
gravity: { y: 300 }
}
},
scene: {
preload: preload,
create: create,
update: update
}
};
var game = new Phaser.Game(config);
var sprite;
var player;
var map;
var groundLayer;
var coinLayer;
var cursors;
var coinsCollected = 0;
/***** New *****/
var text;
/***************/
function preload ()
{
this.load.image('staticPlayer', 'assets/sprites/dude.png');
this.load.spritesheet('animatedPlayer', 'assets/sprites/animated-dude.png', { frameWidth: 32, frameHeight: 48 });
this.load.image('ground_1x1', 'assets/tilemaps/tiles/ground_1x1.png');
this.load.spritesheet('coin', 'assets/sprites/coin.png', { frameWidth: 32, frameHeight: 32 });
this.load.tilemapTiledJSON('map', 'assets/tilemaps/maps/tile-collision-test.json');
}
function create ()
{
sprite = this.physics.add.sprite(100, 450, 'staticPlayer');
player = this.physics.add.sprite(200, 450, 'animatedPlayer');
this.anims.create({
key: 'left',
frames: this.anims.generateFrameNumbers('animatedPlayer', { start: 0, end: 3 }),
frameRate: 10,
repeat: -1
});
this.anims.create({
key: 'idle',
frames: [ { key: 'animatedPlayer', frame: 4 } ],
frameRate: 20
});
this.anims.create({
key: 'right',
frames: this.anims.generateFrameNumbers('animatedPlayer', { start: 5, end: 8 }),
frameRate: 10,
repeat: -1
});
player.anims.play('right', true);
map = this.make.tilemap({ key: 'map' });
var groundTiles = map.addTilesetImage('ground_1x1');
var coinTiles = map.addTilesetImage('coin');
map.createDynamicLayer('Background Layer', groundTiles, 0, 0);
groundLayer = map.createDynamicLayer('Ground Layer', groundTiles, 0, 0);
coinLayer = map.createDynamicLayer('Coin Layer', coinTiles, 0, 0);
cursors = this.input.keyboard.createCursorKeys();
this.cameras.main.setBounds(0, 0, map.widthInPixels, map.heightInPixels);
this.cameras.main.startFollow(player);
groundLayer.setCollisionBetween(1, 25);
this.physics.add.collider(player, groundLayer);
this.physics.add.overlap(player, coinLayer);
this.physics.add.collider(sprite, groundLayer);
this.physics.add.overlap(sprite, coinLayer);
coinLayer.setTileIndexCallback(26, hitCoin, this);
/***** New *****/
/*** Text setup ***/
text = this.add.text(16, 16, '', {
fontSize: '20px',
fill: '#ffffff'
});
updateText();
text.setScrollFactor(0);
/***************/
}
function update (time, delta)
{
player.body.setVelocityX(0);
if (cursors.left.isDown)
{
player.body.setVelocityX(-200);
player.anims.play('left', true);
}
else if (cursors.right.isDown)
{
player.body.setVelocityX(200);
player.anims.play('right', true);
}
else {
player.anims.play('idle', true);
}
if ((cursors.space.isDown || cursors.up.isDown) && player.body.onFloor())
{
player.body.setVelocityY(-300);
}
}
function hitCoin (sprite, tile)
{
coinLayer.removeTileAt(tile.x, tile.y);
coinsCollected += 1;
/***** New *****/
updateText();
/***************/
return false;
}
/***** New *****/
function updateText ()
{
text.setText(
'Arrow keys to move. Space to jump'
+ '\nCoins collected: ' + coinsCollected + '/28'
// NOTE: The \n above is a special character that makes the following text go on a new line
);
}
/***************/
</script>
</body>
</html>