Ver Mensaje Individual
  #3 (permalink)  
Antiguo 24/05/2013, 09:29
phyronx
 
Fecha de Ingreso: mayo-2009
Mensajes: 232
Antigüedad: 15 años
Puntos: 3
Respuesta: Porque pasa esto?

Muchas gracias maycolalvarez! Ese era el error.

Ahora estuve toqueteandolo un poco mas, para añadir eventos y niveles.

Peor otra vez llego a un punto que no se porque pasa lo que pasa...

Tengo una funcion Start que dibuja los cuadrados en el cambas, guarda los elementos en un array y crea los eventos de click.

Si pulsas el rojo se para el tiempo, sube un nivel 'level +=1',detiene el listener, borra todo y redibuja otra vez con la funcion start, esta vez con un cuadrado mas. Si por el contrario pulsas en uno erronio, aparte del mensaje, cambia el level a 1 otra vez, y lo otro igual.

Eso es la teoria, pero en la practica, aunque no te equivoques, los niveles no se suman 1 a 1, sino k van 1,2,4,8,12,16,20.. etc...

Aparte, en el nivel 4 o 6 normalmente todo se descontrola, pulsas sobre el rojo y dice que te has equivocado, y adems de sumar mas niveles de los k son, añade el doble o mas de cuadrados. cuando es una simple funcion k le dice k sume 1.


El codigo:

Código Javascript:
Ver original
  1. var level = 1;
  2. var anchurapantalla = screen.availWidth;
  3. var alturapantalla = screen.availHeight;
  4.  
  5. var popup = document.getElementById('alert');
  6.  
  7. popup.style.left= (anchurapantalla / 2) -100;
  8. popup.style.top= (alturapantalla / 2) -100
  9.  
  10. var elem = document.getElementById('canvas'),
  11.     elemLeft = elem.offsetLeft,
  12.     elemTop = elem.offsetTop,
  13.     context = elem.getContext('2d'),
  14.     elements = [];
  15.  
  16. elem.width = anchurapantalla;
  17. elem.height = alturapantalla;
  18.  
  19. context.fillRect(0, 0, anchurapantalla, alturapantalla);
  20.  
  21.  
  22. var size = 40;
  23. var numxfila = 3;
  24. var posx = 10;
  25. var posy = 10;
  26. var sumax = 50;
  27.  
  28. var a = document.getElementById("time");
  29. var time = 222;
  30.  
  31. var colors=new Array("yellow","green","blue","white","orange");
  32.  
  33. var timer ;
  34.  
  35. //AGREGO LOS LISTENERS A LOS ELEMENTOS
  36.  
  37.  
  38. function listener(event){
  39.  
  40. var x = event.pageX - elemLeft,
  41.         y = event.pageY - elemTop;
  42.     console.log(x, y);
  43.     elements.forEach(function(element) {
  44.         if (y > element.top && y < element.top + element.height && x > element.left && x < element.left + element.width) {
  45.  
  46.  
  47.             if (element.colour == 'red') {
  48.  
  49.                 elem.removeEventListener('click', listener, false);
  50.                 clearInterval(timer);
  51.                 time = 222;
  52.                 posx = 10;
  53.                 level +=1;
  54.  
  55.                 context.fillStyle="#000000";
  56.                 context.fillRect(0, 0, anchurapantalla, alturapantalla);
  57.  
  58.                 start();
  59.  
  60.             }else{
  61.  
  62.  
  63.                 elem.removeEventListener('click', listener, false);
  64.                 clearInterval(timer);
  65.  
  66.                
  67.  
  68.                 context.fillStyle="#000000";
  69.                 context.fillRect(0, 0, anchurapantalla, alturapantalla);
  70.  
  71.                 alert('Fallaste! llegaste al nivel '+level );
  72.  
  73.                 time = 222;
  74.                 posx = 10;
  75.                 level = 1;
  76.                
  77.          
  78.                 start();
  79.  
  80.             }
  81.            
  82.         }
  83.     });
  84.  
  85.  }
  86.  
  87.  
  88.  
  89.  
  90. function start(){
  91.  
  92. var rojo = Math.floor((Math.random()*level));
  93.     rojo = Math.round(rojo);
  94.  
  95. if (rojo == 0) { rojo = 1;};
  96. //CREO EL TEXTO
  97.  
  98. context.fillStyle = '#ffffff';
  99. context.font = 'italic bold 11px sans-serif';
  100. context.textBaseline = 'bottom';
  101. context.fillText('LeVeL: '+level, alturapantalla/2, 30);
  102.  
  103.  
  104. document.getElementById('alert').style.display = "none";
  105.  
  106.  
  107. var cont = 0;
  108.  
  109. for (var i = 1 ; i <= level; i++) {
  110.    
  111.     var color = Math.random() * 4;
  112.     color = Math.round(color);
  113.  
  114.  
  115.     if (cont == numxfila) {
  116.  
  117.         cont = 0;
  118.         posy += 50;
  119.         posx = 10;
  120.     };
  121.  
  122.  
  123.  
  124. if (i == rojo) {
  125.  
  126.     elements.push({
  127.     colour: 'red',
  128.     width: size,
  129.     height: size,
  130.     top: posy,
  131.     left: posx
  132. });
  133.  
  134. }else {
  135.  
  136.     elements.push({
  137.     colour: colors[color],
  138.     width: size,
  139.     height: size,
  140.     top: posy,
  141.     left: posx
  142. });
  143. }
  144.  
  145.  
  146. posx += sumax;
  147. cont += 1;
  148.  
  149. };
  150.  
  151.  
  152.  
  153. // Creo los elementos.
  154. elements.forEach(function(element) {
  155.     context.fillStyle = element.colour;
  156.     context.fillRect(element.left, element.top, element.width, element.height);
  157. });
  158.  
  159. //ejecuto el listener
  160. elem.addEventListener('click', listener, false);
  161.  
  162. //EMPIEZA L CUENTA ATRAS
  163.  
  164. timer = setInterval(ciclo,1000,false);
  165.  
  166.  
  167. }
  168.  
  169.  
  170.  
  171.  
  172. function ciclo(){
  173.  
  174.  
  175.  
  176. time -=1;
  177.  
  178. document.getElementById('time').innerHTML = time;
  179.  
  180. if(time == 0){
  181.  
  182. clearInterval(timer);
  183.  
  184. alert('Fuiste demasiado lento');
  185.  
  186.  
  187. }
  188.  
  189.  
  190.  
  191. }


Hos pongo la parte de html por si quereis provarlo:

Código HTML:
Ver original
  1.     <title></title>
  2.     <link rel="stylesheet" type="text/css" href="css/style.css" />
  3. </head>
  4. <div width='100%' height='10' align = 'center'>Time: <font id='time' size='5'> 3</font></div>
  5. <canvas id="canvas" width="100" height="100" align='center'/></canvas>
  6. <div id='alert' width='100' height='100' align='center'>
  7.     3 SEGUNDOS PARA PULSAR EL ROJO.
  8. <br>
  9.     <button onclick='start();'> START </button>
  10. </div>
  11. </body>
  12. <script src="js/game.js"></script>
  13. </html>


PD: En niveles superiores, crea tambien mas de uno rojo, y e comprovado que si clickeas en uno igual te suma 4 niveles, en otro solo te suma 1 y en otro te dice k no es rojo y pierdes. -_-

Última edición por phyronx; 24/05/2013 a las 09:47