如何在不重复的情况下生成数组中的对象?
How to spawn objects in array without repeating?
我将 MC rockThrowers
添加到舞台的 3 个不同位置。它们使用运行良好的随机生成器随机生成。用户单击舞台上的一个按钮,rockThrowers
被添加到舞台并推入他们自己的数组 aRockThrowerArray
我希望能够检查他们生成的 3 个位置中的哪一个并且不与下一个重叠rockThrowers
已添加到舞台,如果是,则将新的添加到空位置。我尝试了不同的策略,主要是布尔值,并将它们从它们自己的 class 调用到我的主要 class 但似乎没有任何效果。这是我的
摇滚Class:
private function startPosition():void
{
// y position
this.y = (stage.stageHeight / 2) + 200;
//Start Speed
nSpeed = randomNumber(5, 8);
leftScreenSpawn = randomNumber(1, 3);
//For Left Screen
leftNeg = (stage.stageWidth / 2) - 200;
leftMiddle = (stage.stageWidth / 2) - 150;
leftPos = (stage.stageWidth / 2) - 100;
//Left Screen
if (leftScreenSpawn == 1)
{
this.x = leftNeg;
bLeftNeg = true; // Now if the left Rock thrower is destroyed then turn back to false on main engine class
}else
if (leftScreenSpawn == 2)
{
this.x = leftMiddle;
bLeftMiddle = true;
}else
if (leftScreenSpawn == 3)
{
this.x = leftPos;
bLeftPos = true;
}
//Move
startMoving();
}
现在在我的 Main Class 中,当用户单击左侧屏幕 Btn 时,我将其设置如下:
private function rockThrowerSpawn(e:MouseEvent):void
{
//Instantiate screens before hand
rockThrowerSpawnScreen.x = (stage.stageWidth / 2);
rockThrowerSpawnScreen.y = (stage.stageHeight / 2) + 200;
addChild(rockThrowerSpawnScreen);
rockThrowerSpawnScreen.left.addEventListener(MouseEvent.CLICK, chooseSpawnSideRockThrowers);
}
然后生成函数:
private function chooseSpawnSideRockThrowers(e:MouseEvent):void
{
if (e.currentTarget == rockThrowerSpawnScreen.left) // Spawn LEFT
{
//add new rock thrower
rockThrowers = new mcRockThrowers();
//Add object
addChild(rockThrowers);
//Add to Array
aRockThrowerArray.push(rockThrowers);
//trace("LEFT SPAWN");
}
//Subtract resources and update text
nResources -= 10;
updateResourceTextField();
//Remove Listeners
rockThrowerSpawnScreen.left.removeEventListener(MouseEvent.CLICK, chooseSpawnSideRockThrowers);
rockThrowerSpawnScreen.destroy();
}
我知道仅此一项就会始终产生随机位置我删除了所有无效的内容现在我回到了这个方块。关于如何实现这一目标的任何想法?感谢所有支持。
简单。您需要一个以随机顺序生成 3 个值的有限 Array。
var L:Array =
[
(stage.stageWidth / 2) - 200,
(stage.stageWidth / 2) - 150,
(stage.stageWidth / 2) - 100,
];
function fetchPosition():Number
{
// Get a random index based on the current length of L.
var anIndex:int = Math.random() * L.length;
// Record the result.
var result:Number = L[anIndex];
// Remove the result from the list.
L.splice(anIndex, 1);
return result;
}
因此,每个应用程序 运行 可以有效地调用 fetchPosition() 三次,并且每次 运行 L 的内容 将以随机顺序获取,更重要的是,您不会两次获得相同的值,因为获取的值已从数据集中删除。
我将 MC rockThrowers
添加到舞台的 3 个不同位置。它们使用运行良好的随机生成器随机生成。用户单击舞台上的一个按钮,rockThrowers
被添加到舞台并推入他们自己的数组 aRockThrowerArray
我希望能够检查他们生成的 3 个位置中的哪一个并且不与下一个重叠rockThrowers
已添加到舞台,如果是,则将新的添加到空位置。我尝试了不同的策略,主要是布尔值,并将它们从它们自己的 class 调用到我的主要 class 但似乎没有任何效果。这是我的
摇滚Class:
private function startPosition():void
{
// y position
this.y = (stage.stageHeight / 2) + 200;
//Start Speed
nSpeed = randomNumber(5, 8);
leftScreenSpawn = randomNumber(1, 3);
//For Left Screen
leftNeg = (stage.stageWidth / 2) - 200;
leftMiddle = (stage.stageWidth / 2) - 150;
leftPos = (stage.stageWidth / 2) - 100;
//Left Screen
if (leftScreenSpawn == 1)
{
this.x = leftNeg;
bLeftNeg = true; // Now if the left Rock thrower is destroyed then turn back to false on main engine class
}else
if (leftScreenSpawn == 2)
{
this.x = leftMiddle;
bLeftMiddle = true;
}else
if (leftScreenSpawn == 3)
{
this.x = leftPos;
bLeftPos = true;
}
//Move
startMoving();
}
现在在我的 Main Class 中,当用户单击左侧屏幕 Btn 时,我将其设置如下:
private function rockThrowerSpawn(e:MouseEvent):void
{
//Instantiate screens before hand
rockThrowerSpawnScreen.x = (stage.stageWidth / 2);
rockThrowerSpawnScreen.y = (stage.stageHeight / 2) + 200;
addChild(rockThrowerSpawnScreen);
rockThrowerSpawnScreen.left.addEventListener(MouseEvent.CLICK, chooseSpawnSideRockThrowers);
}
然后生成函数:
private function chooseSpawnSideRockThrowers(e:MouseEvent):void
{
if (e.currentTarget == rockThrowerSpawnScreen.left) // Spawn LEFT
{
//add new rock thrower
rockThrowers = new mcRockThrowers();
//Add object
addChild(rockThrowers);
//Add to Array
aRockThrowerArray.push(rockThrowers);
//trace("LEFT SPAWN");
}
//Subtract resources and update text
nResources -= 10;
updateResourceTextField();
//Remove Listeners
rockThrowerSpawnScreen.left.removeEventListener(MouseEvent.CLICK, chooseSpawnSideRockThrowers);
rockThrowerSpawnScreen.destroy();
}
我知道仅此一项就会始终产生随机位置我删除了所有无效的内容现在我回到了这个方块。关于如何实现这一目标的任何想法?感谢所有支持。
简单。您需要一个以随机顺序生成 3 个值的有限 Array。
var L:Array =
[
(stage.stageWidth / 2) - 200,
(stage.stageWidth / 2) - 150,
(stage.stageWidth / 2) - 100,
];
function fetchPosition():Number
{
// Get a random index based on the current length of L.
var anIndex:int = Math.random() * L.length;
// Record the result.
var result:Number = L[anIndex];
// Remove the result from the list.
L.splice(anIndex, 1);
return result;
}
因此,每个应用程序 运行 可以有效地调用 fetchPosition() 三次,并且每次 运行 L 的内容 将以随机顺序获取,更重要的是,您不会两次获得相同的值,因为获取的值已从数据集中删除。