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

时间:2017-02-07 16:41:40

标签: actionscript-3 flash

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

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

示例:

  1. 第一个测验是第3帧
  2. 第二个测验是第6帧
  3. 第3次测验是第2帧
  4. (......)
  5. (n)......(...(尚未显示的最后一帧)
  6. 我在第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 个答案:

答案 0 :(得分:2)

您需要跟踪您曾经去过哪些帧,这样您就不会重复,因此您知道测验何时完成。

最简单的方法可能是设置一个问题框数组,然后在完成问题时从该数组中删除问题框架。

这些方面的东西:

第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);