2012-07-08 3 views
-2

다음은 MakeNPC, CreateNPC 및 NPCAI의 3 가지 기능입니다. CreateNPC는 시작시 한 번 실행되며 MakeNPC에 데이터를 보내 게임에 표시되도록합니다. NPCAI는 1 초마다 실행되며 게임에서 NPC를 움직입니다. CreateNPC가 MakeNPC를 호출 할 때 모든 것이 OK입니다. 문제는 MakeNPC 함수가 NPCAI에서 호출을 받으면 오류가 발생한다는 것입니다. undefined를 객체로 변환 할 수 없습니다. 여기 자바 스크립트 오류로 정의되지 않은 개체로 변환

Previous_bead_NPC[NPCid][0] = x; 

이 3 개 함수의 전체 코드 : 오류가 마련 방화범에 의해보고되는

라인 MakeNPC의 첫 번째 줄에 있습니다. 나는이 문제를 해결하려고 하루 종일 낭비와 나는 이렇게

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; 
}        
}; 
+0

'Previous_bead_NPC'는 2 차원 배열입니다.이'Array' 객체의 선언/초기화는 어디입니까? – xandercoded

+0

어쩌면 어떤 인덱스가 어떤 것을 의미 하는지를 기억해야하는 배열 대신 명명 된 속성이있는 객체를 사용해야합니다. 또한 중첩 된 거대한 중첩을'r = Math.round (PS.Random (10));로 대체하십시오. 스위치 (r) {케이스 0 : ...}'. – OrangeDog

+0

그리고 나서 프로토 타입 메소드로'NPC' 생성자를 만드십시오. 막연하게 읽을 수 있도록 다시 작성을 마칠 때까지이 오류는 사라 졌을 것입니다. – OrangeDog

답변

1

Previous_bead_NPC가 정의되지 않은 어떤 분 :(잠을 자신을 울에 대한, 그리고 아니에요 객체. 당신이 과거를 얻을 때 Previous_bead_NPC[NPCid] 또한, 정의되지 않습니다 그 중 하나. 속성을 액세스/할당하기 전에 어떤 점에서 이들을 정의해야합니다 (아마도 공백 개체 ({})를 할당해야합니다.)

+0

정의되지 않은 형식으로 어떻게 정의 할 수 있습니까? – DerpTomi

+0

많은 코드를 게시하고 오류에서 참조되는 변수의 정의와 같은 기본 사항을 포함하지 않을 경우 추가 도움이 필요한 이유는 무엇입니까? – OrangeDog

+0

무엇이라도 정의하면 더 이상 정의되지 않습니다. – AlienWebguy

1

죄송합니다. 규칙을 어기고 게시 해 보겠습니다. -topic 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]; 
            } 
0

:

Previous_bead_NPC[NPCid][0] = x; 

당신이 그것을 정의 할 배열을 포함합니다 :

Previous_bead_NPC[NPCid] = []; 
관련 문제