Just been trying to randomise a bunch of button coordinates and managed this successfully using this code...
刚试图随机化一堆按钮坐标并使用此代码成功管理...
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);
This code works and when I start the game, the coordinates of the buttons randomise.
这段代码有效,当我开始游戏时,按钮的坐标随机化。
The problem I am having is I want these coordinates to randomise every time there is a correct answer.
我遇到的问题是我希望每次有正确答案时这些坐标都是随机的。
My code for this is...
我的代码是......
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);
}
}
Most of the checkAnswers function is irrelevant to the question I think, it just shows how the function works by checking if two things match, something needs to happen.
大多数checkAnswers函数与我认为的问题无关,它只是通过检查两个事物是否匹配来显示函数是如何工作的,需要发生一些事情。
When I run this, the buttons randomise like normal... but when I get a correct answer I get this error on my Output
当我运行它时,按钮随机正常...但是当我得到正确答案时,我的输出上出现此错误
RangeError: Error #1125: The index 0 is out of range 0.
RangeError:错误#1125:索引0超出范围0。
...And they do not randomise anymore.
......他们不再随意了。
Any ideas SO? Would be much appreciated.
有什么想法吗?非常感谢。
EDIT
Per Aarons suggestion, my revised code is...
Per 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);
}
I then added to my checkAnswers() function...
然后我添加到我的checkAnswers()函数中......
positionButtonsRandomly();
It now works seamlessly with no Range Errors.
它现在无缝地工作,没有范围错误。
Thanks! All hail Aaron.
谢谢!所有人都欢呼亚伦。
1 个解决方案
#1
1
You need to re-create your coordinates each time since you are removing all the points using splice()
when you pick a random position.
您需要每次重新创建坐标,因为当您选择随机位置时使用splice()移除所有点。
For example, instead of using a top level coordinates
var, you could create a function to create the possible coordinates:
例如,您可以创建一个函数来创建可能的坐标,而不是使用*坐标var:
function createCoordinatePositions():Vector.<Point> {
return new <Point>[
new Point(44, 420),
new Point(270, 420),
new Point(44, 550),
new Point(270, 550)
];
}
Then modify your random position function to take an argument of possible coordinates:
然后修改随机位置函数以获取可能坐标的参数:
function positionAtRandomCoordinate(object:DisplayObject, coordinates:Vector.<Pont>):void {
// Same as before
}
Now to position all your buttons randomly you first create a new list of possible coordinates and pass it into each call to randomly position:
现在,随机定位所有按钮,首先创建一个新的可能坐标列表,并将其传递给每个随机定位的调用:
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
}
#1
1
You need to re-create your coordinates each time since you are removing all the points using splice()
when you pick a random position.
您需要每次重新创建坐标,因为当您选择随机位置时使用splice()移除所有点。
For example, instead of using a top level coordinates
var, you could create a function to create the possible coordinates:
例如,您可以创建一个函数来创建可能的坐标,而不是使用*坐标var:
function createCoordinatePositions():Vector.<Point> {
return new <Point>[
new Point(44, 420),
new Point(270, 420),
new Point(44, 550),
new Point(270, 550)
];
}
Then modify your random position function to take an argument of possible coordinates:
然后修改随机位置函数以获取可能坐标的参数:
function positionAtRandomCoordinate(object:DisplayObject, coordinates:Vector.<Pont>):void {
// Same as before
}
Now to position all your buttons randomly you first create a new list of possible coordinates and pass it into each call to randomly position:
现在,随机定位所有按钮,首先创建一个新的可能坐标列表,并将其传递给每个随机定位的调用:
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
}