elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: AIO elhacker.NET 2021 Compilación herramientas análisis y desinfección malware


+  Foro de elhacker.net
|-+  Programación
| |-+  Desarrollo Web (Moderador: #!drvy)
| | |-+  [javascript] Estoy haciendo un juego y tengo errores en las variables.
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: [javascript] Estoy haciendo un juego y tengo errores en las variables.  (Leído 2,009 veces)
Ori-chan

Desconectado Desconectado

Mensajes: 257


El rey de los novatos en persona.


Ver Perfil
[javascript] Estoy haciendo un juego y tengo errores en las variables.
« en: 7 Mayo 2014, 20:01 pm »

El juego no corre como debería (No se ve nada) y cuando le doy a "inspeccionar elemento" me salen 2 errores: "Can not read de propety 'vel_x' of undefined" y "Can not read de propety 'vel_x' of undefined".


Código
  1. window.addEventListener('load',init,false);
  2.  
  3. //VARIABLES
  4. var canvas=null,ctx=null;
  5. var lastPress=null;
  6. var pause=true;
  7. var gameover=false;
  8. var dir=0;
  9. var score=0;
  10. var player=new Rectangle(40,40,10,10);
  11. player.tam=10;
  12. player.x=40;
  13. player.y=40;
  14. player.vel_x=1;
  15. player.vel_y=1;
  16. var vel_maxima=8;
  17.  
  18. var KEY_ENTER=13;
  19. var KEY_LEFT=37;
  20. var KEY_UP=38;
  21. var KEY_RIGHT=39;
  22. var KEY_DOWN=40;
  23.  
  24.  
  25. var food=new Array();
  26.  
  27. food.push(new Rectangle(100,50,10,10));
  28. food.push(new Rectangle(100,100,10,10));
  29. food.push(new Rectangle(200,50,10,10));
  30. food.push(new Rectangle(200,100,10,10));
  31.  
  32.    for(var i=0,l=food.length;i<l;i++){
  33. food[i].tam=10;
  34. food[i].dir=0;
  35. food[i].vel_x=6;
  36. food[i].vel_y=6; }
  37.  
  38. //VARIABLES
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48. function random(max){
  49.    return Math.floor(Math.random()*max);
  50. }
  51.  
  52.  
  53. function init(){
  54.    canvas=document.getElementById('canvas');
  55.    ctx=canvas.getContext('2d');
  56.    run();
  57.    repaint();
  58. }
  59.  
  60.  
  61. function run(){
  62.    setTimeout(run,50);
  63.    act();
  64. }
  65.  
  66.  
  67. function repaint(){
  68.    requestAnimationFrame(repaint);
  69.    paint(ctx);
  70. }
  71.  
  72.  
  73. function reset(){
  74. lastPress=null
  75. dir=0
  76. score=0
  77. player=new Rectangle(40,40,10,10)
  78. player.tam=10
  79. player.vel_x=1
  80. playervel_y=1
  81. vel_maxima=8
  82.    for(var i=0,l=food.length;i<l;i++){
  83. food[i].tam=10
  84. food[i].dir=0
  85. food[i].vel_x=6
  86. food[i].vel_y=6}
  87. pause=true
  88. gameover=false
  89. }
  90.  
  91.  
  92. function act(){
  93.    if(!pause){
  94.        if(gameover){
  95.            reset();}
  96.  
  97.        // Change Direction
  98.        if(lastPress==KEY_UP)
  99.            dir=0;
  100.        if(lastPress==KEY_RIGHT)
  101.            dir=1;
  102.        if(lastPress==KEY_DOWN)
  103.            dir=2;
  104.        if(lastPress==KEY_LEFT)
  105.            dir=3;
  106.  
  107.        // Move Rect
  108.  
  109.        if(dir==0)
  110.        if(player.vel_y!=-vel_maxima && dir==0)
  111.            player.vel_y-=1;
  112.  
  113.        if(dir==1)
  114.        if(player.vel_x!=vel_maxima && dir==1)
  115.            player.vel_x+=1;
  116.  
  117.        if(dir==2)
  118.        if(player.vel_y!=vel_maxima && dir==2)
  119.            player.vel_y+=1;
  120.  
  121.        if(dir==3)
  122.        if(player.vel_x!=-vel_maxima && dir==3)
  123.            player.vel_x-=1;
  124.  
  125.            player.y+=player.vel_y
  126.            player.x+=player.vel_x
  127.  
  128.  
  129.        // Out Screen
  130.        if(player.x>canvas.width-player.tam){
  131.            player.vel_x=0
  132.            player.x=canvas.width-player.tam }
  133.  
  134.        if(player.y>canvas.height-player.tam){
  135.            player.vel_y=0
  136.            player.y=canvas.height-player.tam}
  137.  
  138.        if(player.x<0){
  139.            player.vel_x=0
  140.            player.x=0 }
  141.  
  142.        if(player.y<0){
  143.            player.vel_y=0
  144.            player.y=0 }
  145.  
  146.  
  147.    for(var i=0,l=food.length;i<l;i++){
  148.  
  149.        // Move food
  150.            if(food[i].dir==0){
  151.                food[i].x-=food[i].vel_x
  152.                food[i].y+=food[i].vel_y }
  153.            if(food[i].dir==1){
  154.                food[i].x-=food[i].vel_x
  155.                food[i].y-=food[i].vel_y }
  156.            if(food[i].dir==2){
  157.                food[i].x+=food[i].vel_x
  158.                food[i].y+=food[i].vel_y }
  159.            if(food[i].dir==3){
  160.                food[i].x+=food[i].vel_x
  161.                food[i].y-=food[i].vel_y }
  162.  
  163.  
  164.        //Food Out Screen
  165.        if(food[i].y>canvas.height-food[i].tam || food[i].y<0){
  166.           food[i].vel_y=-food[i].vel_y}
  167.        if(food[i].x>canvas.width-food[i].tam || food[i].x<0){
  168.           food[i].vel_x=-food[i].vel_x}
  169.  
  170.     }
  171.  
  172.  
  173.        // Food Intersects
  174.    for(var i=0,l=food.length;i<l;i++){
  175.        if(player.intersects(food[i])){
  176.        if(food[i].tam>player.tam){
  177.            gameover=true;
  178.            pause=true;
  179.         }else {
  180.            score++;
  181.            player.tam+=2;
  182.            food[i].x=random(canvas.width/10-1)*10;
  183.            food[i].y=random(canvas.height/10-1)*10;
  184.            food[i].tam=Math.floor(Math.random() * (20-10+1)) + 10;
  185.            food[i].dir=Math.floor(Math.random() * (3-0+1)) + 0;
  186.            food[i].vel_x=Math.floor(Math.random() * (7-3+1)) + 3;
  187.            food[i].vel_y=Math.floor(Math.random() * (7-3+1)) + 3; }
  188.        }
  189.        }
  190.    }
  191.    // Pause/Unpause
  192.    if(lastPress==KEY_ENTER){
  193.        pause=!pause;
  194.        lastPress=null;
  195.    }
  196. }
  197.  
  198.  
  199.    for(var i=0,l=food.length;i<l;i++){
  200. function paint(ctx){
  201.    ctx.fillStyle='#000';
  202.    ctx.fillRect(0,0,canvas.width,canvas.height);
  203.    ctx.fillStyle='#0f0';
  204.    ctx.fillRect(player.x,player.y,player.tam,player.tam);
  205.    ctx.fillStyle='#f00';
  206.    for(var i=0,l=food.length;i<l;i++){
  207.    ctx.fillRect(food[i].x,food[i].y,food[i].tam,food[i].tam); }
  208.  
  209.    ctx.fillStyle='#fff';
  210.    //ctx.fillText('Last Press: '+lastPress,0,20);
  211.    ctx.fillText('Score: '+score,0,10);
  212.  
  213.    if(pause){
  214.        ctx.textAlign='center';
  215.        if(gameover){
  216.            ctx.fillText('GAME OVER',150,75);}
  217.        else{
  218.            ctx.fillText('PAUSE',150,75);
  219.        ctx.textAlign='left';}
  220.    }
  221. }}
  222.  
  223. document.addEventListener('keydown',function(evt){
  224.    lastPress=evt.keyCode;
  225. },false);
  226.  
  227. function Rectangle(x,y,width,height){
  228.    this.x=(x==null)?0:x;
  229.    this.y=(y==null)?0:y;
  230.    this.width=(width==null)?0:width;
  231.    this.height=(height==null)?this.width:height;
  232.   for(var i=0,l=food.length;i<l;i++){
  233.  
  234.    this.intersects=function(rect){
  235.        if(rect!=null){
  236.            return(this.x<rect.x+food[i].tam&&
  237.                this.x+player.tam>rect.x&&
  238.                this.y<rect.y+food[i].tam&&
  239.                this.y+player.tam>rect.y);
  240.         }
  241.        }
  242.    }
  243.  
  244.    this.fill=function(ctx){
  245.        if(ctx!=null){
  246.            ctx.fillRect(this.x,this.y,this.width,this.height);
  247.        }
  248.    }
  249. }
  250.  
  251. window.requestAnimationFrame=(function(){
  252.    return window.requestAnimationFrame ||
  253.        window.webkitRequestAnimationFrame ||
  254.        window.mozRequestAnimationFrame ||
  255.        function(callback){window.setTimeout(callback,17);};
  256. })();


¿He cometido algún error con las variables o algo así? El error me dio cuando hice la lista del array de food. Y puse los bucles for por ahí


« Última modificación: 7 Mayo 2014, 21:03 pm por #!drvy » En línea


EFEX


Desconectado Desconectado

Mensajes: 1.171


"Dinero Facil"


Ver Perfil WWW
Re: [javascript] Estoy haciendo un juego y tengo errores en las variables.
« Respuesta #1 en: 8 Mayo 2014, 01:03 am »

Este codigo es de algun lado? deberias buscar el source original..

Posiblemente se deba a que se borro la variable..


En línea

Ori-chan

Desconectado Desconectado

Mensajes: 257


El rey de los novatos en persona.


Ver Perfil
Re: [javascript] Estoy haciendo un juego y tengo errores en las variables.
« Respuesta #2 en: 8 Mayo 2014, 16:27 pm »

El código es enteramente mio. Pero prendí a hacer esto en: http://juegoscanvas.blogspot.com.es/
En línea


Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines