-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmovement.js
147 lines (141 loc) · 4.62 KB
/
movement.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
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
var square = document.getElementById("square");
var movementRight = 0;
var movementJump = 0;
var idMovement = setInterval(voidFunction, 20000);
var idJump;
var jumpMove = false;
var ticks = 0;
var stopMovement;
var rightMove = false;
var leftMove = false;
var button;
function voidFunction(){}
document.body.addEventListener("keydown", movement);
document.body.addEventListener("keyup", stopMovementFunction);
function movement(e){
if(e.key == "ArrowRight" || e.key == "ArrowLeft") // Related with the control of the player input
button = e.key //Better control of the stop or continue movement of the square
if(e.key == "ArrowRight" && !rightMove){
console.log("right");
clearInterval(idMovement);
leftMove = false;
rightMove = true;
stopMovement = false;
idMovement = setInterval(movementRightAnimation, 20);
}
if(e.key == "ArrowLeft" && !leftMove){
clearInterval(idMovement);
rightMove = false;
leftMove = true;
stopMovement = false;
idMovement = setInterval(movementLeftAnimation, 20);
}
if(e.key == " " && !jumpMove){
jumpMove = true;
idJump = setInterval(jump, 20);
}
}
function movementLeftAnimation(){
switch(ticks){
case 0:
ticks++;
movementRight -= 10;
square.style.left = movementRight + "px";
square.style.bottom = movementJump + 8 + "px";
square.style.transform = "rotate(-10deg)";
break;
case 1:
ticks++;
movementRight -= 15;
square.style.left = movementRight + "px";
square.style.bottom = movementJump + 15 + "px";
square.style.transform = "rotate(-20deg)";
break;
case 2:
movementRight -= 20;
ticks++;
square.style.left = movementRight + "px";
square.style.bottom = movementJump + 23 + "px";
square.style.transform = "rotate(-45deg)";
break;
case 3:
ticks++;
movementRight -= 20;
square.style.left = movementRight + "px";
square.style.bottom = movementJump + 15+"px";
square.style.transform = "rotate(-70deg)";
break;
case 4:
ticks++;
movementRight -= 15;
square.style.left = movementRight + "px";
square.style.bottom = movementJump + 8 + "px";
square.style.transform = "rotate(-80deg)";
break;
case 5:
movementRight -= 10;
ticks = 0;
square.style.left = movementRight + "px";
square.style.bottom = movementJump + "px";
square.style.transform = "none";
if(stopMovement){
leftMove = false;
clearInterval(idMovement);
}
break;
}
}
function movementRightAnimation(){
switch(ticks){
case 0:
ticks++;
movementRight += 10;
square.style.left = movementRight + "px";
square.style.bottom = movementJump + 8 + "px";
square.style.transform = "rotate(10deg)";
break;
case 1:
ticks++;
movementRight += 15;
square.style.left = movementRight + "px";
square.style.bottom = movementJump + 15 + "px";
square.style.transform = "rotate(20deg)";
break;
case 2:
movementRight += 20;
ticks++;
square.style.left = movementRight + "px";
square.style.bottom = movementJump + 23+ "px";
square.style.transform = "rotate(45deg)";
break;
case 3:
ticks++;
movementRight += 20;
square.style.left = movementRight + "px";
square.style.bottom = movementJump + 15+ "px";
square.style.transform = "rotate(70deg)";
break;
case 4:
ticks++;
movementRight += 15;
square.style.left = movementRight + "px";
square.style.bottom = movementJump + 8 + "px";
square.style.transform = "rotate(80deg)";
break;
case 5:
movementRight += 10;
ticks = 0;
square.style.bottom = movementJump + "px";
square.style.transform = "none";
square.style.left = movementRight + "px";
if(stopMovement){
rightMove = false;
clearInterval(idMovement);
}
break;
}
}
function stopMovementFunction(e){
if(button == e.key)
stopMovement = true;
}