2013-05-06 69 views
0

我想要生成一个随机数并与另一个数进行比较,如果它们相同,我希望随机数增加1,然后将其添加到舞台上。但是如果它不同于我想让它直接添加到舞台上。但如果数字相同,它不能正常工作,它会通过radomize ++,但仍然会添加生成的初始数字。可以somebnody请帮助我如何解决这个问题?Number generation going wrong

function randomizedorder() 
    { 
     randomize = Math.floor(Math.random() * (choices.length)); 

     trace("the random number is" + randomize); 

    if (randomize == indexcount) { 
      randomize++; 
      trace ("it goes through this pahse" + randomize); 

     } 
     else { 
       addChild(choices [randomize]); 
     } 

    } 
+0

不知道什么是错的代码在这里看起来okayish。我认为你应该考虑事先随机/加扰列表。也许克隆数组,然后随机选择随机交换n个点,然后使用克隆数组添加(和检查)子项。 – shaunhusain 2013-05-06 18:37:56

回答

1

want the random number to increase by one and then add it to the stage

但你是一个增加它,因为的addChild是else子句中不添加任何东西的阶段。

function randomizedorder() 
{ 
    randomize = Math.floor(Math.random() * (choices.length)); 

    trace("the random number is" + randomize); 

    if (randomize == indexcount) { 
     randomize++; 
     randomize = randomize % choices.length; 
     trace ("it goes through this pahse" + randomize); 
    } 

    addChild(choices [randomize]); 

} 

还需要在做什么决定,如果随机化等于indexcount,也是在这种情况下,你不能用它来选择水下爆炸等于choices.length-1

编辑:我加入了模运算符因此,如果随机化去出界将回到0

+0

是的,这工作thx。但我也需要知道随机数是否越界,因为随机数超过了选择的长度,我该如何问,而不是增加一个减少一个?那么这是一个选择是可用的。因为它现在确实发生了,如果我有一个在1的索引,并且仍然有0的可用选项,但是如果随机数来到1,它将移动到2。我需要让它回到0我知道我可以要求它检查最后的其他人,但是我怎么问它将它添加到舞台上? – tailedmouse 2013-05-06 20:00:50

+0

编辑我的答案,你可以使用模数运算符来将'randomize'回到0,如果它超出界限。 – 2013-05-06 21:28:11

+0

oooo fancy ..非常感谢你为我教授新的东西:D也解决了这个问题:p我从来不知道这种关系。 – tailedmouse 2013-05-06 21:50:01