Arrays AS3-通过数组元素导航

Arrays AS3-通过数组元素导航,arrays,actionscript-3,random,Arrays,Actionscript 3,Random,你好,谢谢你抽出时间。我有一个as3代码,它从7帧中随机选取5帧,没有重复 var mygroup1:RadioButtonGroup = new RadioButtonGroup("group1"); q1a1.group = q1a2.group = q1a3.group = q1a4.group = q1a5.group = mygroup1; var number_array:Array = [8158,8159,8160,8161,8162,8163,8164]; var final

你好,谢谢你抽出时间。我有一个as3代码,它从7帧中随机选取5帧,没有重复

var mygroup1:RadioButtonGroup = new RadioButtonGroup("group1");
q1a1.group = q1a2.group = q1a3.group = q1a4.group = q1a5.group = mygroup1;

var number_array:Array = [8158,8159,8160,8161,8162,8163,8164];
var final_array:Array = [];
var count_selected:int = 5;
var i:int;

for(i = 0; i < count_selected; i++)
   {
      if(number_array.length == 0)
         break;
      else
         final_array.push(number_array.splice(Math.floor(Math.random() * number_array.length), 1)[0]);
   }

var index = 0;
var currentQuestion:int = final_array[index];
var answers:Object = {
    8158: 'B) 12',
    8159: 'F) All of the above',
    8160: 'A) True',
    8161: 'B) False',
    8162: 'C) 4',
    8163: 'F) D and E',
    8164: 'B) B'
};

var getAnswer = mygroup1.selection.label;  //Getting the selection from RadioButtonGroup

submitBtn.addEventListener(MouseEvent.CLICK, onSubmitClicked);

function onSubmitClicked(e:MouseEvent):void {
var answer:String = getAnswer();
   if (answer === answers[currentQuestion]) 
      awardScore(currentQuestion);
      ++index;
      currentQuestion = final_array[index];
      gotoAndStop(final_array[index]);
   }

我的问题是…由于用户只会随机选择7帧中的5帧,我如何确保所有7帧中的所有按钮都会收听并遵循gotoAndStop(最终数组[]);语句的顺序是5?因为在最后,两帧将被省略,这两帧将在每一卷中随机更改。我希望我能解释我的困境。再次感谢您。

听起来您只需要一个进入下一帧的提交按钮。这样你就不用担心订单了

一些示例代码让您了解它的工作原理

var index = 0;
var currentQuestion:int = final_array[index];
var answers:Object = {
  8158: '12',
  8159: '13'
  //...etc.
};

submitBtn.addEventListener(MouseEvent.CLICK, onSubmitClicked);

function onSubmitClicked(e:MouseEvent):void {
   var answer:String = getAnswer(); //get the user's answer
   if (answer === answers[currentQuestion]) {
      awardScore(currentQuestion);
      ++index;
      currentQuestion = final_array[index];
      gotoAndStop(final_array[index]);
   }
}

我们的想法是将所有这些逻辑都放在一个单击处理程序中,一旦单击,它将抓取用户的答案并检查其是否正确,如果正确,然后继续下一个问题。

要做像您这样的测试,我认为您不需要在每个帧上复制所有内容。您可以将问题放入MovieClips(或一个具有多个帧的MovieClip),然后将其添加到您的舞台上,对于您的复选框,您可以在每次更改其值和标签时创建一次。对于按钮,您只需要一个按钮来验证和检查当前问题以及转到下一个问题

看看这个例子:

var score:int;
var max_score:int;
const default_score:int = 20;

// create our questions list (7 questions) : 
var questions_list:Object = {
    // index        : to identify our question
    // movie_clip   : the class name used for AS linkage    
    1: { index : 1, movie_clip : Question1, good_answer : 'css3', first_answer : 'html5', second_answer : 'jquery', third_answer : 'css3', score : default_score }, 
    2: { index : 2, movie_clip : Question2, good_answer : 'html5', first_answer : 'html5',  second_answer : 'less', third_answer : 'ruby', score : 50 }, 
    3: { index : 3, movie_clip : Question3, good_answer : 'jquery', first_answer : 'ruby',  second_answer : 'jquery', third_answer : 'css3', score : default_score }, 
    4: { index : 4, movie_clip : Question4, good_answer : 'less', first_answer : 'less', second_answer : 'html5', third_answer : 'css3', score : 15 }, 
    5: { index : 5, movie_clip : Question5, good_answer : 'nodejs', first_answer : 'python', second_answer : 'nodejs', third_answer : 'jquery', score : 10 }, 
    6: { index : 6, movie_clip : Question6, good_answer : 'python', first_answer : 'css3', second_answer : 'html5', third_answer : 'python', score : default_score }, 
    7: { index : 7, movie_clip : Question7, good_answer : 'ruby', first_answer : 'ruby', second_answer : 'html5', third_answer : 'less', score : default_score }
};

