MakeNPC、CreateNPC、NPCAI の 3 つの関数があります。CreateNPC は起動時に 1 回実行され、MakeNPC にデータを送信してゲームに表示されるようにします。NPCAI は毎秒実行され、NPC をゲーム内で動かします。CreateNPC が MakeNPC を呼び出すと、すべて問題ありません。問題は、MakeNPC 関数が NPCAI からの呼び出しを取得すると、エラーが発生することです: undefined をオブジェクトに変換できません。
Firebug によってエラーが報告される行は、MakeNPC の最初の行にあります。
Previous_bead_NPC[NPCid][0] = x;
これら 3 つの関数のコード全体を次に示します。私はこれを解決しようとして一日を無駄にしました.
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;
}
};