Hey!
Ik ben nog redelijk beginner in programmeren met Javascript, vroeg me af waarom deze code niet werkt om een raster te tekenen op een HTML5 <canvas> element. Zie ik iets over het hoofd? Canvas is gewoon leeg.
Ik ben nog redelijk beginner in programmeren met Javascript, vroeg me af waarom deze code niet werkt om een raster te tekenen op een HTML5 <canvas> element. Zie ik iets over het hoofd? Canvas is gewoon leeg.
code:
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
| var cv = document.getElementById('gamecanvas');
var cvctx = cv.getContext('2d');
var world = [
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,1,1,1,1,1,1,1,1,1,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,1,1,1,1,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,1,1,1,1,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,1,1,1,1,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,1,1,1,1,1,1,1,1,1,0],
[0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
];
function drawTile(type, xco, yco){
switch(type){
case "wall":
cvctx.fillStyle = '#aaaaaa';
cvctx.fillRect(xco, yco, 50, 50);
case "floor":
cvctx.fillStyle = '#eeeeee';
cvctx.fillRect(xco, yco, 50, 50);
case "box":
cvctx.fillStyle = '#ffa500';
cvctx.fillRect(xco, yco, 50, 50);
case "player"
cvctx.fillStyle = '#000000';
cvctx.fillRect(xco, yco, 50, 50);
}
return;
}
for(var x = 0; x < 16; x++){
for(var y = 0; y < 12; y++){
if (world[x][y] === 0) {
cvctx.fillStyle = '#aaaaaa';
cvctx.fillRect(xco, yco, 50, 50);
drawTile("wall", x*50, y*50);
} else if (world[x][y] === 1) {
drawTile("floor", x*50, y*50);
} else if (world[x][y] === 2) {
drawTile("box", x*50, y*50);
} else if (world[x][y] === 3) {
drawTile("player", x*50, y*50);
}
}
} |
[ Voor 6% gewijzigd door Verwijderd op 15-10-2013 17:04 ]