-2

这里有 3 个函数:MakeNPC、CreateNPC 和 NPCAI。CreateNPC 在启动时运行一次并向 MakeNPC 发送数据,以便它们出现在游戏中。NPCAI 每秒运行一次,使 NPC 在游戏中移动。当 CreateNPC 调用 MakeNPC 时,一切正常。问题是当 MakeNPC 函数收到来自 NPCAI 的调用时,会发生错误:无法将未定义转换为对象。

Firebug报告错误的行在MakeNPC的第一行:

 Previous_bead_NPC[NPCid][0] = x;

这是这三个函数的完整代码。我浪费了一整天的时间来解决这个问题,我随时都想哭着入睡:(

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;
}                               
};
4

3 回答 3

1

Previous_bead_NPC是未定义的,因此不是对象。 Previous_bead_NPC[NPCid]当您超过那个时,也将是未定义的。您需要在某些时候定义它们(并且可能{}在尝试访问/分配它们的属性之前将空对象 ( ) 分配给它们。

于 2012-07-08T18:03:07.693 回答
1

对不起,我要打破规则并发布一个离题的 CW 答案。

您可以使用这段代码...

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];
                                    }
于 2012-07-08T18:12:36.137 回答
0

行前:

Previous_bead_NPC[NPCid][0] = x;

你必须定义它将包含数组:

Previous_bead_NPC[NPCid] = [];
于 2013-02-18T20:51:36.510 回答