JavaScript - Uncaught TypeError: Cannot read property 'dibuja' of undefined

 
Vista:
sin imagen de perfil
Val: 8
Ha aumentado su posición en 359 puestos en JavaScript (en relación al último mes)
Gráfica de JavaScript

Uncaught TypeError: Cannot read property 'dibuja' of undefined

Publicado por CRISTIAN OMAR (13 intervenciones) el 25/02/2021 03:10:26
EN YOUTUBE: https://www.youtube.com/watch?v=SjH4CcOAcsg
DEL CANAL: Programar es increíble
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
TENGO EL SIGUIENTE ERROR

Uncaught TypeError: Cannot read property 'dibuja' of undefined
at dibujaTablero (juego.js:110)
at principal (juego.js:122)
at juego.js:103
dibujaTablero @ juego.js:110
principal @ juego.js:122
(anonymous) @ juego.js:103
setInterval (async)
inicializa @ juego.js:103
onload @ index.html:4

*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
CODIGO HTML

1
2
3
4
5
6
7
8
<html>
<head>
</head>
<body onload='inicializa();'>
	<script src='js/juego.js'></script>
	<canvas id='canvas' style='border:1px solid #000000'></canvas>
</body>
</html>
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
CODIGO JS

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
var canvas;
var ctx;
var fps = 30;
 
var canvasX = 530; //ANCHO
var canvasY = 500; //ALTO
 
var tileX, tileY;
 
//VARIABLES RELACIONADAS CON EL TABLERO DE JUEGO
var tablero;
var filas = 100;
var columnas = 100;
 
var blanco = '#FFFFFF';
var negro = '#000000';
 
function creaArray2D(f,c){
	var obj = new Array(f);
	for(y=0;y<f;y++){
		obj[y] = new Array(c);
	}
	return obj;
}
 
//AGENTE O TERMITA
var Agente = function(x,y, estado){
	this.x = x;
	this.y = y;
	this.estado = estado; // VIVO = 1 , MUERTO = 2
	this.estadoProx = this.estado; // ESTADO SIGUIENTE CICLO
 
	this.vecinos = []; //GUARDAMOS EL LISTADO DE SUS VECINOS
 
	//METODO QUE AÑADE LOS VECINOS DEL OBJETO ACTUAL
	this.addVecinos = function(){
		var xVecino;
		var yVecino;
 
		for(i=1;i<2;i++){
			for(j=1;j<2;j++){
				xVecino = (this.x + j + columnas) % columnas;
				yVecino = (this.y + i + filas) % filas;
 
				//DESCARTAMOS EL AGENTE ACTUAL, YO NO PUEDO SER MI PROPIO VECINO
				if(i!=0 || j!=0){
					this.vecinos.push(tablero[yVecino][xVecino]);
				}
			}
		}
	}
 
	this.dibuja = function(){
		var color;
 
		if(this.estado == 1)
			color = blanco;
		else
			color = negro;
 
		ctx.fillStyle = color;
		ctx.fillRect(this.x*tileX, this.y*tileY, tileX, tileY);
	}
}
 
function inicializaTablero(obj){
	var estado;
 
	for(i=0; y<filas;y++){
		for(x=0;x<columnas;x++){
			estado = Math.floor(Math.random()*2);
			obj[y][x] = new Agente(y,x,estado)
		}
	}
 
	for(i=0; y<filas;y++){
		for(x=0;x<columnas;x++){
			obj[y][x].addVecinos();
		}
	}
}
 
function inicializa(){
	//ASOCIAMOS EL CANVAS
	canvas = document.getElementById('canvas');
	ctx = canvas.getContext('2d');
 
	//AJUSTAMOS EL TAMAÑO DEL CANVAS
	canvas.width = canvasX;
	canvas.height = canvasY;
 
	//CALCULAMOS TAMAÑO TILES
	tileX = Math.floor(canvasX/filas);
	tileY = Math.floor(canvasY/columnas);
 
	//CREAMOS EL TABLERO
	tablero = creaArray2D(filas, columnas);
 
	//LO INICIALIZAMOS
	inicializaTablero(tablero);
 
	//EJECUTAMOS EL BUCLE PRINCIPAL
	setInterval(function(){principal();},1000/fps);
 
}
 
function dibujaTablero(obj){
	for(y=0;y<filas;y++){
		for(x=0;x<columnas;x++){
			obj[y][x].dibuja();
		}
	}
}
 
function borrarCanvas(){
	canvas.width = canvas.width;
	canvas.height = canvas.height;
}
 
function principal(){
	borrarCanvas();
	dibujaTablero(tablero);
}
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 joel
Val: 3.506
Oro
Ha mantenido su posición en JavaScript (en relación al último mes)
Gráfica de JavaScript

Uncaught TypeError: Cannot read property 'dibuja' of undefined

Publicado por joel (895 intervenciones) el 25/02/2021 08:27:56
Hola Cristian, tienes dos pequeños errores en las lineas 69 y 76... tienes puesto esto:
1
for(i=0; y<filas;y++){
y tiene que ser esto:
1
for(y=0; y<filas;y++){

y yo te recomiendo esto:
1
for(let y=0; y<filas;y++){
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
sin imagen de perfil
Val: 8
Ha aumentado su posición en 359 puestos en JavaScript (en relación al último mes)
Gráfica de JavaScript

Uncaught TypeError: Cannot read property 'dibuja' of undefined

Publicado por CRISTIAN OMAR (13 intervenciones) el 25/02/2021 15:24:25
Primero dar las gracias por su eficiente respuesta y su apreciable tiempo.

Linea 69, Mi error:
for(i=0; y<filas;y++){ // DECLARE ALGO QUE NO OCUPE i = 0

Linea 69, corregi la i por y, solo una pequeña letra, dios como dice el canal, Programar es increíble:
for(i=0; y<filas;y++){

Buen día
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