質問編集履歴
1
コード追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -23,6 +23,22 @@
|
|
23
23
|
canvas.width = 320;
|
24
24
|
|
25
25
|
canvas.height = 256;
|
26
|
+
|
27
|
+
var Human = new Object();
|
28
|
+
|
29
|
+
Human.img = new Image();
|
30
|
+
|
31
|
+
Human.img.src = 'キャラの画像';
|
32
|
+
|
33
|
+
Human.x = 0;
|
34
|
+
|
35
|
+
Human.y = 0;
|
36
|
+
|
37
|
+
Human.move = 0;
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
var positionX = 0
|
26
42
|
|
27
43
|
|
28
44
|
|
@@ -76,9 +92,59 @@
|
|
76
92
|
|
77
93
|
}
|
78
94
|
|
95
|
+
ctx.drawImage(Human.img, Human.x, Human.y);
|
79
96
|
|
80
97
|
|
98
|
+
|
99
|
+
window.addEventListener("keydown", keydownfunc, false);
|
100
|
+
|
101
|
+
window.addEventListener("keyup", keyupfunc, false);
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
if ( Human.move === 0 ) {
|
106
|
+
|
107
|
+
if ( key.right === true ) {
|
108
|
+
|
109
|
+
var x = Human.x/32;
|
110
|
+
|
111
|
+
var y = Human.y/32;
|
112
|
+
|
113
|
+
if(Human.x === 256 && positionX === 0){
|
114
|
+
|
115
|
+
ctx.translate(-32,0);
|
116
|
+
|
81
|
-
|
117
|
+
positionX = 1;
|
118
|
+
|
119
|
+
}else if(Human.x === 288 && positionX === 1){
|
120
|
+
|
121
|
+
ctx.translate(-32,0);
|
122
|
+
|
123
|
+
positionX = 2;
|
124
|
+
|
125
|
+
}
|
126
|
+
|
127
|
+
if (map[y][x] === 0) {
|
128
|
+
|
129
|
+
Human.move = 32;
|
130
|
+
|
131
|
+
key.push = 'right';
|
132
|
+
|
133
|
+
}
|
134
|
+
|
135
|
+
}
|
136
|
+
|
137
|
+
}
|
138
|
+
|
139
|
+
}
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
if (Human.move > 0) {
|
144
|
+
|
145
|
+
Human.move -= 4;
|
146
|
+
|
147
|
+
if ( key.push === 'right' ) Human.x += 4;
|
82
148
|
|
83
149
|
}
|
84
150
|
|
@@ -88,4 +154,36 @@
|
|
88
154
|
|
89
155
|
window.addEventListener('load', main(), false);
|
90
156
|
|
157
|
+
|
158
|
+
|
159
|
+
function keydownfunc( event ) {
|
160
|
+
|
161
|
+
var key_code = event.keyCode;
|
162
|
+
|
163
|
+
if(key_code === 39){
|
164
|
+
|
165
|
+
key.right = true;
|
166
|
+
|
167
|
+
Human.img.src = 'キャラが右を向く画像';
|
168
|
+
|
169
|
+
}
|
170
|
+
|
171
|
+
event.preventDefault();
|
172
|
+
|
173
|
+
}
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
function keyupfunc( event ) {
|
178
|
+
|
179
|
+
var key_code = event.keyCode;
|
180
|
+
|
181
|
+
if(key_code === 39) key.right = false;
|
182
|
+
|
183
|
+
}
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
キャラを動かすコードですが、右だけ書いています。ほかの方角もこの書き方で書いています。
|
188
|
+
|
91
189
|
```
|