Pequeño problemita de novato...
Publicado por Pablo Sax (2 intervenciones) el 08/01/2014 23:47:06
Dejo aquí un cacho de código que me está dando problemillas con el objeto monigote. Ayuda, por favor!
Qué estoy haciendo mal? No funciona el objeto monigote...
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
<!DOCTYPE html>
<html>
<head>
<script>
var main = function(){
var canvas = document.getElementById('pacCanvas');
var ctx = canvas.getContext('2d');
document.body.appendChild(pacCanvas);
var semilla;
var celda = 30;
function plantilla(tipo)
{
switch(tipo)
{
case 0:
ctx.beginPath();
ctx.fillStyle = 'yellow';
ctx.arc(pacman.x*celda,pacman.y*celda,(celda/2)-3,0,2*Math.PI);
ctx.closePath();
ctx.fill();
break;
case 1:
ctx.beginPath();
ctx.fillStyle = "red";
ctx.fillRect((rojo.x*celda)+3,(rojo.y*celda)+3,celda-6,celda-6);
ctx.fillStyle = "white";
ctx.fillRect((rojo.x*celda)+3+4,(rojo.y*celda)+3+4,6,6);
ctx.fillRect((rojo.x*celda)+3+16,(rojo.y*celda)+3+4,6,6);
ctx.fillStyle = "black";
ctx.fillRect((rojo.x*celda)+3+4,(rojo.y*celda)+3+7,3,3);
ctx.fillRect((rojo.x*celda)+3+16,(rojo.y*celda)+3+7,3,3);
ctx.closePath();
break;
case 2:
ctx.beginPath();
ctx.fillStyle = "blue";
ctx.fillRect((azul.x*celda)+3,(azul.y*celda)+3,celda-6,celda-6);
ctx.fillStyle = "white";
ctx.fillRect((azul.x*celda)+3+4,(azul.y*celda)+3+4,6,6);
ctx.fillRect((azul.x*celda)+3+16,(azul.y*celda)+3+4,6,6);
ctx.fillStyle = "black";
ctx.fillRect((azul.x*celda)+3+4,(azul.y*celda)+3+7,3,3);
ctx.fillRect((azul.x*celda)+3+16,(azul.y*celda)+3+7,3,3);
ctx.closePath();
break;
case 3:
ctx.beginPath();
ctx.fillStyle = "green";
ctx.fillRect((verde.x*celda)+3,(verde.y*celda)+3,celda-6,celda-6);
ctx.fillStyle = "white";
ctx.fillRect((verde.x*celda)+3+4,(verde.y*celda)+3+4,6,6);
ctx.fillRect((verde.x*celda)+3+16,(verde.y*celda)+3+4,6,6);
ctx.fillStyle = "black";
ctx.fillRect((verde.x*celda)+3+4,(verde.y*celda)+3+7,3,3);
ctx.fillRect((verde.x*celda)+3+16,(verde.y*celda)+3+7,3,3);
ctx.closePath();
break;
default:
break;
}
}
function getId(){
return semilla;
}
var monigote = new Object();
monigote.id = getId();
monigote.x = monigote.xinicial(monigote.id) ;
monigote.y = monigote.yinicial(monigote.id) ;
monigote.d = 0;
monigote.status = false;
monigote.draw = function(){ plantilla(monigote.id);};
monigote.changed = function(d){monigote.d = d;};
monigote.mover = function()
{
switch(monigote.d){
case 1:
monigote.x+= -1;
monigote.y+= 0;
break;
case 2:
monigote.x+= 0;
monigote.y+= -1;
break;
case 3:
monigote.x+= 1;
monigote.y+= 0;
break;
case 4:
monigote.x+= 0;
monigote.y+= 1;
break;
}
};
monigote.xinicial = function(id){
switch(id){
case 0:
return 10;
case 1:
return 10;
case 2:
return 10;
case 3:
return 10;
default:
return null;
}
};
monigote.yinicial = function(id){
switch(id){
case 0:
return 10;
case 1:
return 10;
case 2:
return 10;
case 3:
return 10;
default:
return null;
}
};
function init(){
semilla = 0;
var pacman = new monigote();
semilla = 1;
var rojo = new monigote();
semilla = 2;
var azul = new monigote();
semilla = 3;
var verde = new monigote();
}
function tecla(){
window.addEventListener('keydown',function(event){
if(event.keyCode == 37){
pacman.changed(1);
}
if(event.keyCode == 38){
pacman.changed(2);
}
if(event.keyCode == 39){
pacman.changed(3);
}
if(event.keyCode == 40){
pacman.changed(4);
}
},false);
}//fin de tecla
init();
pacman.draw();
} //fin del main
</script>
</head>
<body onload="main()">
<canvas id="pacCanvas" width="600px" height="600px">Tu navegador no soporta el canvas de HTML5 :( </canvas>
</body>
</html>
Qué estoy haciendo mal? No funciona el objeto monigote...
Valora esta pregunta
0