AS3做3次随机停留,永远不会彼此相等

时间:2012-10-28 17:40:00

标签: actionscript-3

我有3个动画片段,每个片段有7个帧和标签可以播放不同的图像,我想随机停止每一个,但永远不要让它们等于同一帧。我曾经在思考array.push,但我不知道该怎么做。所以到目前为止我有这个:

function startGame(event:MouseEvent)
{
    addChild(level6_mc);
    addChild(inGameNav_mc);
    level6_mc.gotoAndPlay(2);
    var timer = setTimeout(startAgain, 1000);
    startAgain();
}


// level 6
function startAgain()
{
    var randomNumber:Number= Math.floor(Math.random()*7);
    var door1 = level6_mc.door1_mc;
    var door2 = level6_mc.door2_mc;
    var door3 = level6_mc.door3_mc;
    door1.gotoAndStop(randomNumber);
    door2.gotoAndStop(randomNumber);
    door3.gotoAndStop(randomNumber);
}

必须有一个简单的方法,我只是无法弄明白。我一直在网上寻找解决方案,但每种方法都是如此复杂。任何人都可以帮我找到一个简单的解决方案,如果你告诉我使用数组你能举个例子。提前致谢

3 个答案:

答案 0 :(得分:1)

我会选择这样的方法:

function startGame(event:MouseEvent){
    addChild(level6_mc);
    addChild(inGameNav_mc);
    level6_mc.gotoAndPlay(2);
    var timer= setTimeout(startAgain, 1000);
    startAgain();
}

function startAgain(){
    var randomFrames:Array = getRandomFrames(3);
    var door1 = level6_mc.door1_mc;
    var door2 = level6_mc.door2_mc;
    var door3 = level6_mc.door3_mc;
    door1.gotoAndStop(randomFrames[0]);
    door2.gotoAndStop(randomFrames[1]);
    door3.gotoAndStop(randomFrames[2]);
}


/*
   Given input of 3, the return should be an array of integers randomly 
   chosen from the array defined by `frames`.  An example would be `[5,2,7]`
*/
function getRandomFrames(var $frameCount:Number):Array {
    var frames:Array = [1, 2, 3, 4, 5, 6, 7]
    var randomFrames:Array = new Array(Math.min($frameCount, frames.length));

    var pos:Number = 0;
    for (var i:int = 0; i < randomFrames.length; i++)
    {
        pos = int(Math.random() * frames.length);
        randomFrames[i] = frames.splice(pos, 1)[0];
    }
    return randomFrames
}

我这台机器上没有Flash,所以我无法测试。但是这个想法是你有一个1-7的帧数,你可以随机选择其中3个作为你的停止帧。

答案 1 :(得分:1)

由于帧数较少,您可以将帧拼接出阵列:

function startGame(event:MouseEvent){
        addChild(level6_mc);
        addChild(inGameNav_mc);
        level6_mc.gotoAndPlay(2);
        var timer= setTimeout(startAgain, 1000);
        startAgain();
    }


    //level 6
    function startAgain(){
            var door1 = level6_mc.door1_mc;
            var door2 = level6_mc.door2_mc;
            var door3 = level6_mc.door3_mc;
            var stopsAt:Array = [1,2,3,4,5,6];
            door1.gotoAndStop(stopsAt.splice(Math.random()*stopsAt.length, 1)[0]);
            door2.gotoAndStop(stopsAt.splice(Math.random()*stopsAt.length, 1)[0]);
            door3.gotoAndStop(stopsAt.splice(Math.random()*stopsAt.length, 1)[0]);

答案 2 :(得分:0)

var prevNum:int = 0;

function startGame(event:MouseEvent)
{
addChild(level6_mc);
addChild(inGameNav_mc);
level6_mc.gotoAndPlay(2);
var timer = setTimeout(startAgain, 1000);
startAgain();
}

//等级6

function startAgain()
{
var randomNumber:int= getRandomNumber(7,prevNum);
prevNum = randomNumber;
var door1 = level6_mc.door1_mc;
var door2 = level6_mc.door2_mc;
var door3 = level6_mc.door3_mc;
door1.gotoAndStop(randomNumber);
door2.gotoAndStop(randomNumber);
door3.gotoAndStop(randomNumber);
}

function getRandomNumber(length:int,previousNumber:int):int
{ 
    var currentNumber:int;
    do{

  currentNumber = Math.floor(1+Math.random()*length);

}while(currentNumber==previousNumber);

return currentNumber;
}