Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Actionscript 3 错误1010操作脚本3_Actionscript 3_Loops_Error Handling - Fatal编程技术网

Actionscript 3 错误1010操作脚本3

Actionscript 3 错误1010操作脚本3,actionscript-3,loops,error-handling,Actionscript 3,Loops,Error Handling,使用此while循环时出现错误#1010: while (pos.length>0) { coo = pos.splice(Math.floor(Math.random() * pos.length),1)[0]; (pos_array[index]).x = coo.x; (pos_array[index]).y = coo.y; index++; } 错误显示:术语未定义且没有属性。 我的循环有什么问题,因为我对其他程序使用了相同的循环,但没有得到这

使用此while循环时出现错误#1010:

while (pos.length>0)
{
    coo = pos.splice(Math.floor(Math.random() * pos.length),1)[0];

    (pos_array[index]).x = coo.x;
    (pos_array[index]).y = coo.y;
    index++;
}
错误显示:
术语未定义且没有属性。

我的循环有什么问题,因为我对其他程序使用了相同的循环,但没有得到这样的错误


感谢您的关注。

在不知道集合包含什么的情况下,我假设集合中填充了DisplayObjects或具有x和y属性的对象

强制转换引用,以便编译器理解集合包含的内容。例如:

DisplayObject(pos_array[index]).x = coo.x;
DisplayObject(pos_array[index]).y = coo.y;

…或集合包含的任何类型。

您的while循环正在中断

pos.length永远不会改变,最终
pos\u数组[索引]
将超出范围

当你出界时,它是未定义的。 所以基本上你是在做

undefined.x = coo.x;
正如错误所说,undefined没有属性

我看不出这个循环是如何运作的

试试这个,更干净

var savedX:Number = 0
for each( var obj:Object in pos_array ){
  coo = new MovieClip()
  coo = pos.splice(Math.floor(Math.random() * pos.length),1)[0];
  obj.x = savedX;
  obj.y = 0;
  savedX += coo.width;
}

循环开始时,pos.lengthpos_array.length可能不相等

试试这个:

while (pos.length>0)
{

    coo = pos.splice(Math.floor(Math.random() * pos.length),1)[0];
    if (pos_array[index])
    {
        (pos_array[index]).x = coo.x;
        (pos_array[index]).y = coo.y;
    }
    index++;

}

从这个代码看不出来。”“pos”、“pos_数组”、“pos_数组”中的任何元素、“pos”和“index”中的任何元素都可能未定义。它们都已定义:var coo:Object:var pos_数组:array=new array();var指数:uint=0;“pos”和“pos_数组”中的元素是什么?错误在哪一行抛出?你没有调试器吗?一切都很好。我使用调试器查看它是哪一行。错误就在循环中。但不知道在设置“pos_array”的“x”属性时,“pos_array”中的元素已经存在于何处?e、 g.如果执行“(pos_array[0]).x=coo.x”,数组中该位置是否有属性为x的类的实例?如果我将循环条件更改为:“while(pos.length==0)”,则可以工作是的,数组包含电影剪辑,但这不是解决方案,因为我得到了一个新错误“undefined method”我试过了,没有出错,但mc并不像以前那样并排排列。它们现在已经堆放好了。我怎样把它们放在一边?我已经准备好了数组中的所有存储位置。你的逻辑有缺陷。您使用一个数组来存储位置,这很好,但是,您可以随机访问它们,这样就有可能多次使用一个位置。“使用”该位置后,您需要从pos数组中删除元素。我会这样做:对于每个(pos_数组中的var obj:Object){coo=pos.splice(Math.floor(Math.random()*pos.length),1)[0];obj.x+=loader.width/4;obj.y=0;if(x>loader.width){x=0;y=loader.height/2;}我不确定这段代码是否能实现您所期望的功能。我会首先验证pos的值,并确保它们都是好位置。好的,你能告诉我一种方法吗。我已经在一个阵列中存储了一些mc,我想把这些mc并排放在舞台上。我该怎么做?非常感谢。