-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclaw2.js
executable file
·280 lines (218 loc) · 6.45 KB
/
claw2.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
class charMain {
//main character attributes
char:document.getElementById("man"),
originalPos:13,
x:0,
y:0,
health:100;
jump_power:12;
isJump:0;
gravity:gravity;
runFramesR : ["im.png","im2.png"];
runFramesL : ["im.png","im2.png"];
//jumpFrames : [];
//shootFrames : [];
//duckFrames : [];
mvLeft(){
console.log("mvLeft body");
let w = main_win.offsetWidth - this.char.offsetWidth;
let h = main_win.innerHeight - this.char.offsetHeight;
console.log(w);
if (this.x <=0 )
{
console.log("moving left");
this.x=w;
this.char.style.left=this.x +"px";
console.log(background_images[j]);
main_win.style.backgroundImage=background_images[j];
j=(++j)%background_images.length;
}
else{
this.char.src = this.runFramesL[i];
i=(++i)%this.runFramesL.length;
this.x -= 25;
this.char.style.left = this.x + "px";
}
}
}
//charMain.prototype.mvRight = function(){
//}
charMain.prototype.jump = function(){
let h = main_win.innerHeight - this.char.offsetHeight;
console.log(this.char.offsetHeight);
console.log("ya sal7daaaaar nazl el sabbbt");
console.log(h);
console.log(this.originalPos);
this.y=40;
this.char.style.bottom=this.y +"%";
this.gravity = setInterval(this.land,25);
}
charMain.prototype.land = function(){
console.log("ana nazeeeeeeeeeeel");
if( this.y > this.originalPos)
{ this.y-=1;
this.char.style.bottom=this.y+ "%";
}
else{
this.char.style.bottom=this.originalPos+"%";
clearInterval(this.gravity);
}
}
charMain.prototype.move = function(event){
console.log(event.type);
if(event.which === 39 )
{
console.log("mvRight called");
var mvRight = () => {
console.log("mvRight body");
console.log(this);
let w = main_win.offsetWidth - this.char.offsetWidth; //search how to work this out
let h = main_win.innerHeight - this.char.offsetHeight; //search how to work this out
console.log(w);
if ( w <= this.x )
{
console.log("moving right");
this.x=0;
this.char.style.left=this.x +"px";
console.log(background_images[j]);
main_win.style.backgroundImage=background_images[j];
j=(++j)%background_images.length;
}
else{
//console.log(character_run[i]);
this.char.src = this.runFramesR[i];
i=(++i)%this.runFramesR.length;
this.x += 25;
this.char.style.left = this.x + "px";
}
}
mvRight.call(claw);
}
else if (event.which === 37)
{
console.log("mvLeft called");
this.mvLeft();
}
else if ((event.which === 38) && (!this.land.isJump))
{
console.log("Jump called");
this.jump();
}
}
var background_images = ["url(back.jpg)","url(back2.jpg)"];
var gravity=0;
let i=0; //character positions
let j=0; //background swapper
var main_win = document.getElementById("main_window");
var claw = new charMain;
console.log(claw.char.offsetHeight);
document.addEventListener( 'keydown', claw.move );
/*
//positions
let pos1=0; //inmain char
let pos2=0;// ina main char
let original = 13; //
var jumpFlag=0; //
var gravity=0;
//character attributes
jump_power=25; //
//get elements
var character = document.getElementById("man");
var main_win = document.getElementById("main_window");
// images
var character_run = ["im.png","im2.png"];
var background_images = ["url(back.jpg)","url(back2.jpg)"];
let i=0; //character positions
let j=0; //background swapper
document.addEventListener( 'keydown', move );
//functions
function move(event){
console.log(event.type);
if(event.which === 39 )
{
console.log("hello");
right();
}
else if (event.which === 37)
{
left();
}
else if ((event.which === 38) && (!jumpFlag))
{
jump();
}
}
function right( ){
console.log("ya lahwaaaay");
let w = main_win.offsetWidth - character.offsetWidth;
let h = main_win.innerHeight - character.offsetHeight;
console.log(w);
if ( w <= pos1 )
{
console.log("ana wasalet");
pos1=0;
character.style.left=pos1 +"px";
console.log(background_images[j]);
main_win.style.backgroundImage=background_images[j];
j=(++j)%background_images.length;
}
else{
//console.log(character_run[i]);
character.src = character_run[i];
i=(++i)%character_run.length;
pos1 += 25;
character.style.left = pos1 + "px";
}
}
function left( ){
console.log("ya 5taaaaayy");
let w = main_win.offsetWidth - character.offsetWidth;
let h = main_win.innerHeight - character.offsetHeight;
console.log(w);
if (pos1 <=0 )
{
console.log("ana wasalet");
pos1=w;
character.style.left=pos1 +"px";
console.log(background_images[j]);
main_win.style.backgroundImage=background_images[j];
j=(++j)%background_images.length;
}
else{
character.src = character_run[i];
i=(++i)%character_run.length;
pos1 -= 25;
character.style.left = pos1 + "px";
}
}
function jump(event){
let h = main_win.innerHeight - character.offsetHeight;
console.log(character.offsetHeight);
console.log("ya sal7daaaaar nazl el sabbbt");
console.log(h);
console.log(original);
pos2=40;
character.style.bottom=pos2 +"%";
gravity = setInterval(land,25);
/*
else{
character.src = character_run[i];
i=(++i)%character_run.length;
pos1 -= 25;
character.style.left = pos1 + "px";
}
*/
/*
function land()
{
console.log("ana nazeeeeeeeeeeel");
if( pos2 > original)
{ pos2-=1;
character.style.bottom=pos2+ "%";
}
else{
character.style.bottom=original+"%";
clearInterval(gravity);
}
}
*/