/
Chapter 4.txt
525 lines (440 loc) · 16.4 KB
/
Chapter 4.txt
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
Listing 4-1. Setting up for Scenes
enchant();
window.onload = function() {
//Core object creation
var game = new Core(320, 320);
game.fps = 16;
//Image loading
game.preload('http://enchantjs.com/assets/images/bg/bg01.jpg',
'http://enchantjs.com/assets/images/bg/bg02.jpg',
'http://enchantjs.com/assets/images/bg/bg03.jpg');
//Called when pre-loading is complete
game.onload = function() {
//Background creation
var bg = makeBackground(game.assets['http://enchantjs.com/assets/images/bg/bg01.jpg'])
game.rootScene.addChild(bg);
//Message creation
game.rootScene.addChild(makeMessage("This is the root scene."));
//Choice button creation
var select=makeSelect("【Move to Scene 1】", 320 - 32 * 2);
select.addEventListener(Event.TOUCH_START, function(e) {
game.pushScene(game.makeScene1());
});
game.rootScene.addChild(select);
};
//Scene 1 creation
game.makeScene1 = function() {
var scene = new Scene();
//Background creation
var bg = makeBackground(game.assets['http://enchantjs.com/assets/images/bg/bg02.jpg'])
scene.addChild(bg);
//Message creation
scene.addChild(makeMessage("This is Scene 1."));
//Choice button creation
var select = makeSelect("【Move to Scene 2】", 320 - 32 * 2);
select.addEventListener(Event.TOUCH_START, function(e) {
game.pushScene(game.makeScene2());
});
scene.addChild(select);
scene.addChild(makeReturn(1));
return scene;
};
//Scene 2 creation
game.makeScene2 = function() {
var scene = new Scene();
//Background creation
var bg = makeBackground(game.assets['http://enchantjs.com/assets/images/bg/bg03.jpg'])
scene.addChild(bg);
//Label creation
scene.addChild(makeMessage("This is Scene 2."));
scene.addChild(makeReturn(0));
return scene;
};
//Start game
game.start();
};
//Background creation
function makeBackground(image) {
var bg = new Sprite(320, 320);
bg.image = image;
return bg;
}
//Message creation
function makeMessage(text) {
var label = new Label(text);
label.font = "16px monospace";
label.color = "rgb(255,255,255)";
label.backgroundColor = "rgba(0,0,0,0.6)";
label.y = 320 - 32 * 3;
label.width = 320;
label.height = 32 * 3;
return label;
}
//Choice button creation
function makeSelect(text, y) {
var label = new Label(text);
label.font = "16px monospace";
label.color = "rgb(255,200,0)";
label.y = y;
label.width = 320;
return label;
}
//Return button creation
function makeReturn(lineNumber) {
var game = enchant.Game.instance;
var returnLabel = makeSelect("【Return】", 320 - 32 * (2-lineNumber));
returnLabel.addEventListener(Event.TOUCH_START, function(e) {
game.popScene();
});
return returnLabel;
}
Listing 4-2. Apple-Catching Game
enchant();
window.onload = function() {
//Game object creation
var game = new Core(320, 320);
game.fps = 16;
game.score = 0;
var label;
var bear;
//Image loading
game.preload('chara1.gif',
'http://enchantjs.com/assets/images/map0.gif',
'http://enchantjs.com/assets/images/icon0.gif');
//Called when the loading is complete
game.onload = function() {
//Background creation
var bg = new Sprite(320, 320);
bg.backgroundColor = "rgb(0, 200, 255)";
var maptip = game.assets['http://enchantjs.com/assets/images/map0.gif'];
var image = new Surface(320, 320);
for (var i = 0; i < 320; i += 16) {
image.draw(maptip, 7 * 16, 0, 16, 16, i, 320 - 16, 16, 16);
}
bg.image = image;
game.rootScene.addChild(bg);
//Virtual pad creation
var pad = new Pad();
pad.x = 0;
pad.y = 220;
game.rootScene.addChild(pad);
//Label creation
label = new Label("");
game.rootScene.addChild(label);
//Bear creation
bear = new Sprite(32, 32);
bear.image = game.assets['chara1.gif'];
bear.x = 160 - 16;
bear.y = 320 - 16 - 32;
bear.anim = [10, 11, 10, 12];
bear.frame = bear.anim[0];
game.rootScene.addChild(bear);
//Periodic processing of the bear sprite
bear.addEventListener(Event.ENTER_FRAME, function() {
//Left
if (game.input.left) {
bear.x -= 3;
bear.scaleX = -1;
}
//Right
else if (game.input.right) {
bear.x += 3;
bear.scaleX = 1;
}
//Frame settings
if (!game.input.left && !game.input.right) {
bear.frame = bear.anim[0];
} else {
bear.frame = bear.anim[bear.age % bear.anim.length];
}
});
};
//Adds an apple
game.addApple = function(x, speed) {
//Create apple
var apple = new Sprite(16, 16);
apple.image = game.assets['http://enchantjs.com/assets/images/icon0.gif'];
apple.x = x;
apple.y = -16;
apple.frame = 15;
apple.speed = speed;
game.rootScene.addChild(apple);
//Periodic processing of the sprite
apple.addEventListener(Event.ENTER_FRAME, function() {
apple.y += apple.speed;
//Collision with the bear
//Collision with the ground
else if (apple.y > 320 - 32) {
game.rootScene.removeChild(apple);
}
});
};
//Periodic processing of the scene
game.framesLeft = 10*game.fps; // 10 seconds
game.rootScene.addEventListener(Event.ENTER_FRAME, function() {
game.framesLeft--;
if (game.framesLeft > 0) {
//Apple creation
if ((game.frame % 10) === 0) {
var x = rand(300);
var speed = 3 + rand(6);
game.addApple(x,speed);
}
label.text = "Time left:" + Math.floor(game.framesLeft / game.fps) +
"<BR />Score:" + game.score;
} else {
//Display Game Over
game.end(game.score, "Your score is " + game.score);
}
});
//Start game
game.start();
};
//Generates a random number
function rand(num){
return Math.floor(Math.random() * num);
}
Listing 4-3. Adding the nineleap.enchant.js Plug-In
<script src='/static/enchant.js-latest/plugins/nineleap.enchant.js'></script>
Listing 4-4. Using the Score-Sharing Screen
game.end(game.score, "Your score is " + game.score);
Listing 4-4. Using within()
if (bear.within(apple, 16)) {
game.score+=30;
game.rootScene.removeChild(apple);
}
Listing 4-5. Basics of the Sidescroller
var DIR_LEFT = 0;
var DIR_RIGHT = 1;
var DIR_UP = 2;
var DIR_DOWN = 3;
enchant();
window.onload = function() {
var game = new Core(320, 320);
game.fps = 16;
game.preload(
'http://enchantjs.com/assets/images/map0.gif',
'http://enchantjs.com/assets/images/chara0.gif');
game.onload = function() {
var player = new Sprite(32, 32);
player.image = game.assets['http://enchantjs.com/assets/images/chara0.gif'];
player.x = 2 * 16;
player.y = 16;
player.dir = DIR_DOWN;
player.anim = [
9, 10, 11, 10, //Left
18, 19, 20, 19, //Right
27, 28, 29, 28, //Up
0, 1, 2, 1];//Down
//Frame setting
if (!game.input.up && !game.input.down &&
!game.input.left && !game.input.right) player.age = 1;//Standing Still
player.frame = player.anim[player.dir * 4 + (player.age % 4)];
});
var pad = new Pad();
pad.y = 220;
game.rootScene.addChild(pad);
};
game.start();
};
Listing 4-6. Creating the Map Object
var map = new Map(16, 16);
map.image = game.assets['http://enchantjs.com/assets/images/map0.gif'];
Listing 4-7. Loading in Tile Data
map.loadData([
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,2,2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,2,0],
[0,2,2,2,2,0,0,2,2,2,2,2,2,2,2,0,0,0,2,2,2,2,0],
[0,2,2,2,2,0,0,2,2,2,2,2,2,2,2,0,0,0,2,2,2,2,0],
[0,0,2,2,0,0,0,2,2,0,0,0,0,2,2,0,0,0,0,2,2,0,0],
[0,0,2,2,0,0,0,2,2,0,0,0,0,2,2,0,0,0,0,2,2,0,0],
[0,0,2,2,0,0,0,2,2,0,0,0,0,0,0,0,0,2,2,2,2,0,0],
[0,0,2,2,2,2,2,2,2,2,2,2,2,2,0,0,0,2,2,2,2,0,0],
[0,0,2,2,2,2,2,2,2,2,2,2,2,2,0,0,0,2,2,0,0,0,0],
[0,0,0,0,0,2,2,0,0,0,0,0,2,2,2,2,2,2,2,0,0,0,0],
[0,0,0,0,0,2,2,0,0,0,0,0,2,2,2,2,2,2,2,0,0,0,0],
[0,0,0,2,2,2,2,0,0,0,0,0,2,2,0,0,0,0,0,0,0,0,0],
[0,0,2,2,2,2,2,0,0,0,0,0,2,2,2,2,2,2,2,2,2,0,0],
[0,0,2,2,2,0,0,0,0,0,0,0,2,2,2,2,2,2,2,2,2,0,0],
[0,0,2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,0,0],
[0,0,0,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,0,0],
[0,0,0,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,2,0],
[0,0,0,2,2,2,2,2,2,2,2,2,2,2,2,2,0,0,2,2,2,2,0],
[0,0,0,2,2,2,2,2,2,2,2,2,2,2,2,2,0,0,2,2,2,2,0],
[0,0,0,0,0,0,0,0,0,0,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,1,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,1,1],
[1,1,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,1,1],
[1,1,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,1],
[1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,1],
[1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,1],
[1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,1],
[1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,1],
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
];
Listing 4-9. Character Movement with Map Collision Detection
player.addEventListener(Event.ENTER_FRAME, function() {
//Move up
if (game.input.up) {
player.dir = DIR_UP;
player.y -= 4;
if (map.hitTest(player.x + 16, player.y + 32)) player.y += 4;
}
//Move down
else if (game.input.down) {
player.dir = DIR_DOWN;
player.y += 4;
if (map.hitTest(player.x + 16, player.y + 32)) player.y -= 4;
}
//Move left
else if (game.input.left) {
player.dir = DIR_LEFT;
player.x -= 4;
if (map.hitTest(player.x + 16, player.y + 32)) player.x += 4;
}
//Move right
else if (game.input.right) {
player.dir = DIR_RIGHT;
player.x += 4;
if (map.hitTest(player.x + 16, player.y + 32)) player.x -= 4;
}
//Frame setting
if (!game.input.up && !game.input.down &&
!game.input.left && !game.input.right) player.age = 1;//Standing Still
player.frame = player.anim[player.dir * 4 + (player.age % 4)];
});
Listing 4-10. Combining the Map and Player into a Group
var stage = new Group();
stage.addChild(map);
stage.addChild(player);
game.rootScene.addChild(stage);
2. Set the map to scroll with the player by typing in the code in Listing 4-11 after game.rootScene.addChild(pad);.
Listing 4-11. Scrolling the Map
game.rootScene.addEventListener(Event.ENTER_FRAME, function(e) {
//Set stage XY coordinates
var x = Math.min((game.width - 16) / 2 - player.x, 0);
var y = Math.min((game.height - 16) / 2 - player.y, 0);
x = Math.max(game.width, x + map.width) - map.width;
y = Math.max(game.height, y + map.height) - map.height;
stage.x = x;
stage.y = y;
});
Listing 4-12. Creating a Function to Calculate Length
function calcLen(x0, y0, x1, y1) {
return Math.sqrt((x0 - x1) * (x0 - x1) + (y0 - y1) * (y0 - y1));
}
Listing 4-13. Checking for Collision with the Treasure Chest
if (calcLen(player.x + 16, player.y + 16, 20 * 16 + 8, 17 * 16 + 8) <= 16) {
game.end(0, "Goal");
}
Listing 4-14. Banana Game
enchant();
window.onload = function() {
//Game object creation
var game = new Core(320, 320);
game.fps = 16;
game.score = 0;
game.bananaNum = 10;
game.time = 0;
//Sound and image loading
game.preload(['se1.wav',
'http://enchantjs.com/assets/images/icon0.gif',
'http://enchantjs.com/assets/images/map0.gif']);
//Called when the loading is complete
game.onload = function() {
//Background creation
var bg = new Sprite(320, 320);
var maptip = game.assets['http://enchantjs.com/assets/images/map0.gif'];
var image = new Surface(320, 320);
for (var j = 0; j < 320; j += 16) {
for (var i = 0; i < 320; i += 16) {
image.draw(maptip, 16 * 2, 0, 16, 16, i, j, 16, 16);
}
}
bg.image = image;
game.rootScene.addChild(bg);
//Add bananas
for (var k = 0; k < 10; k++) game.addBanana();
//Add skull
game.addDokuro();
//Periodic scene processing
game.rootScene.addEventListener(Event.ENTER_FRAME, function(){
game.time ++;
});
};
//Adds a skull
game.addDokuro = function(){
var dokuro = new Sprite(16, 16);
dokuro.x = rand(260) + 20;
dokuro.y = rand(260) + 20;
dokuro.image = game.assets['http://enchantjs.com/assets/images/icon0.gif'];
dokuro.frame = 11;
dokuro.addEventListener(Event.TOUCH_START, function(e) {
game.end(0, "Game Over");
});
game.rootScene.addChild(dokuro);
};
//Adds a banana
game.addBanana = function(){
var banana = new Sprite(16, 16);
banana.x = rand(260) + 20;
banana.y = rand(260) + 20;
banana.image = game.assets['http://enchantjs.com/assets/images/icon0.gif'];
banana.frame = 16;
//Event handling when the banana is touched
banana.addEventListener(Event.TOUCH_START, function(e) {
game.rootScene.removeChild(this);
game.bananaNum--;
if (game.bananaNum === 0){
game.end(1000000 - game.time,
(game.time / game.fps).toFixed(2) + " seconds to Clear!");
}
});
game.rootScene.addChild(banana);
};
//Start game
game.start();
};
//Generates a random number
function rand(num){
return Math.floor(Math.random() * num);
}
Listing 4-15. Creating a Sound Object
game.se = game.assets['se1.wav'];
Listing 4-16. Playing a Sound
game.se.play();