forked from astro/b-runner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.js
51 lines (32 loc) · 773 Bytes
/
player.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
var Player = function() {
this.x = 160;
this.y = 0;
this.dx = 0;
this.dy = 0;
this.radius = 10;
this.frame = 0;
this.anim = 0;
};
Player.prototype.draw = function() {
this.frame = (this.frame + 1) % 3;
if(!this.frame) { this.anim = (this.anim + 1) % 3; }
var a = 3;
if(this.dy) { a = 4; }
else if(this.dx) { a = this.anim; }
drawSprite('player' + a, this.x - 9, this.y - 11);
};
Player.prototype.update = function() {
if(this.dy == 0) { // when on ground
if(keys[88]) this.dy = -5; // jump
}
this.dx = (keys[39] - keys[37]) * 3;
this.dy += 0.17;
this.x += this.dx;
this.y += this.dy;
if(this.y > 450) { // collision
this.y = 450;
this.dy = 0;
}
cameraX += (this.x - cameraX) / 10;
cameraY += (this.y - 50 - cameraY) / 10;
};