我在使用函数的对象参数数组时遇到了一些麻烦。
真实代码:
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:
});
错误是:
未捕获的类型错误:无法读取未定义的属性“tocado”。
在行中
if (e[numero].tocado == "rojo") {
数据不为空,我测试过,从服务器返回正确。