TypeError: Error #2007: Parameter hitTestObject must be non-null. error upon click event
TypeError: Error #2007: Parameter hitTestObject must be non-null. error upon click event
function checkboard(abc:MovieClip)//:Number
{
var targetarray:Array= new Array (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14,
T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27,
T28, T29, T30, T31, T32, T33, T34, T35, T36,T37,T38,T39,T40,T41,T42,
T43,T44,T45,T46,T47,T48,T49,T50,T51,T52,T53,T54,T55,T56,T57,T58,
T59,T60,T61,T62,T63,T64);
var black:Array= new Array (B1,B2,B3,B4,B5,B6,B7,B8,B9,B10,B11,B12,B13,B14,B15,B16);
var red:Array= new Array (R1,R2,R3,R4,R5,R6,R7,R8,R9,R10,R11,R12,R13,R14,R15,R16);
//var propertiess:Array = new Array (0);
//var legal:Array =(targetarray,propertiess);
var i:Number = 0;
var j:Number=0;
var loh:Number;
for (i=0;i<64;i++)
{
if(targetarray[i].hitTestObject(black[j]))
{
trace("black peice on ",targetarray[i+1]);
}
if(j>18){
j=18;
}else{
j++;
}
}
}
这是导致错误的函数,似乎在这一行 if(targetarray[i].hitTestObject(black[j]))
。
但是,我不明白为什么这行不通,B[j]
片段是影片剪辑,T[i]
影片片段是允许黑色棋子移动的地方棋盘。
谁能帮我解决这个问题?
您的问题是您的 black
数组中有 16 个元素,但您允许 j
变量达到 18
。因此,在您的 for 循环中,当您执行 black[j]
时,当 j
的值为 16
、17
或 [=13= 时,它将是 undefined
]
一种更安全的编码方式是按照以下几行:
//use targetArray.length to only loop to the amount of items in the array
for(i=0;i<targetArray.length;i++){
if(targetarray[i].hitTestObject(black[j]))
{
//not sure why you're adding 1 here? This would be null if 'i' is the last number in the loop
//remember, arrays are 0 based, so `T1` is actually targetArray[0]
trace("black peice on ",targetarray[i+1]);
}
//only increment j if the result is still less than the length of black array.
if(j < black.length - 1){
j++;
}
}
function checkboard(abc:MovieClip)//:Number
{
var targetarray:Array= new Array (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14,
T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27,
T28, T29, T30, T31, T32, T33, T34, T35, T36,T37,T38,T39,T40,T41,T42,
T43,T44,T45,T46,T47,T48,T49,T50,T51,T52,T53,T54,T55,T56,T57,T58,
T59,T60,T61,T62,T63,T64);
var black:Array= new Array (B1,B2,B3,B4,B5,B6,B7,B8,B9,B10,B11,B12,B13,B14,B15,B16);
var red:Array= new Array (R1,R2,R3,R4,R5,R6,R7,R8,R9,R10,R11,R12,R13,R14,R15,R16);
//var propertiess:Array = new Array (0);
//var legal:Array =(targetarray,propertiess);
var i:Number = 0;
var j:Number=0;
var loh:Number;
for (i=0;i<64;i++)
{
if(targetarray[i].hitTestObject(black[j]))
{
trace("black peice on ",targetarray[i+1]);
}
if(j>18){
j=18;
}else{
j++;
}
}
}
这是导致错误的函数,似乎在这一行 if(targetarray[i].hitTestObject(black[j]))
。
但是,我不明白为什么这行不通,B[j]
片段是影片剪辑,T[i]
影片片段是允许黑色棋子移动的地方棋盘。
谁能帮我解决这个问题?
您的问题是您的 black
数组中有 16 个元素,但您允许 j
变量达到 18
。因此,在您的 for 循环中,当您执行 black[j]
时,当 j
的值为 16
、17
或 [=13= 时,它将是 undefined
]
一种更安全的编码方式是按照以下几行:
//use targetArray.length to only loop to the amount of items in the array
for(i=0;i<targetArray.length;i++){
if(targetarray[i].hitTestObject(black[j]))
{
//not sure why you're adding 1 here? This would be null if 'i' is the last number in the loop
//remember, arrays are 0 based, so `T1` is actually targetArray[0]
trace("black peice on ",targetarray[i+1]);
}
//only increment j if the result is still less than the length of black array.
if(j < black.length - 1){
j++;
}
}