将未定义对象转换为Javascript中的对象时出错
这里有3个函数:MakeNPC、CreateNPC和NPCAI。CreateNPC在启动时运行一次,并将数据发送给MakeNPC,以便它们出现在游戏中。NPCAI每秒运行一次,使NPC在游戏中移动。当CreateNPC调用MakeNPC时,一切正常。问题是,当MakeNPC函数从NPCAI获得调用时,会发生一个错误:无法将未定义转换为对象 Firebug报告出现错误的行位于MakeNPC的第一行:将未定义对象转换为Javascript中的对象时出错,javascript,undefined,Javascript,Undefined,这里有3个函数:MakeNPC、CreateNPC和NPCAI。CreateNPC在启动时运行一次,并将数据发送给MakeNPC,以便它们出现在游戏中。NPCAI每秒运行一次,使NPC在游戏中移动。当CreateNPC调用MakeNPC时,一切正常。问题是,当MakeNPC函数从NPCAI获得调用时,会发生一个错误:无法将未定义转换为对象 Firebug报告出现错误的行位于MakeNPC的第一行: Previous_bead_NPC[NPCid][0] = x; 下面是这3个函数的全部代码。
Previous_bead_NPC[NPCid][0] = x;
下面是这3个函数的全部代码。为了解决这个问题,我浪费了一整天的时间,我随时都想哭着睡觉:(
var-Previous\u-bead\u-NPC=新数组(10);
对于(变量i=0;i<10;i++)
{
以前的_珠_NPC[i]=新数组(7);
};
var NPC=新阵列(10);
对于(变量i=0;i<10;i++)
{
NPC[i]=新阵列(7);
};
var MakeNPC=函数(x,y,c,g,NPCid,dx,dy,s)
{
“严格使用”;
上一次的NPC[NPCid][0]=x;
上一次NPC[NPCid][1]=y;
上一个_bead _NPC[NPCid][2]=PS.BeadColor(x,y);
前一个珠子NPC[NPCid][3]=PS.BeadGlyph(x,y);
豆色聚苯乙烯(x,y,c);
PS.BeadData(x,y,“阻塞”);
珠母贝(x,y,g);
NPC[NPCid][0]=x;//x
NPC[NPCid][1]=y;//y
NPC[NPCid][2]=c;//颜色
NPC[NPCid][3]=g;//字形
NPC[NPCid][4]=dx;//目的地x
NPC[NPCid][5]=dy;//目的地y
NPC[NPCid][6]=s;//状态-0:已到达,1:正在途中
};
var CreateNPC=函数()
{
“严格使用”;
var i;
对于(i=0;i<10;i++)
{
var x=随机PS(30);
var y=PS.随机(30);
var c=颜色。人类;
var g=“”;
var r=随机PS(100);
如果(r<50)
{
c=颜色。人类;
}
否则如果(r<75)
{
c=颜色。黑色;
}
其他的
{
c=颜色。亚洲;
}
r=PS.随机(19);
g=字母[r];
而(PS.BeadData(x,y)=“阻止”)
{
x=PS.随机(30);
y=PS.随机(30);
}
MakeNPC(x,y,c,g,i,x,y,0);
}
};
//NPC寻路
var NPCAI=功能(NPCid)
{
“严格使用”;
//为NPC选择目的地
如果(NPC[NPCid][6]==0)
{
var r=随机PS(100);
if(r<10)
{
NPC[NPCid][4]=位置[0][0];
NPC[NPCid][5]=位置[0][1];
}
否则如果(r<20)
{
NPC[NPCid][4]=位置[1][0];
NPC[NPCid][5]=地点[1][1];
}
否则如果(r<30)
{
NPC[NPCid][4]=地点[2][0];
NPC[NPCid][5]=地点[2][1];
}
否则如果(r<40)
{
NPC[NPCid][4]=地点[3][0];
NPC[NPCid][5]=地点[3][1];
}
否则如果(r<50)
{
NPC[NPCid][4]=地点[4][0];
NPC[NPCid][5]=地点[4][1];
}
否则如果(r<60)
{
NPC[NPCid][4]=地点[5][0];
NPC[NPCid][5]=地点[5][1];
}
否则如果(r<70)
{
NPC[NPCid][4]=地点[6][0];
NPC[NPCid][5]=地点[6][1];
}
否则如果(r<80)
{
NPC[NPCid][4]=地点[7][0];
NPC[NPCid][5]=地点[7][1];
}
否则如果(r<90)
{
NPC[NPCid][4]=地点[8][0];
NPC[NPCid][5]=地点[8][1];
}
否则如果(r<100)
{
NPC[NPCid][4]=地点[9][0];
NPC[NPCid][5]=地点[9][1];
}
//检查NPC是否已经到达目的地
如果(NPC[NPCid][4]==NPC[NPCid][0]&&NPC[NPCid][5]==NPC[NPCid][1])
{
NPC[NPCid][6]=0;
}
其他的
{
NPC[NPCid][6]=1;
}
}
//寻路逻辑
如果(NPC[NPCid][6]==1)
{
var pointAx=NPC[NPCid][0];//当前位置x
var pointAy=NPC[NPCid][1];//当前位置y
var pointBx=NPC[NPCid][4];//目的地x
var pointBy=NPC[NPCid][5];//目的地y
var_squareNx=pointAx;
var_squareNy=pointAy-1;
var_squarex=pointAx+1;
var_squarey=pointAy;
var_squareSx=pointAx;
var_squareSy=pointAy+1;
var_squareWx=pointAx-1;
var_squareWy=点Ay;
var G=新数组(4);//移动到给定相邻正方形的成本
var H=新数组(4);//从给定的相邻正方形移动到点B的代价
var F=新数组(4);//移动的总成本=G+H
G[0]=10;//N
G[1]=10;//E
G[2]=10;//S
G[3]=10;//W
H[0]=10*(Math.abs(邻接_squareNx-pointBx)+Math.abs(邻接_squareNy-pointBy));
H[1]=10*(Math.abs(邻接_squarex-pointBx)+Math.abs(邻接_squarey-pointBy));
H[2]=10*(Math.abs(邻接平方x-pointBx)+Math.abs(邻接平方y-pointBy));
H[3]=10*(Math.abs(邻接_squareWx-pointBx)+Math.abs(邻接_squareWy-pointBy));
F[0]=G[0]+H[0];
F[1]=G[1]+H[1];
F[2]=G[2]+H[2];
F[3]=G[3]+H[3];
var path=Math.min(F[0],F[1],F[2],F[3]);//WARPATH LOL
//选择正确的pa
var Previous_bead_NPC = new Array(10);
for (var i = 0; i < 10; i++)
{
Previous_bead_NPC[i] = new Array(7);
};
var NPC = new Array(10);
for (var i = 0; i < 10; i++)
{
NPC[i] = new Array(7);
};
var MakeNPC = function (x, y, c, g, NPCid, dx, dy, s)
{
"use strict";
Previous_bead_NPC[NPCid][0] = x;
Previous_bead_NPC[NPCid][1] = y;
Previous_bead_NPC[NPCid][2] = PS.BeadColor(x, y);
Previous_bead_NPC[NPCid][3] = PS.BeadGlyph(x, y);
PS.BeadColor(x, y, c);
PS.BeadData(x, y, "blocked");
PS.BeadGlyph(x, y, g);
NPC[NPCid][0] = x; //x
NPC[NPCid][1] = y; //y
NPC[NPCid][2] = c; //color
NPC[NPCid][3] = g; //glyph
NPC[NPCid][4] = dx; //destination x
NPC[NPCid][5] = dy; //destination y
NPC[NPCid][6] = s; //status - 0: arrived, 1: en route
};
var CreateNPC = function ()
{
"use strict";
var i;
for (i = 0; i < 10; i++)
{
var x = PS.Random (30);
var y = PS.Random (30);
var c = COLOR.human;
var g = " ";
var r = PS.Random (100);
if (r < 50)
{
c = COLOR.human;
}
else if (r < 75)
{
c = COLOR.nigro;
}
else
{
c = COLOR.asian;
}
r = PS.Random (19);
g = letters[r];
while (PS.BeadData(x, y) === "blocked")
{
x = PS.Random (30);
y = PS.Random (30);
}
MakeNPC(x, y, c, g, i, x, y, 0);
}
};
// NPC pathfinding
var NPCAI = function(NPCid)
{
"use strict";
//choosing destination for NPC
if (NPC[NPCid][6] == 0)
{
var r = PS.Random (100);
if (r < 10)
{
NPC[NPCid][4] = locations[0][0];
NPC[NPCid][5] = locations[0][1];
}
else if (r < 20)
{
NPC[NPCid][4] = locations[1][0];
NPC[NPCid][5] = locations[1][1];
}
else if (r < 30)
{
NPC[NPCid][4] = locations[2][0];
NPC[NPCid][5] = locations[2][1];
}
else if (r < 40)
{
NPC[NPCid][4] = locations[3][0];
NPC[NPCid][5] = locations[3][1];
}
else if (r < 50)
{
NPC[NPCid][4] = locations[4][0];
NPC[NPCid][5] = locations[4][1];
}
else if (r < 60)
{
NPC[NPCid][4] = locations[5][0];
NPC[NPCid][5] = locations[5][1];
}
else if (r < 70)
{
NPC[NPCid][4] = locations[6][0];
NPC[NPCid][5] = locations[6][1];
}
else if (r < 80)
{
NPC[NPCid][4] = locations[7][0];
NPC[NPCid][5] = locations[7][1];
}
else if (r < 90)
{
NPC[NPCid][4] = locations[8][0];
NPC[NPCid][5] = locations[8][1];
}
else if (r < 100)
{
NPC[NPCid][4] = locations[9][0];
NPC[NPCid][5] = locations[9][1];
}
//checking if NPC isn't already at its destination
if (NPC[NPCid][4] == NPC[NPCid][0] && NPC[NPCid][5] == NPC[NPCid][1])
{
NPC[NPCid][6] = 0;
}
else
{
NPC[NPCid][6] = 1;
}
}
//pathfinding logic
if (NPC[NPCid][6] == 1)
{
var pointAx = NPC[NPCid][0]; //current position x
var pointAy = NPC[NPCid][1]; //current position y
var pointBx = NPC[NPCid][4]; //destination x
var pointBy = NPC[NPCid][5]; //destination y
var adjacent_squareNx = pointAx;
var adjacent_squareNy = pointAy - 1;
var adjacent_squareEx = pointAx + 1;
var adjacent_squareEy = pointAy;
var adjacent_squareSx = pointAx;
var adjacent_squareSy = pointAy + 1;
var adjacent_squareWx = pointAx - 1;
var adjacent_squareWy = pointAy;
var G = new Array(4); //cost of moving to given adjacent square
var H = new Array(4); //cost of movimg to pointB from given adjacent square
var F = new Array(4); //total cost of a move = G + H
G[0] = 10; //N
G[1] = 10; //E
G[2] = 10; //S
G[3] = 10; //W
H[0] = 10*(Math.abs(adjacent_squareNx-pointBx)+Math.abs(adjacent_squareNy-pointBy));
H[1] = 10*(Math.abs(adjacent_squareEx-pointBx)+Math.abs(adjacent_squareEy-pointBy));
H[2] = 10*(Math.abs(adjacent_squareSx-pointBx)+Math.abs(adjacent_squareSy-pointBy));
H[3] = 10*(Math.abs(adjacent_squareWx-pointBx)+Math.abs(adjacent_squareWy-pointBy));
F[0] = G[0]+H[0];
F[1] = G[1]+H[1];
F[2] = G[2]+H[2];
F[3] = G[3]+H[3];
var path = Math.min(F[0], F[1], F[2], F[3]); //WARPATH LOL
//choosing the right path
if (path == F[0])
{
//go N
if (pointAy > 0)
{
if (!(PS.BeadData(pointAx, pointAy - 1) === "blocked"))
{
// Set bead to Previous State
PS.BeadColor(pointAx, pointAy, Previous_bead_NPC[NPCid][2]);
PS.BeadData(pointAx, pointAy, 0);
PS.BeadGlyph(pointAx, pointAy, " ");
// Increment
pointAy -= 1;
// Place NPC
MakeNPC(pointAx, pointAy, NPC[NPCid][2], NPC[NPCid][3], NPC[NPCid][4], NPC[NPCid][5], 1);
}
}
}
else if (path == F[1])
{
//go E
if (pointAx < 31)
{
if (!(PS.BeadData(pointAx + 1, pointAy) === "blocked"))
{
// Set bead to Previous State
PS.BeadColor(pointAx, pointAy, Previous_bead_NPC[NPCid][2]);
PS.BeadData(pointAx, pointAy, 0);
PS.BeadGlyph(pointAx, pointAy, " ");
// Increment
pointAx += 1;
// Place NPC
MakeNPC(pointAx, pointAy, NPC[NPCid][2], NPC[NPCid][3], NPC[NPCid][4], NPC[NPCid][5], 1);
}
}
}
else if (path == F[2])
{
//go S
if (pointAy < 31)
{
if (!(PS.BeadData(pointAx, pointAy + 1) === "blocked"))
{
// Set bead to Previous State
PS.BeadColor(pointAx, pointAy, Previous_bead_NPC[NPCid][2]);
PS.BeadData(pointAx, pointAy, 0);
PS.BeadGlyph(pointAx, pointAy, " ");
// Increment
pointAy += 1;
// Place NPC
MakeNPC(pointAx, pointAy, NPC[NPCid][2], NPC[NPCid][3], NPC[NPCid][4], NPC[NPCid][5], 1);
}
}
}
else if (path == F[3])
{
//go W
if (pointAx > 0)
{
if (!(PS.BeadData(pointAx - 1, pointAy) === "blocked"))
{
// Set bead to Previous State
PS.BeadColor(pointAx, pointAy, Previous_bead_NPC[NPCid][2]);
PS.BeadData(pointAx, pointAy, 0);
PS.BeadGlyph(pointAx, pointAy, " ");
// Increment
pointAx -= 1;
// Place NPC
MakeNPC(pointAx, pointAy, NPC[NPCid][2], NPC[NPCid][3], NPC[NPCid][4], NPC[NPCid][5], 1);
}
}
}
}
//checking if NPC has arrived
if (NPC[NPCid][4] == NPC[NPCid][0] && NPC[NPCid][5] == NPC[NPCid][1])
{
NPC[NPCid][6] = 0;
}
else
{
NPC[NPCid][6] = 1;
}
};
var r = PS.Random (100),
idx = Math.floor(r / 10);
NPC[NPCid][4] = locations[idx][0];
NPC[NPCid][5] = locations[idx][1];
var r = PS.Random (100);
if (r < 10)
{
NPC[NPCid][4] = locations[0][0];
NPC[NPCid][5] = locations[0][1];
}
else if (r < 20)
{
NPC[NPCid][4] = locations[1][0];
NPC[NPCid][5] = locations[1][1];
}
else if (r < 30)
{
NPC[NPCid][4] = locations[2][0];
NPC[NPCid][5] = locations[2][1];
}
else if (r < 40)
{
NPC[NPCid][4] = locations[3][0];
NPC[NPCid][5] = locations[3][1];
}
else if (r < 50)
{
NPC[NPCid][4] = locations[4][0];
NPC[NPCid][5] = locations[4][1];
}
else if (r < 60)
{
NPC[NPCid][4] = locations[5][0];
NPC[NPCid][5] = locations[5][1];
}
else if (r < 70)
{
NPC[NPCid][4] = locations[6][0];
NPC[NPCid][5] = locations[6][1];
}
else if (r < 80)
{
NPC[NPCid][4] = locations[7][0];
NPC[NPCid][5] = locations[7][1];
}
else if (r < 90)
{
NPC[NPCid][4] = locations[8][0];
NPC[NPCid][5] = locations[8][1];
}
else if (r < 100)
{
NPC[NPCid][4] = locations[9][0];
NPC[NPCid][5] = locations[9][1];
}
Previous_bead_NPC[NPCid][0] = x;
Previous_bead_NPC[NPCid] = [];