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
|
'use strict';
var Player = function(game, x, y, key, frame, controls) {
Phaser.Sprite.call(this, game, x, y, key, frame);
this.moving = false;
this.game.input.keyboard.addKeyCapture([
controls.up,
controls.down,
controls.left,
controls.right
]);
this.game.input.keyboard.addKey(controls.up).onDown.add(this.moveUp, this);
this.game.input.keyboard.addKey(controls.down).onDown.add(this.moveDown, this);
this.game.input.keyboard.addKey(controls.left).onDown.add(this.moveLeft, this);
this.game.input.keyboard.addKey(controls.right).onDown.add(this.moveRight, this);
};
Player.prototype = Object.create(Phaser.Sprite.prototype);
Player.prototype.constructor = Player;
Player.prototype.update = function() {
};
Player.prototype.moveUp = function() {
this.move(0, -100);
};
Player.prototype.moveDown = function() {
this.move(0, 100);
};
Player.prototype.moveLeft = function() {
this.move(-100, 0);
};
Player.prototype.moveRight = function() {
this.move(100, 0);
};
Player.prototype.move = function(deltaX, deltaY) {
if (this.moving) {
return;
}
var newX = this.x + deltaX;
var newY = this.y + deltaY;
if (!this.canMoveToNewLocation(newX, newY)) {
return;
}
this.moving = true;
var tween = this.game.add.tween(this).to({x: newX, y: newY}, 500);
tween.onComplete.add(this.finishMovement, this);
tween.start();
};
Player.prototype.finishMovement = function() {
this.moving = false;
};
Player.prototype.canMoveToNewLocation = function(newX, newY) {
return true;
};
module.exports = Player;
|