TypeError:错误#2007:参数hitTestObject必须为非null。点击事件时出错

时间:2017-07-27 14:31:12

标签: actionscript-3 flash actionscript

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]电影剪辑是允许黑色棋子片移动的地方棋盘。

任何人都可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

您的问题是black数组中有16个元素,但您允许j var达到18。因此,在您的for循环中,当您black[j]时,undefined的值为j16或{{1}时,1718 }

更安全的编码方式是:

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