数字生成错了

时间:2013-05-06 18:22:26

标签: actionscript-3

我正在尝试生成一个随机数并与另一个数字进行比较,如果它们是相同的,我希望随机数增加1,然后将其添加到舞台上。但如果它开始不同我想让它直接添加到舞台上。但如果数字相同则它不能正常工作,它确实通过radomize ++但仍然添加了生成的所有内容。 somebnody能帮助我解决这个问题吗?

 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]);
        }

    }

1 个答案:

答案 0 :(得分: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]);

}

此外,如果randomize等于indexcount并且也等于choices.length-1,则需要决定该做什么,在这种情况下,您不能将其用于undex选择。

编辑:我添加了模数运算符,所以如果随机化超出范围,它将返回到0。