首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Flash AS3 -如何在播放时将每个帧随机化

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

Stack Overflow用户
提问于 2017-02-07 16:41:40
回答 1查看 302关注 0票数 0

我在框架2-11中有一些测试级别1,框架12是测试的结果,在框架13-22中有测试2级,在框架23中有结果。

我想把2-11之间的测试随机化,在13-22帧中随机化测试,

示例:

  1. 第一项测验是第三组。
  2. 第二项测验是第六组。
  3. 第三项测验是第二组。
  4. (.)
  5. (N)第.(尚未显示的最后一帧)

我在框架1中使用这个代码:

代码语言:javascript
复制
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

代码语言:javascript
复制
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 :

代码语言:javascript
复制
kuncinya("a") // the correct answer of a question;  
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-02-07 18:00:43

你需要跟踪你去过的帧,这样你就不会重复,这样你就知道什么时候完成测试了。

最容易做到这一点的方法可能是有一个问题框架数组,然后在完成问题时从该数组中删除问题框架。

类似于这样的东西:

框架1代码:

代码语言:javascript
复制
//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(); --这很可能是在next按钮上单击每个问题框架上的处理程序。

代码语言:javascript
复制
nextBtn.addEventListener(MouseEvent.CLICK, nextQuestion, false, 0, true);

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

例如:

代码语言:javascript
复制
startQuiz(quiz1, 12);
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42095345

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档