/
Chapter 1.txt
181 lines (141 loc) · 4.17 KB
/
Chapter 1.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
Listing 1-1.
//Player object creation
var player = new Sprite(32, 32);
player.image = game.assets['player.png'];
//Enemy character object creation
var enemy = new Sprite(32, 32);
enemy.image = game.assets['enemy.png'];
//Registration in the display object tree
var scene = game.currentScene;
scene.addChild(player);
scene.addChild(enemy);
Listing 1-2.
//player (character) object creation
var player = new Sprite(32, 32);
//handling of the character in each frame
player.addEventListener(Event.ENTER_FRAME, function() {
...
});
//handling touch events
player.addEventListener(Event.TOUCH_START, function(e) {
var x = e.localX;
var y = e.localY;
...
});
Listing 1-3.
document.write('<p>Hello, World!</p>');
Listing 1-4.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
<title>HelloWorld</title>
<style type="text/css">
body {
margin: 0;
}
</style>
</head>
<body>
<p>Hello World! This is the content part of an HTML page.</p>
</body>
</html>
Listing 1-5.
body { background-color: #DDDDDD; font: 30px sans-serif; }
Listing 1-6.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>HelloWorld</title>
<script type="text/javascript" src="enchant.js"></script>
<script type="text/javascript" src="main.js"></script>
</head>
<body>
</body>
</html>
Listing 1-7.
enchant();
var game;
window.onload = function(){
game = new Core(320, 320);
game.onload = function(){
sign = new Label();
sign.text = "Hello World!";
game.rootScene.addChild(sign);
};
game.start();
};
Listing 1-8.
enchant(); //the magic words that start enchant.js
window.onload = function() {
game = new Game();
game.onload = function() { //Prepares the game
hello = new Label("Hello,Bear"); //Create a new label with the words "Hello,Bear"
hello.x = 10; //Place the label 10 pixels to the right (0 will always be the left border)
hello.y = 150; //Place the label 150 pixels from the top (0 will always be the top border)
game.rootScene.addChild(hello); //Show the label on the active screen
}
game.start(); //Begin the game
}
Listing 1-9.
<script src='/static/enchant.js-latest/enchant.js'></script>
<script src='/code.9leap.js'></script>
<script src='main.js'></script>
Listing 1-10.
enchant();
Listing 1-11.
window.onload = function() {
//code to be executed
};
Listing 1-12.
var game = new Core(320, 320);
Source code for the HelloEnchant example at http://code.9leap.net/codes/show/21345:
//initialization of enchant.js
enchant();
//code written here will be executed once the HTML file is completely loaded
window.onload = function() {
//game object creation
var game = new Game(320, 320);
//image loading
game.preload('chara1.gif');
//execution once the image has loaded completely
game.onload = function() {
//Sprite creation
var bear = new Sprite(32, 32);
bear.image = game.assets['chara1.gif'];
bear.frame=4;
//frame loop to move the bear every frame
bear.addEventListener(Event.ENTER_FRAME, function() {
this.x += 3; //move by 3 pixels
});
//add the bear to the display object tree
game.rootScene.addChild(bear);
};
game.start();
};
Listing 1-20.
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
<style type="text/css">
body {
margin: 0;
}
</style>
<link rel='stylesheet' href='style.css' type='text/css'>
<script src='/static/enchant.js-latest/enchant.js'></script>
<script src='/code.9leap.js'></script>
<script src='main.js'></script>
<title>HelloEnchant</title>
</head>
<body>
</body>
</html>