以下一段as3代码是为纸牌游戏编写的,我很好奇,为什么我得到错误#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 ++;
}
这是错误日志:
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
问题是哪条线出错,以及为什么当我们为每圈调用 removeBack 函数时,NOPC 不会定期增加。
请注意,p 是介于 0 和 3 之间的整数,位置是大小为 4 的整数数组,包含每个玩家的位置(0、1、2 或 3)
你正在使用填充反手数组
for(j=1;j<4;j++){
backHands[j-1] = newBacks;
所以如果你调用 removeBack(3); 它试图找到 backHands[position[3] - 1],你说位置数组是这样的
var position:Array = [1,2,3,4]
所以它变成了反手[4-1] ->反手[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