0

The following piece of the as3 code is written for a card game, and I am curious, why I get Error #1010?

var backHands:Array = new Array();
var numberOfPlayingCards = 0;
function initBackHands(){ 
    var i:int;
    var j:int;

    for(j=1;j<4;j++){
        var newBacks:Array = new Array();
        for(i=0;i<13;i++){
            newBacks[i] = new CBack();
            addChild(newBacks[i]);
        }
        backHands[j-1] = newBacks;
    }
}
function removeBack(p:int){
try{
    if(position[p]>0){
        var index:int = (numberOfPlayingCards/4);
        index = 12- index;
        if(contains(backHands[position[p] - 1][index])){
            removeChild(backHands[position[p] - 1][index]);
        }
    }

    }catch(error:Error){
    trace("playCard= NOPC: "+ numberOfPlayingCards +
            " index: " + index +
            " position: " + (position[p] - 1) + 
            " err: "+ error);
    }
    numberOfPlayingCards ++;

}

and this is the error log:

playCard= NOPC: 0 index: 12 position: 1 err: TypeError: Error #1010 
playCard= NOPC: 1 index: 12 position: 0 err: TypeError: Error #1010 
playCard= NOPC: 1 index: 12 position: 2 err: TypeError: Error #1010 
playCard= NOPC: 2 index: 12 position: 0 err: TypeError: Error #1010 
playCard= NOPC: 2 index: 12 position: 1 err: TypeError: Error #1010 
playCard= NOPC: 3 index: 12 position: 2 err: TypeError: Error #1010 
playCard= NOPC: 4 index: 11 position: 2 err: TypeError: Error #1010 
playCard= NOPC: 5 index: 11 position: 1 err: TypeError: Error #1010
playCard= NOPC: 6 index: 11 position: 0 err: TypeError: Error #1010 
playCard= NOPC: 8 index: 10 position: 0 err: TypeError: Error #1010 
playCard= NOPC: 10 index: 10 position: 2 err: TypeError: Error #1010
playCard= NOPC: 11 index: 10 position: 1 err: TypeError: Error #1010
playCard= NOPC: 12 index: 9 position: 0 err: TypeError: Error #1010 

The question is which line makes the error and why the NOPC does not increase regularly while we call removeBack function for each turn.

Please note that p is an integer between 0 and 3, and position is an array of integer of size 4 containing the position of each player(0,1, 2 or 3)

4

1 回答 1

0

您正在使用填充 backHands 数组

for(j=1;j<4;j++){
 backHands[j-1] = newBacks;

所以如果你调用 removeBack(3); 它试图找到 backHands[position[3] - 1],你说位置数组是这样的

var position:Array = [1,2,3,4]

所以它转向 backHands[4-1] -> backHands[3] 并且它是未定义的,因为您可以看到您的 for 循环将 j 增加到 3

backHands[1-1] = newBacks;//j=1
backHands[2-1] = newBacks;//j=2
backHands[3-1] = newBacks;//j=3

如您所见,j 没有 4,因为您输入的是 j < 4 而不是 j <=4

于 2013-10-19T12:26:30.750 回答