-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPlayer.js
110 lines (75 loc) · 2.35 KB
/
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
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
Player = function(game, x, y) {
this.blinking = false;
this.blinkCounter = 5;
this.blinkEndTime = 0;
this.isFrozen = false;
Phaser.Sprite.call(this, game, x, y, 'baddie_yellow');
this.anchor.setTo(0.5, 0.5);
this.animations.add('runleft',[0,1],5,true);
this.animations.add('runright',[2,3],5,true);
this.animations.play('runright');
game.physics.enable(this, Phaser.Physics.ARCADE);
this.body.collideWorldBounds = true;
//player.body.bounce.y = 0.2;
this.body.drag.setTo(1000,100);
this.body.maxVelocity.setTo(250,250);
this.body.gravity.y = 300;
this.body.setSize(28,28,0.5,0.5);
game.add.existing(this);
};
Player.prototype = Object.create(Phaser.Sprite.prototype);
Player.prototype.constructor = Player;
Player.prototype.update = function() {
if (this.isBlinking()) {
if (--this.blinkCounter == 0) {
this.renderable = !this.renderable;
this.blinkCounter = 5;
}
if (this.game.time.now > this.blinkEndTime) {
this.setBlinking(false);
this.renderable = true;
}
}
}
Player.prototype.updateMovement = function(cursors) {
this.body.acceleration.x = 0;
this.body.acceleration.y = 0;
if (this.isFrozen) {
this.body.velocity.setTo(0,0);
}
else {
if (cursors.left.isDown) {
this.body.acceleration.x = -1500;
if (this.animations.name != 'runleft') {
this.animations.play('runleft');
}
}
else
if (cursors.right.isDown) {
this.body.acceleration.x = 1500;
if (this.animations.name != 'runright') {
this.animations.play('runright');
}
}
if (cursors.up.isDown || jumpButton.isDown) {
this.body.acceleration.y = -1500;
}
if (cursors.down.isDown) {
this.body.acceleration.y = 1500;
}
}
}
Player.prototype.setBlinking = function(enable, time) {
this.blinking = enable;
if (enable) {
this.blinkCounter = 5;
time = time || 3000;
this.blinkEndTime = this.game.time.now + time;
}
};
Player.prototype.isBlinking = function() {
return this.blinking;
};
Player.prototype.setFrozen = function(enable) {
this.isFrozen = enable;
}