-
Notifications
You must be signed in to change notification settings - Fork 6
/
character.js
101 lines (83 loc) · 1.81 KB
/
character.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
game.addEventListener("load", function(){
// Get a character
character = new Sprite(32, 32)
character.image = game.assets['images/girl.gif']
character.frame = 0
character.x = 100
character.y = 100
game.rootScene.addChild(character)
speed = 2
function movement(){
x = this.x
y = this.y
if (game.input.left) {
x -= 1 * speed
character.frame = 3
}
if (game.input.right) {
x += 1 * speed
character.frame = 6
}
if (game.input.down) {
y += 1 * speed
character.frame = 0
}
if (game.input.up) {
y -= 1 * speed
character.frame = 9
}
// Did our character run into something?
var top = y
var bottom = y + 32
var left = x
var right = x + 32
if ( map.hitTest(left, top) ) {
if (this.x != x) {
x += speed
}
if (this.y != y){
y += speed
}
}
if ( map.hitTest(right, top) ) {
if (this.x != x) {
x -= speed
}
if (this.y != y){
y += speed
}
}
if ( map.hitTest(left, bottom) ) {
if (this.x != x) {
x += speed
}
if (this.y != y){
y -= speed
}
}
if ( map.hitTest(right, bottom) ) {
if (this.x != x) {
x -= speed
}
if (this.y != y){
y -= speed
}
}
// Set character.blocked = true to stop moving when colliding when objects
if (character.blocked) {
x = this.x + 2 * (this.x - x)
y = this.y + 2 * (this.y - y)
setTimeout(function() {
character.blocked = false
}, 50)
}
this.x = x
this.y = y
}
character.addEventListener("enterframe", movement)
function resetCharacter() {
character.x = 100
character.y = 100
}
game.rootScene.addEventListener("reset", resetCharacter)
})