var enabled_questions:Array = [1, 2, 3, 4, 5, 6, 7];    
var current_question:int = 0;

//----------------------------------------------------------------------------------------------------

// sprite that shows our questions 
var questions_container:Sprite = new Sprite();
    questions_container.x = questions_container.y = 20;
    questions_container.visible = false;
    addChild(questions_container);

var checks_group:RadioButtonGroup = new RadioButtonGroup('checks_group');

// sprite that contains our 3 check box 
var checks_container:Sprite = new Sprite();
    checks_container.visible = false;
    checks_container.x = int(stage.stageWidth / 2);
    checks_container.y = 50;
    addChild(checks_container);

// create our check box, 3 buttons in my case
// the 4th one is used only to uncheck every time all check box
for(var i:int = 0; i < 4; i++)
{
    var btn_check:RadioButton = new RadioButton();
        if(i != 3){
            btn_check.y = i * 30;       
        } else {

            // our 4th check box is hidden
            btn_check.visible = false;
        }
        btn_check.group = checks_group; 
        btn_check.name = 'btn_check' + i;
        checks_container.addChild(btn_check);
}

// start button
var btn_start:Button = new Button();
    btn_start.label = 'Start ! ';
    btn_start.width = 180;
    btn_start.height = 60;
    btn_start.x = int((stage.stageWidth - btn_start.width)/2);
    btn_start.y = int((stage.stageHeight - btn_start.height)/2) - 20;
    btn_start.addEventListener(MouseEvent.CLICK, startQuiz);
    addChild(btn_start);

// next button, to go to the next question
var btn_next:Button = new Button()
    btn_next.label = 'Next  >> ';
    btn_next.width = 90;
    btn_next.height = 30;
    btn_next.visible = false;
    btn_next.x = stage.stageWidth - btn_next.width - 10;
    btn_next.y = stage.stageHeight - btn_next.height - 10;
    btn_next.addEventListener(MouseEvent.CLICK, checkAnswer);
    addChild(btn_next); 

// a text field which will show the score and the current question number
var txt_score:TextField = new TextField();
    txt_score.width = 200;
    txt_score.height = 30;
    txt_score.x = stage.stageWidth - txt_score.width;
    txt_score.y = 10;
    txt_score.visible = false;
    txt_score.selectable = false;
    addChild(txt_score);

//----------------------------------------------------------------------------------------------------

function startQuiz(e:MouseEvent):void
{   
    // here we will get only 5 questions
    // I used a method from http://stackoverflow.com/questions/11980657/as3-random-array-randomize-array-actionscript-3 to shuffle the array
    // and then I remove the last 2 elements from the array
    enabled_questions = enabled_questions.sort(function(i:*,j:*){return(Math.random()<.5)?-1:1;}).slice(0,enabled_questions.length-2);

    for(var i:int = 0; i < enabled_questions.length; i++){
        var q_i:int = enabled_questions[i];
        var q:Object = questions_list[q_i];
        max_score += q.score;

        // create our questions instance and hide it
        // every question is an instance of a movieclip in our library
        // we can alse use only one movieclip and then we can use its timeline to go from a question to another
        var question = new (q.movie_clip);
            question.index = q_i;
            question.visible = false;
            questions_container.addChild(question);
    }       

    // hide the start button
    e.target.visible = false;

    // show other elements 
    questions_container.visible = checks_container.visible = btn_next.visible = txt_score.visible = true    

    // load the first question
    loadQuestion(current_question);

}

// check the answer and update score
function checkAnswer(e:MouseEvent):void 
{   
    var question:Object = questions_list[enabled_questions[current_question]];

    if(question.good_answer == checks_group.selectedData){

        // update the score
        setScore(question.score);       
    }

    if(current_question < enabled_questions.length - 1){        

        current_question ++;
        loadQuestion(current_question);

    } else {

        e.target.x = e.target.y = 0;
        e.target.enabled = false;
        e.target.width = stage.stageWidth;
        e.target.height = stage.stageHeight;
        e.target.label = 'You have finished the quiz, your score is : ' + score;

        checks_container.visible = questions_container.visible = txt_score.visible = false;

    }
}

