Flash AS3 - 如何在播放时随机化每一帧

Flash AS3 - how to randomize each frame while play

我在第 2-11 帧中有一些第 1 级测验,第 12 帧是测验的结果,第 13-22 帧中的第 2 级测验结果在第 23 帧中..

我想在 2-11 之间随机化一个测验,在 13-22 帧中随机化一个测验,

示例:

  1. 第一个测验是第 3 帧
  2. 第二个测验是第 6 帧
  3. 第三个测验是第 2 帧
  4. (... ...)
  5. 第(n) ... ... ...(最后一帧尚未显示)

我在第 1 帧中使用此代码:

stop();
autom.play();
soal = 1;
var pic:Number=11;
var randomFrame:Number = Math.ceil(Math.random()*pic);
trace(randomFrame);
gotoAndStop(randomFrame);
nextFrame();

但闪光灯只是第一次随机化

我想知道是否有办法使闪光灯 运行 像我在 示例 中想要的那样???

编辑:

full code of frame 1

var kunci:String,
jawaban:String,
dikunci:String,
soal:int,
betul:int,
salah:int,
hati:int=0,
nilai:int,
mcres:mcrespon = new mcrespon();
mcres.x = 20;
mcres.y = 40;
mcres.scaleX = 3;
mcres.scaleY = 3;

tbhome.addEventListener(MouseEvent.MOUSE_UP,clikmenu);
tbmulai.addEventListener(MouseEvent.CLICK,cliklanjut);
function cliklanjut(event:MouseEvent):void
{
    stop();
    autom.play();
    soal = 1;
    betul = 0;
    salah = 0;
    nilai = 0;6;
    var pic:Number=22;
    var randomFrame:Number = Math.ceil(Math.random()*pic);
    trace(randomFrame);
    gotoAndStop(randomFrame);
    nextFrame();
    /*stop();
    autom.play();
    soal = 1;
    var pic:Number=11;
    var randomFrame:Number = Math.ceil(Math.random()*pic);
    trace(randomFrame);
    gotoAndStop(randomFrame);
    nextFrame();*/
}
function clika(event:MouseEvent):void
{
    autom.play();
    jawaban = "a";
    cocokan();
}
function clikb(event:MouseEvent):void
{
    autom.play();
    jawaban = "b";
    cocokan();
}
function clikc(event:MouseEvent):void
{
    autom.play();
    jawaban = "c";
    cocokan();
}
function clikd(event:MouseEvent):void
{
    autom.play();
    jawaban = "d";
    cocokan();
}
function cocokan()
{
    addChild(mcres);
    if (jawaban == kunci)
    {
        mcres.gotoAndPlay(2);
        setTimeout(lanjutbenar,0);
    }
    else
    {
        mcres.gotoAndPlay(16);
        setTimeout(lanjutsalah,0);
    }
}
function copot()
{
    removeChild(mcres);
}
function lanjutbenar()
{
    betul +=  1;
    soal +=  1;
    copot();
    nextFrame();
}
function lanjutsalah()
{
    salah +=  1;
    hati +=  1;
    mchati.nextFrame();
    copot();
    if (hati>=3)
    {
        gotoAndStop("gameover");
    }
    else
    {
        soal +=  1;
        nextFrame();
    }
}
function clikulang(event:MouseEvent):void
{
    autom.play();
    soal = 1;
    betul = 0;
    salah = 0;
    nilai = 0;
    hati = 0;
    gotoAndStop(1);
}
function kuncinya(sikunci:String)
{
    soalnya.text = "Soal no " + soal.toString() + "/20";
    pila.addEventListener(MouseEvent.CLICK,clika);
    pilb.addEventListener(MouseEvent.CLICK,clikb);
    pilc.addEventListener(MouseEvent.CLICK,clikc);
    pild.addEventListener(MouseEvent.CLICK,clikd);
    kunci = sikunci;
}

function diresumequis(batasbagus:int,komen1:String,komen2:String,komen3:String)
{
    stop();
    tbulang.addEventListener(MouseEvent.CLICK,clikulang);
    tbnextlevel.addEventListener(MouseEvent.CLICK,cliklanjut);
    betulnya.text = "Benar = " + betul.toString();
    salahnya.text = "Salah = " + salah.toString();
    nilai = betul / 10 * 100;
    nilainya.text = "Nilai = " + nilai.toString();

    if (nilai == 100)
    {
        komentar.text = komen1;
    }
    else
    {
        if (nilai>=batasbagus)
        {
            komentar.text = komen2;
        }
        else
        {
            komentar.text = komen3;
        }
    }
}

in frame 2 till 11 are a same code like this :

kuncinya("a") // the correct answer of a question;  

您需要跟踪您去过的帧,这样您就不会重复,这样您就知道测验何时完成。

执行此操作的最简单方法可能是拥有一个问题框架数组,然后在您完成问题时从该数组中删除问题框架。

大致如下:

第1帧代码:

//a var to hold the current quiz (an array/collection of frames)
var curQuiz:Array;

//a var to hold the ending frame of the current quiz
var curQuizEndFrame:int;

 //the frame number or labels that are questions for this quiz
var quiz1:Array = [2,3,4,5,6,7,8,9,10,11];
var quiz2:Array = [13,14,15,16]; //etc

//create a function to start a quiz with two parameters - the quiz array to start, and the frame to goto once all the questions have been asked.
function startQuiz(quiz:Array, endFrame:int):void {
    curQuizEndFrame = endFrame;
    curQuiz = quiz.concat(); //this copies the passed in quiz array (so you don't modify the original)

    //randomize the question order
    curQuiz.sort(randomizeArray);

    //now call nextQuestion (created below) to go to the first question
    nextQuestion(); 
}

//create a function to show the next question. Call this whenever the user answers a question - the e parameter is just there in case you want to call this function from an event handler
function nextQuestion(e:Event = null):void {
    //check if there is a current quiz, and if there are still questions left in it
    if(curQuiz && curQuiz.length > 0){
        //goto the next question in the quiz
        gotoAndStop(curQuiz.pop()); //pop removes the last element from the array and returns its value
    }else{
        if(curQuiz){
            //if we get here, it means the quiz exists but doesn't have any questions left, so go to the end frame
            gotoAndStop(curQuizEndFrame); //quiz is finished
        }else{
            //quiz hasn't started yet, do something like tell the user to start the quiz
        }
    }
}

//a randomize sort function for arrays
function randomizeArray(a:*,b:*):int {
    return(Math.random() > .5) ? 1 : -1;
}

然后,每当您完成一个问题时,调用 nextQuestion(); 这可能会出现在每个问题框架的下一个按钮单击处理程序上。

nextBtn.addEventListener(MouseEvent.CLICK, nextQuestion, false, 0, true);

每当您想开始新的测验时,调用 startQuiz 并传入测验数组和结束帧。

例如:

startQuiz(quiz1, 12);