JavaScript - Pequeño problemita de novato...

 
Vista:

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!

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
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de xve
Val: 3.162
Oro
Ha mantenido su posición en JavaScript (en relación al último mes)
Gráfica de JavaScript

Pequeño problemita de novato...

Publicado por xve (2100 intervenciones) el 09/01/2014 10:05:30
Hola Pablo, creo que tu problema esta en que defines las funciones xinicial y yinicial después de la llamada...

Prueba a poner las funciones antes de su llamda:
monigote.x = monigote.xinicial(monigote.id) ;
monigote.y = monigote.yinicial(monigote.id) ;

Coméntanos, ok?
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

Pequeño problemita de novato...

Publicado por Pablo Sax (2 intervenciones) el 09/01/2014 16:15:41
He conseguido solucionar eso, he conseguido que se mueva el pacman y tal... pero al añadirle el método canmoveto:

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
monigote.prototype.canmoveto = function()
{
    var xaux = this.x;
    var yaux = this.y;
 
            switch(this.d){
                case 1:
                    xaux+= -1;
                    yaux+=  0;
                    break;
                case 2:
                    xaux+=  0;
                    yaux+= -1;
                    break;
                case 3:
                    xaux+=  1;
                    yaux+=  0;
                    break;
                case 4:
                    xaux+=  0;
                    yaux+=  1;
                    break;
                }
 
if(mapArray[xaux][yaux]==0){
        var posible = true;
    }else{
        var posible = false;
    }
 
    return posible;
 
};

y poniendolo como condición para cambiar la x y la y no se mueve... Perdonad si soy muy pesado, pero llevo muy poco tiempo con javascript y me lo piden en la uni.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
Imágen de perfil de xve
Val: 3.162
Oro
Ha mantenido su posición en JavaScript (en relación al último mes)
Gráfica de JavaScript

Pequeño problemita de novato...

Publicado por xve (2100 intervenciones) el 09/01/2014 21:27:05
Hola Pablo, la verdad que sin ver el código entero no lo podemos probar, y es un poco complicado ayudarte, ya que, que valor tiene this.d?
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar