RangeError: Error #1125: The index 0 is out of range 0

RangeError: Error #1125: The index 0 is out of range 0

刚刚尝试随机化一堆按钮坐标并使用此代码成功地管理了它...

var coordinates:Vector.<Point> = new <Point>[
new Point(44, 420),
new Point(270, 420),
new Point(44, 550),
new Point(270, 550)
];


function positionAtRandomCoordinate(object:DisplayObject):void {
var index:int = Math.random() * coordinates.length;
var coordinate:Point = coordinates.splice(index, 1)[0];
object.x = coordinate.x;
object.y = coordinate.y;
}

positionAtRandomCoordinate(answerButtons);
positionAtRandomCoordinate(answerButtons_2);
positionAtRandomCoordinate(answerButtons_3);
positionAtRandomCoordinate(answerButtons_4);

此代码有效,当我开始游戏时,按钮的坐标随机化。

我遇到的问题是每次有正确答案时我都希望这些坐标随机化。

我的代码是...

function checkAnswers() {
if (questionColour == answerColour){
    textLabeller(textBox);
    colourLabeller(textBox);
    updateScore();
positionAtRandomCoordinate(answerButtons);
positionAtRandomCoordinate(answerButtons_2);
positionAtRandomCoordinate(answerButtons_3);
positionAtRandomCoordinate(answerButtons_4);


    if (score > sharedData.data.highScore){
        sharedData.data.highScore = score;
        sharedData.flush();
        updateHiScore();


    }
    SecondsToCountDown = 6;

    }else {
    trace ("colours do not match");
    CountDownTimer.stop();
     gotoAndStop(3);
}

}

大多数 checkAnswers 函数与我认为的问题无关,它只是通过检查两个事物是否匹配来显示该函数的工作原理,是否需要发生某些事情。

当我 运行 这样做时,按钮像正常情况一样随机化...但是当我得到正确答案时,我的输出出现此错误

RangeError:错误 #1125:索引 0 超出范围 0。

...而且他们不再随机化了。

有什么想法吗?将不胜感激。

编辑

根据 Aarons 的建议,我修改后的代码是...

function createCoordinatePositions():Vector.<Point> {
return new <Point>[
    new Point(44, 420),
    new Point(270, 420),
    new Point(44, 550),
    new Point(270, 550)
];
}

function positionAtRandomCoordinate(object:DisplayObject, coordinates:Vector.<Point>):void {
var index:int = Math.random() * coordinates.length;
var coordinate:Point = coordinates.splice(index, 1)[0];
object.x = coordinate.x;
object.y = coordinate.y;
}

function positionButtonsRandomly():void {
var coordinates:Vector.<Point> = createCoordinatePositions();
positionAtRandomCoordinate(answerButtons, coordinates);
positionAtRandomCoordinate(answerButtons_2, coordinates);
positionAtRandomCoordinate(answerButtons_3, coordinates);
positionAtRandomCoordinate(answerButtons_4, coordinates);
}

然后我添加到我的 checkAnswers() 函数中...

positionButtonsRandomly();

它现在可以无缝运行,没有范围错误。

谢谢!向亚伦致敬。

您每次都需要重新创建坐标,因为您在选择随机位置时使用 splice() 删除了所有点。

例如,您可以创建一个函数来创建可能的坐标,而不是使用顶级 coordinates 变量:

function createCoordinatePositions():Vector.<Point> {
    return new <Point>[
        new Point(44, 420),
        new Point(270, 420),
        new Point(44, 550),
        new Point(270, 550)
    ];
}

然后修改您的随机位置函数以获取可能坐标的参数:

function positionAtRandomCoordinate(object:DisplayObject, coordinates:Vector.<Pont>):void {
    // Same as before
}

现在要随机放置所有按钮,您首先要创建一个新的可能坐标列表,并将其传递到每个随机位置调用中:

function positionButtonsRandomly():void {
    var coordinates:Vector.<Point> = createCoordinatePositions();
    positionAtRandomCoordinate(answerButtons, coordinates);
    positionAtRandomCoordinate(answerButtons_2, coordinates);
    positionAtRandomCoordinate(answerButtons_3, coordinates);
    positionAtRandomCoordinate(answerButtons_4, coordinates);
    // By here the coordinates.length = 0
}