// load question (show it) and update our check box
function loadQuestion(index:int):void 
{   
    var question:Object = questions_list[enabled_questions[index]];

    for(var i:int = 0; i < checks_container.numChildren; i++){      
        if(checks_container.getChildAt(i) is RadioButton){
            var btn:RadioButton = RadioButton(checks_container.getChildAt(i));          
            switch (btn.name){              
                case 'btn_check0' : btn.value = btn.label = question.first_answer; break;
                case 'btn_check1' : btn.value = btn.label = question.second_answer; break;
                case 'btn_check2' : btn.value = btn.label = question.third_answer; break;   
                case 'btn_check3' : btn.selected = true; break; 
            }               
        }       
    }

    for( i=0; i < questions_container.numChildren; i++){            
        if(questions_container.getChildAt(i) is MovieClip){
            var mc:MovieClip = MovieClip(questions_container.getChildAt(i));
                mc.visible = mc.index == question.index;    
        }       
    }

    // setScore is used here just to update the question number in the score text field
    setScore();

}

// show the score and current question
function setScore(new_score:int = 0){
    score += new_score;
    txt_score.text = 'Score : ' + score.toString() + ' / ' + max_score.toString() + ', Question : ' + (current_question+1) + ' / ' + enabled_questions.length;
}

// icons used in this example by Jozef Krajčovič : https://www.iconfinder.com/Jozef89
var评分:int;
var max_分数:int;
常量默认值:int=20;
//创建我们的问题列表(7个问题):
变量问题列表:对象={
//索引:确定我们的问题
//电影剪辑:用作链接的类名
1:{索引:1,电影剪辑:问题1,好答案:'css3',第一个答案:'html5',第二个答案:'jquery',第三个答案:'css3',分数:默认分数},
2:{索引:2,电影剪辑:问题2,好答案:“html5”,第一个答案:“html5”,第二个答案:“更少”,第三个答案:“ruby”,分数:50},
3:{索引:3,电影剪辑:问题3,好答案:'jquery',第一个答案:'ruby',第二个答案:'jquery',第三个答案:'css3',分数:默认分数},
4:{索引:4,电影剪辑:问题4,好答案:“更少”,第一个答案:“更少”,第二个答案:“html5”,第三个答案:“css3”,分数:15},
5:{索引:5,电影剪辑:问题5,好答案:“nodejs”,第一个答案:“python”,第二个答案:“nodejs”,第三个答案:“jquery”,分数:10},
6:{索引:6,电影剪辑:问题6,好答案:“python”,第一个答案:“css3”,第二个答案:“html5”,第三个答案:“python”,分数:默认分数},
7:{索引:7,电影剪辑:问题7,好答案:“ruby”,第一个答案:“ruby”,第二个答案:“html5”,第三个答案:“less”,分数:默认分数}
};
启用var的_问题:数组=[1,2,3,4,5,6,7];
var当前问题:int=0;
//----------------------------------------------------------------------------------------------------
//显示我们问题的精灵
var questions_container:Sprite=新Sprite();
问题\容器。x=问题\容器。y=20;
问题\u container.visible=false;
addChild(问题和容器);
var checks_group:RadioButtonGroup=新的RadioButtonGroup(“checks_group”);
//包含3个复选框的精灵
var检查容器:Sprite=newsprite();
检查_container.visible=false;
检查容器x=int(stage.stageWidth/2);
检查容器y=50;
addChild(检查容器);
//创建我们的复选框,我的案例中有3个按钮
//第四个选项仅用于取消选中“每次全部”复选框
对于(变量i:int=0;i<4;i++)
{
var btn_检查:RadioButton=新RadioButton();
如果(i!=3){
btn_check.y=i*30;
}否则{
//我们的第四个复选框被隐藏
btn_check.visible=false;
}
btn_check.group=checks_group;
btn_check.name='btn_check'+i;
检查容器addChild(btn检查);
}
//开始按钮
var btn_start:Button=new Button();
btn_start.label='start!';
btn_起始宽度=180;
btn_起始高度=60;
btn_start.x=int((stage.stageWidth-btn_start.width)/2);
btn_start.y=int((stage.stageHeight-btn_start.height)/2)-20;
btn_start.addEventListener(MouseEvent.CLICK,startQuiz);
addChild(btn_开始);
//“下一步”按钮,转到下一个问题
var btn_next:Button=new Button()
btn_next.label='next>>';
btn_next.width=90;
btn_next.height=30;
btn_next.visible=false;
btn_next.x=stage.stageWidth-btn_next.width-10;
btn_next.y=stage.stageHeight-btn_next.height-10;
btn_next.addEventListener(MouseEvent.CLICK,checkAnswer);
addChild(btn_下一步);
//显示分数和当前问题编号的文本字段
var txt_分数:TextField=new TextField();
txt_score.width=200;
txt_score.height=30;
txt_score.x=stage.stageWidth-txt_score.width;
txt_得分y=10;
txt_score.visible=false;
txt_score.selective=false;
addChild(txt_分数);
//----------------------------------------------------------------------------------------------------
函数startQuiz(e:MouseEvent):void
{   
//这里我们只会有5个问题
//我使用了一种来自http://stackoverflow.com/questions/11980657/as3-random-array-randomize-array-actionscript-3 洗牌
//然后我从数组中删除最后2个元素

enabled_questions=enabled_questions.sort(函数(i:*,j:*){return(Math.random())谢谢Baris。我现在正在尝试实现您的代码,并在执行过程中更新原始代码。如果您能关注线程,我将不胜感激。在上面更新的代码中,我得到错误#1006:值不是函数。调试说“答案未定义”还有,我担心“awardScore”行会有错误,因为它还没有被分配。但我还不能到达那个部分。有什么建议吗?非常感谢你花这么多时间
var score:int;
var max_score:int;
const default_score:int = 20;

// create our questions list (7 questions) : 
var questions_list:Object = {
    // index        : to identify our question
    // movie_clip   : the class name used for AS linkage    
    1: { index : 1, movie_clip : Question1, good_answer : 'css3', first_answer : 'html5', second_answer : 'jquery', third_answer : 'css3', score : default_score }, 
    2: { index : 2, movie_clip : Question2, good_answer : 'html5', first_answer : 'html5',  second_answer : 'less', third_answer : 'ruby', score : 50 }, 
    3: { index : 3, movie_clip : Question3, good_answer : 'jquery', first_answer : 'ruby',  second_answer : 'jquery', third_answer : 'css3', score : default_score }, 
    4: { index : 4, movie_clip : Question4, good_answer : 'less', first_answer : 'less', second_answer : 'html5', third_answer : 'css3', score : 15 }, 
    5: { index : 5, movie_clip : Question5, good_answer : 'nodejs', first_answer : 'python', second_answer : 'nodejs', third_answer : 'jquery', score : 10 }, 
    6: { index : 6, movie_clip : Question6, good_answer : 'python', first_answer : 'css3', second_answer : 'html5', third_answer : 'python', score : default_score }, 
    7: { index : 7, movie_clip : Question7, good_answer : 'ruby', first_answer : 'ruby', second_answer : 'html5', third_answer : 'less', score : default_score }
};

var enabled_questions:Array = [1, 2, 3, 4, 5, 6, 7];    
var current_question:int = 0;

//----------------------------------------------------------------------------------------------------

// sprite that shows our questions 
var questions_container:Sprite = new Sprite();
    questions_container.x = questions_container.y = 20;
    questions_container.visible = false;
    addChild(questions_container);

var checks_group:RadioButtonGroup = new RadioButtonGroup('checks_group');

// sprite that contains our 3 check box 
var checks_container:Sprite = new Sprite();
    checks_container.visible = false;
    checks_container.x = int(stage.stageWidth / 2);
    checks_container.y = 50;
    addChild(checks_container);

// create our check box, 3 buttons in my case
// the 4th one is used only to uncheck every time all check box
for(var i:int = 0; i < 4; i++)
{
    var btn_check:RadioButton = new RadioButton();
        if(i != 3){
            btn_check.y = i * 30;       
        } else {

            // our 4th check box is hidden
            btn_check.visible = false;
        }
        btn_check.group = checks_group; 
        btn_check.name = 'btn_check' + i;
        checks_container.addChild(btn_check);
}

// start button
var btn_start:Button = new Button();
    btn_start.label = 'Start ! ';
    btn_start.width = 180;
    btn_start.height = 60;
    btn_start.x = int((stage.stageWidth - btn_start.width)/2);
    btn_start.y = int((stage.stageHeight - btn_start.height)/2) - 20;
    btn_start.addEventListener(MouseEvent.CLICK, startQuiz);
    addChild(btn_start);

// next button, to go to the next question
var btn_next:Button = new Button()
    btn_next.label = 'Next  >> ';
    btn_next.width = 90;
    btn_next.height = 30;
    btn_next.visible = false;
    btn_next.x = stage.stageWidth - btn_next.width - 10;
    btn_next.y = stage.stageHeight - btn_next.height - 10;
    btn_next.addEventListener(MouseEvent.CLICK, checkAnswer);
    addChild(btn_next); 

// a text field which will show the score and the current question number
var txt_score:TextField = new TextField();
    txt_score.width = 200;
    txt_score.height = 30;
    txt_score.x = stage.stageWidth - txt_score.width;
    txt_score.y = 10;
    txt_score.visible = false;
    txt_score.selectable = false;
    addChild(txt_score);

//----------------------------------------------------------------------------------------------------

function startQuiz(e:MouseEvent):void
{   
    // here we will get only 5 questions
    // I used a method from http://stackoverflow.com/questions/11980657/as3-random-array-randomize-array-actionscript-3 to shuffle the array
    // and then I remove the last 2 elements from the array
    enabled_questions = enabled_questions.sort(function(i:*,j:*){return(Math.random()<.5)?-1:1;}).slice(0,enabled_questions.length-2);

    for(var i:int = 0; i < enabled_questions.length; i++){
        var q_i:int = enabled_questions[i];
        var q:Object = questions_list[q_i];
        max_score += q.score;

        // create our questions instance and hide it
        // every question is an instance of a movieclip in our library
        // we can alse use only one movieclip and then we can use its timeline to go from a question to another
        var question = new (q.movie_clip);
            question.index = q_i;
            question.visible = false;
            questions_container.addChild(question);
    }       

    // hide the start button
    e.target.visible = false;

    // show other elements 
    questions_container.visible = checks_container.visible = btn_next.visible = txt_score.visible = true    

    // load the first question
    loadQuestion(current_question);

}

// check the answer and update score
function checkAnswer(e:MouseEvent):void 
{   
    var question:Object = questions_list[enabled_questions[current_question]];

    if(question.good_answer == checks_group.selectedData){

        // update the score
        setScore(question.score);       
    }

    if(current_question < enabled_questions.length - 1){        

        current_question ++;
        loadQuestion(current_question);

    } else {

        e.target.x = e.target.y = 0;
        e.target.enabled = false;
        e.target.width = stage.stageWidth;
        e.target.height = stage.stageHeight;
        e.target.label = 'You have finished the quiz, your score is : ' + score;

        checks_container.visible = questions_container.visible = txt_score.visible = false;

    }
}

// load question (show it) and update our check box
function loadQuestion(index:int):void 
{   
    var question:Object = questions_list[enabled_questions[index]];

    for(var i:int = 0; i < checks_container.numChildren; i++){      
        if(checks_container.getChildAt(i) is RadioButton){
            var btn:RadioButton = RadioButton(checks_container.getChildAt(i));          
            switch (btn.name){              
                case 'btn_check0' : btn.value = btn.label = question.first_answer; break;
                case 'btn_check1' : btn.value = btn.label = question.second_answer; break;
                case 'btn_check2' : btn.value = btn.label = question.third_answer; break;   
                case 'btn_check3' : btn.selected = true; break; 
            }               
        }       
    }

    for( i=0; i < questions_container.numChildren; i++){            
        if(questions_container.getChildAt(i) is MovieClip){
            var mc:MovieClip = MovieClip(questions_container.getChildAt(i));
                mc.visible = mc.index == question.index;    
        }       
    }

    // setScore is used here just to update the question number in the score text field
    setScore();

}

// show the score and current question
function setScore(new_score:int = 0){
    score += new_score;
    txt_score.text = 'Score : ' + score.toString() + ' / ' + max_score.toString() + ', Question : ' + (current_question+1) + ' / ' + enabled_questions.length;
}

// icons used in this example by Jozef Krajčovič : https://www.iconfinder.com/Jozef89