I´m having some troubles with a array of objects parameter for a function.
REAL CODES:
function drawTable(x,y,numero,t,e) {
context.fillStyle = "#ffff66";
context.fillRect(x*146,y*146,55,55);
var color;
if (e[numero].tocado == "rojo") {
color = "#cc3300"
} else if (e[numero].tocado == "azul") {
color = "#0099ff";
} else {
color = "#66ff66";
}
context.fillStyle = color;
if (t==0 && x < 4) {
context.fillRect((x*146)+55,(y*146)+9,91,39);
}
if (t==1){
context.fillRect((x*146)+9,(y*146)+55,39,91);
}
if (x==4) {
if (y<4) {
if (t==1) {
drawTable(0,y+1,numero+1,0,e);
} else {
drawTable(0,y,numero,1,e);
}
}
} else {
drawTable(x+1,y,numero+1,t,e);
}
return;
}
socket.on("start", function (data) {
game = data;
drawTable(0,0,0,0,game.elements);
//....
}
"Data is a object:"
function Game() {
this.turno = "";
this.puntuacion_rojo = 0;
this.puntuacion_azul = 0;
this.elements = [];
}
"elements built"
this.elements.push({
id:
tipo:
tocado: ,
left:
top:
width:
height:
});
The error is:
Uncaught TypeError: Cannot read property 'tocado' of undefined.
in the line
if (e[numero].tocado == "rojo") {
Data isn't empty, I tested it, It returns from the server correctly.
var e = [{}];. Otherwise it isn't an array (so you can't access the 0th item) and doesn't have any objects in it (so you can't set anyhelloproperty)eisundefinednot an array of objects (you never assigned a value toe). Why do you think JS should think it is an array of objects?data.elementsdoesn't exist. What doesconsole.log(data)show you?